JTextField在各种LAF实例中的外观如何? [关闭]

问题描述 投票:-2回答:1

在各种Pluggable Look&Feel实现中,可编辑,不可编辑和禁用时,Swing文本字段的外观如何?

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

java swing jtextfield look-and-feel
1个回答
4
投票

他们说一张图片描绘了千言万语,所以这里有一个6K字的答案。

enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here

请注意,在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);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.