【发布时间】:2021-07-23 21:06:40
【问题描述】:
目前正在开发一个使用 Java 的 Mail API 编程的电子邮件客户端项目。我在尝试使用 UIManager 更改 Swing 中的某些属性时遇到问题。
可以看到按钮颜色没有随着当前代码的变化而变化:
protected static Credentials credentialHandler() {
String EMAIL = null;
String PASSWORD = null;
UIManager.put("OptionPane.background",new ColorUIResource(32, 38, 44));
UIManager.put("OptionPane.foreground",Color.WHITE);
UIManager.put("Panel.background",new ColorUIResource(32, 38, 44));
UIManager.put("TextField.background",new ColorUIResource(62, 68, 74));
UIManager.put("TextField.foreground",Color.WHITE);
UIManager.put("Label.foreground",Color.WHITE);
UIManager.put("PasswordField.foreground",Color.WHITE);
UIManager.put("Button.background",new ColorUIResource(62, 68, 74));
JPanel panel = new JPanel();
JLabel labelEmail = new JLabel("Enter an email:");
labelEmail.setForeground(Color.WHITE);
JTextField email = new JTextField(32);
email.setBackground(new Color(32, 38, 44));
JLabel labelPass = new JLabel("Enter a password:");
JPasswordField pass = new JPasswordField(16);
pass.setBackground(new Color(32, 38, 44));
panel.add(labelEmail);
panel.add(email);
panel.add(labelPass);
panel.add(pass);
String[] options1 = new String[]{"Login", "Cancel"};
int option1 = JOptionPane.showOptionDialog(null, panel, "Credentials",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options1, options1[1]);
if (option1 == 0) {
EMAIL = email.getText();
char[] passwordChar = pass.getPassword();
PASSWORD = new String(passwordChar);
} else {
JOptionPane.showMessageDialog(null, "Looks like you exited the program. If you think this is a mistake, please report it to the developer!");
System.exit(2);
}
return new Credentials(EMAIL, PASSWORD);
}
我想更改按钮的背景颜色以及字体颜色(假设它与前景有关)。
【问题讨论】: