簡體   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