猿问

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

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

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


紫衣仙女
浏览 125回答 1
1回答

人到中年有点甜

他们说一张图片描绘了一千个单词,所以这里有一个 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);    }}
随时随地看视频慕课网APP

相关分类

Java
我要回答