亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我的組合框無法正確更新其顏色?

為什么我的組合框無法正確更新其顏色?

藍山帝景 2023-10-12 16:59:01
我正在我的程序中實現深色模式,一切正常,除了組合框之外,它不想按照我的意愿更改其顏色。正如您所看到的,組合框的“彈出窗口”很好地改變了顏色,但組合框本身卻沒有。組合框的前景色也發生變化,但背景顏色不變。我想,外觀和感覺可能會導致問題。在我的主課中:UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );我更改為深色模式的地方:TeamInterface.userFilterComboBox.setBackground( darkBackgroundColor );TeamInterface.userFilterComboBox.setForeground( fontColor );SwingUtilities.updateComponentTreeUI( TeamInterface.userFilterComboBox );我必須使用 updateComponentTreeUI-Method,因為否則“彈出窗口”也保持白色。如果我刪除主類中的外觀和感覺,組合框看起來不錯,正如您在這張圖片中看到的,但我不想擺脫系統的外觀和感覺,所以我嘗試使用以下代碼手動將組合框的 UI 編輯為金屬:userFilterComboBox.setUI( new MetalComboBoxUI() );但是..結果很糟糕,即使理論上(至少我是這么認為的)它應該看起來和沒有外觀和感覺一樣
查看完整描述

1 回答

?
慕桂英4014372

TA貢獻1871條經驗 獲得超13個贊

Combobox 不是僅用于背景和前景的組件,而是復雜的組件。示例:JComboBox 的組成為:

  • 箭頭按鈕

  • 推力一覽表

  • 邊框(它有顏色)

  • 所選項目

因此,要進行更改,您可以在 UIManager 中添加所有常量,或者您可以定義一個新的 UIComponent。

因此,PersonalComboBoxUI可以執行以下操作:

/**

?* @contributor https://github.com/vincenzopalazzo

?*/

public class PersonalComboBoxUI extends BasicComboBoxUI {


? ? public static ComponentUI createUI (JComponent c) {

? ? ? ? return new PersonalComboBoxUI ();

? ? }


? ? @Override

? ? public void installUI (JComponent c) {

? ? ? ? super.installUI (c);


? ? ? ? JComboBox<?> comboBox = (JComboBox<?>) c;

? ? ? ? comboBox.setBackground (UIManager.getColor ("ComboBox.background"));

? ? ? ? comboBox.setForeground (UIManager.getColor ("ComboBox.foreground"));

? ? ? ? comboBox.setBorder (UIManager.getBorder ("ComboBox.border"));

? ? ? ? comboBox.setLightWeightPopupEnabled (true);

? ? }


? ? @Override

? ? protected JButton createArrowButton () {

? ? ? ? Icon icon = UIManager.getIcon ("ComboBox.buttonIcon");

? ? ? ? JButton button;

? ? ? ? if (icon != null) {

? ? ? ? ? ? button = new JButton (icon);

? ? ? ? }

? ? ? ? else {

? ? ? ? ? ? button = new BasicArrowButton (SwingConstants.SOUTH);

? ? ? ? }

? ? ? ? button.setOpaque (true);

? ? ? ? button.setBackground (UIManager.getColor ("ComboBox.buttonBackground"));

? ? ? ? button.setBorder (BorderFactory.createLineBorder(Color.black));

? ? ? ? return button;

? ? }


? ? @Override

? ? protected ListCellRenderer createRenderer() {

? ? ? ? return new MaterialComboBoxRenderer();

? ? }

}

您還應該定義 PersonalComboBoxRenderer


/**

?* @contributor https://github.com/vincenzopalazzo

?*/

? ? public class PersonalComboBoxRenderer extends BasicComboBoxRenderer {


? ? ? ? @Override

? ? ? ? public Component getListCellRendererComponent (JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {

? ? ? ? ? ? JComponent component = (JComponent) super.getListCellRendererComponent (list, value, index, isSelected, cellHasFocus);


? ? ? ? ? ? component.setBorder (BorderFactory.createEmptyBorder (5, 5, 5, 5));

? ? ? ? ? ? component.setForeground (UIManager.getColor ("ComboBox.foreground"));

? ? ? ? ? ? component.setBackground (isSelected || cellHasFocus ?

? ? ? ? ? ? ? ? ? ? UIManager.getColor("ComboBox.selectedInDropDownBackground") :

? ? ? ? ? ? ? ? ? ? UIManager.getColor("ComboBox.background"));


? ? ? ? ? ? return component;

? ? ? ? }

? ? }

ps:在本例中,我使用 UIManager.put("ComboBox.background", COLOR) 在 JComponent 內進行添加和分層。


所以我想添加兩個信息,關于如果您在 UIManager 或 PersonalComboBoxUI 中使用個人顏色,則應使用此代碼定義顏色


Color PINK_400 = new ColorUIResource (236, 64, 122);

因為當您去刪除外觀和感覺時,顏色無法刪除,但如果您使用ColorUIResource,則應該正確刪除外觀和感覺。


最后,如果您不需要默認的外觀,我建議您使用一個庫。


Material -UI-swing有一個系統主題,用于在您的應用程序中創建個人計時,并且所有主題都是可個性化的。


這是倉庫vincenzoapalazzo/material-ui-swing和atarw/material-ui-swing是相同的存儲庫和相同的開發人員,因此 vincenzopalazzo /material-us-swing是開發人員分支,包含更多修復和測試。


圖書館的一個例子是

https://img1.sycdn.imooc.com/6527b5bf00017a9a06520384.jpg



查看完整回答
反對 回復 2023-10-12
  • 1 回答
  • 0 關注
  • 103 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號