繁体   English   中英

如何使用 UIManager 更改按钮属性

[英]How to change button properties using UIManager

目前正在为使用 Java 的邮件 API 编程的电子邮件客户端进行项目。 我在尝试使用UIManager更改 Swing 中的某些属性时遇到问题。

对于 JOptionPane 的特定部分,它目前看起来像这样: 在此处输入图片说明

您可以看到按钮颜色没有随着当前代码的变化而改变:

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);
}

我想更改按钮的背景颜色和字体颜色(假设它与前景有关)。

new ColorUIResource(62, 68, 74)替换为new java.awt.Color(62, 68, 74)

/* Add following "import":
 * import java.awt.Color;
 */
UIManager.put("Button.background", new Color(62, 68, 74));

要设置字体颜色,请使用以下内容(将JButton文本颜色设置为白色):

UIManager.put("Button.foreground", Color.white);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM