簡體   English   中英

使用AWT組件在Java中更改MenuBar,Menu和MenuItem的顏色

[英]Change Color of MenuBar, Menu, and MenuItem in Java using the AWT Components

我一直在搜索,但是我沒有找到關於是否可以更改菜單欄,菜單和菜單欄的背景,文本,前景,選擇和文本選擇顏色的答案。 MenuItem AWT組件。

到目前為止,我已經嘗試了以下解決方案,但是都不會影響任何與菜單相關的組件的顏色。 第一個解決方案僅嘗試獲取組件並更改顏色,第二個解決方案嘗試通過UIManager進行更改。

// Just an example of what I did, this is not from the code I'm working with.    
MenuBar bar = new MenuBar();
Menu menu = new Menu();
MenuItem item = new MenuItem();

bar.getAccessibleContext().getAccessibleComponent().setBackground(Color.RED);
bar.getAccessibleContext().getAccessibleComponent().setForeground(Color.BLUE);

menu.getAccessibleContext().getAccessibleComponent().setBackground(Color.RED);
menu.getAccessibleContext().getAccessibleComponent().setForeground(Color.BLUE);

item.getAccessibleContext().getAccessibleComponent().setBackground(Color.RED);
item.getAccessibleContext().getAccessibleComponent().setForeground(Color.BLUE);

-

UIManager.put("MenuItem.background", Color.RED);
UIManager.put("MenuItem.foreground", Color.BLUE);

之前我沒有對AWT組件進行過多的工作,如果答案很明顯,請對不起。


更新:

如果您希望能夠輕松更改組件顏色,則似乎使用AWT組件似乎不是一個好主意。 我將重構我的代碼,以消除盡可能多的AWT組件,而使用Swing組件,並且建議閱讀此文檔的人盡可能地做同樣的事情。

我建議改用Swing組件:它們提供了更大的靈活性:

JMenuBar bar = new JMenuBar();
bar.setBackground(Color.RED);
bar.setForeground(Color.BLUE);

將Swing組件與現有的AWT組件集成在一起應該沒有問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM