的按鈕顏色我有一個JButton
,我想改變爲白色的背景色。當使用金屬外觀和感覺,我實現了與setBackground
預期的效果:如何設置一個JButton(沒有背景色)
不幸的是,使用Windows LAF時的「背景色」的概念是不同的;背景色爲按鈕周圍繪製的顏色:
我想使用Windows LAF,但允許該JButton
的按鈕顏色改爲白色。我該怎麼做呢?
的按鈕顏色我有一個JButton
,我想改變爲白色的背景色。當使用金屬外觀和感覺,我實現了與setBackground
預期的效果:如何設置一個JButton(沒有背景色)
不幸的是,使用Windows LAF時的「背景色」的概念是不同的;背景色爲按鈕周圍繪製的顏色:
我想使用Windows LAF,但允許該JButton
的按鈕顏色改爲白色。我該怎麼做呢?
您必須決定是否值得您付出努力,但您可以隨時創建自己的ButtonUI
,如example中由於@mKorbel所示。
我認爲我必須這樣做。謝謝你的鏈接。 – 2011-06-06 21:01:02
我在XP上使用JDK 6。它看起來像Window UI並不遵循正常的繪畫規則,而不是1。正如你注意到setBackground()不起作用。您應該能夠告訴組件不要填寫內容區域做油畫定製:
import java.awt.*;
import javax.swing.*;
public class ButtonBackground extends JFrame
{
public ButtonBackground()
{
setLayout(new FlowLayout());
JButton normal = new JButton("Normal");
add(normal);
JButton test1 = new JButton("Test 1")
{
@Override
public void paintComponent(Graphics g)
{
g.setColor(Color.GREEN);
g.fillRect(0, 0, getSize().width, getSize().height);
super.paintComponent(g);
}
};
test1.setContentAreaFilled(false);
add(test1);
}
public static void main(String[] args)
{
try
{
// UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (Exception e2) {}
ButtonBackground frame = new ButtonBackground();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
當您運行的代碼,是它似乎正常工作。那是當你點擊按鈕時你看到邊框變化。然而,你運行Windows XP的LAF,邊界不會改變,你沒有看到按鈕點擊效果。
所以,我想這個問題是與WindowUI,你會需要定製這可能是太複雜,這樣做,我沒有一個解決方案的用戶界面。
作爲參考,在Mac OS X上,當選擇Test 1時,BackgroundButton看起來像[this](http://i.imgur.com/dRRg5.png)。 – trashgod 2011-06-06 21:04:52
,而不是與按鈕的背景顏色搞亂,你可以做任何指示你想表現出不同的方式?
顯示圖標,使得按鈕大膽而不是純文本等
只有通過不同的背景顏色指示的東西並不總是顯而易見的,並且根據用戶的系統顏色,可能是不和諧的,或無形。
例如,如果用戶在「高對比度」模式下運行窗口會怎麼樣?
,但我仍然認爲,(修改,但通過達里爾)是正確的UIManager.get("Button.gradient"),因爲是跨平臺
編輯:正確的答案應該是 - 雨雲或一些Custom L&F,爲什麼推倒重來(羅布)
你最好的選擇是使用SwingX:
JXButton允許你使用MattePainter爲背景設置一個Painter作爲背景.setBackgroundPainter(Painter)
實現你想要的。具有JXButton從JButton的延伸,改變在你的代碼是最小:
Color bg = new Color(new Random().nextInt(16777215)); // Random color
JButton button = new JButton();
button.setBackground(bg);
將成爲
JXButton button = new JXButton();
button.setBackgroundPainter(new MattePainter(bg));
嘗試重寫'JButton'的'的paintComponent(圖形G)'方法,並做你的在那裏定製繪畫。 – mre 2011-06-06 18:45:50
「我想使用Windows LAF」我想知道您的OSX和Linux用戶希望看到什麼?我敢打賭,這不是Windows LAF(即使這是實用的)。 ;) – 2011-06-06 18:51:04
@Andrew Thompson:別擔心。我正在使用系統LAF,但現在只需要Windows。 :) – 2011-06-06 18:55:12