本文介绍了在各种LAF实例中,JTextField是什么样的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Swing文本字段在各种Pluggable Look&感觉实现吗?

What does a Swing text field look like when editable, not editable and disabled, across various Pluggable Look & Feel implementations?

以下是在Windows&上看到的PLAF的答案. Apple OSX.我也非常感谢看到其他PLAF(例如* nix上的GTK)的外观.

Below is an answer for PLAFs seen on Windows & Apple OS X. I'd also very much appreciate seeing the look on other PLAFs (e.g. GTK on *nix).

推荐答案

他们说图片描绘了1000个单词,所以这是一个6K单词的答案.

They say a picture paints a thousand words, so here's a 6K word answer.

请注意,在Nimbus& 不可编辑文本字段的背景与可编辑文本字段的背景相同,而在其他三个主题中,背景看起来不同.

Note that in Nimbus & Motif PLAFs, the background of the non-editable text field appears same as the editable text field, while in three others, it looks different.

已禁用文本字段显示为与所有PLAF中的可编辑或不可编辑字段不同.

The disabled text field appears different to either the editable or non-editable fields in all PLAFs.

使用此代码在您的系统/JRE上进行测试.

Use this code to test on your system / JRE.

import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.*;
import javax.imageio.ImageIO;
import java.io.*;

public class TextFieldPLAF {

    TextFieldPLAF() {
        initUI();
    }

    public final void initUI() {
        UIManager.LookAndFeelInfo[] lafInfos = UIManager.getInstalledLookAndFeels();
        try {
            for (UIManager.LookAndFeelInfo lAFI : lafInfos) {
                saveImageOfLookAndFeel(lAFI);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private void saveImageOfLookAndFeel(UIManager.LookAndFeelInfo lafi) throws IOException {
        String classname = lafi.getClassName();
        try {
            UIManager.setLookAndFeel(classname);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        JComponent ui = new JPanel(new GridLayout(1, 0));
        ui.setBorder(new TitledBorder(classname));
        int cols = 13;
        JTextField tf;
        tf = new JTextField("Editable & Enabled", cols);
        ui.add(tf);
        tf = new JTextField("Not Editable", cols);
        tf.setEditable(false);
        ui.add(tf);
        tf = new JTextField("Not Enabled", cols);
        tf.setEnabled(false);
        ui.add(tf);
        JOptionPane.showMessageDialog(null, ui);
        BufferedImage bi = new BufferedImage(
                ui.getWidth(), ui.getHeight(), BufferedImage.TYPE_INT_RGB);
        ui.paint(bi.getGraphics());
        File dir = new File(System.getProperty("user.home"));
        File f = new File(dir, String.format("PLAF-%1s.png", classname));
        ImageIO.write(bi, "png", f);
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            new TextFieldPLAF();
        };
        SwingUtilities.invokeLater(r);
    }
}

这篇关于在各种LAF实例中,JTextField是什么样的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 16:30