2013-09-23 62 views
1

我遇到了改變我的按鈕背景顏色的問題,我剛剛開始學習如何執行GUI,並且我有以下代碼在Windows上完美工作,但我使用的是Mac OS。如何在Mac OS上更改我的按鈕顏色?

你能幫我解決嗎?

我已經添加了按鈕,然後改變了顏色,但是在運行它時,它彈出了帶有9個按鈕的窗口,但它們全都是白色的,背景是紅色的,如您所見我設置了它們在顏色藍色。

 b1.setBackground(Color.BLUE); 
     b2.setBackground(Color.BLUE); 
     b3.setBackground(Color.BLUE); 
     b4.setBackground(Color.BLUE); 
     b5.setBackground(Color.BLUE); 
     b6.setBackground(Color.BLUE); 
     b7.setBackground(Color.BLUE); 
     b8.setBackground(Color.BLUE); 
     b9.setBackground(Color.BLUE); 

     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setExtendedState(JFrame.MAXIMIZED_BOTH); 
     this.getContentPane().setBackground(Color.RED); 
     this.setBackground(Color.BLACK); 
     this.setVisible(true); 
     this.setBounds(100, 100, 800, 500); 
+0

嘗試評論'this.getContentPane()。setBackground(Color.RED);'line – 2013-09-23 15:35:28

回答

1

在OSX和Windows上繪製按鈕的默認方式似乎有所不同。

因爲你的問題很廣,我不是很確定預期的行爲是什麼,所以這裏是你的問題的一些解決方案:

我只是在我的Mac測試,你需要做的是指定的按鈕應該是不透明:

b1.setOpaque(true); 

根據您要完成的,它可能會爲你指定的邊界不應被繪的要求:

b1.setBorderPainted(false); 

如果您不指定不應繪製邊框,則您將獲得一個帶有藍色背景的正方形和一箇中間爲默認顏色的按鈕。

如果你想要的東西看起來是一樣的這兩個操作系統上,你應該使用下面一行在你的JFrame:

try { 
     UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); 
} 
catch (Exception e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

這最後一種方法會讓你失去的OSX/Windows的外觀和感覺,雖然,但按鈕可能看起來更像你所期望的。