1 回答

TA贡献1895条经验 获得超7个赞
他们说一张图片描绘了一千个单词,所以这里有一个 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);
}
}
添加回答
举报