1 回答

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是開發人員分支,包含更多修復和測試。
圖書館的一個例子是
添加回答
舉報