JTextField 在各种 LAF 实例中是什么样子的?

What does a JTextField look like in various LAF instances?

在各种可插入外观实现中,Swing 文本字段在可编辑、不可编辑和禁用时是什么样子的?

以下是在 Windows 和 Apple OS X 上看到的 PLAF 的答案。我也非常希望看到其他 PLAF 的外观(例如 *nix 上的 GTK)。

他们说一张图画了一千个字,所以这里有一个 6K 字的答案。

请注意,在 Nimbus 和 Motif PLAF 中,不可编辑 文本字段的背景看起来与可编辑文本字段相同,而在其他三个中,它看起来不同。

已禁用 文本字段与所有 PLAF 中的可编辑或不可编辑字段不同。

代码

使用此代码在您的系统/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);
    }
}