本文介紹了Java-如何用顏色填充Windows L&;amp;F按鈕,而不僅僅是邊框?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在用Windows 7上的Java做幾個圖形用戶界面測試。我想使用Windows的外觀,因為Java的默認”金屬”用戶界面非常難看。當我設置按鈕的背景色時,它只顯示按鈕周圍的邊框,而不是填充整個背景。
public class GUITest {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {} // I know, not a good idea, but it's just a test
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
JButton button = new JButton("Windows L&F");
button.setFocusable(false);
button.setBackground(Color.GREEN.darker());
frame.add(button);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
結果如下:
鑒于我的目標是這樣的東西(但不使用金屬L&;F):
有沒有辦法用Windows L&;F來實現這一點,或者它根本不是內置的?
推薦答案
某些Plafs支持(自定義)彩色按鈕。其他人則沒有。
要實現您似乎想要的效果,一種方法是將按鈕文本寫在帶有綠色BG的圖像中,并將該圖像用作不帶文本的按鈕圖標。
這篇關于Java-如何用顏色填充Windows L&;amp;F按鈕,而不僅僅是邊框?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,