如何在點擊時用不同的漸變重畫JButton。我已經重寫了paintComponent(Graphics)方法來執行初始繪製。 Onclick我想重畫它,但我不希望用戶在actionperformed事件中這樣做,因爲我希望這是一個獨立的組件。用不同的漸變重畫迴旋按鈕
任何想法如何實現。
感謝
如何在點擊時用不同的漸變重畫JButton。我已經重寫了paintComponent(Graphics)方法來執行初始繪製。 Onclick我想重畫它,但我不希望用戶在actionperformed事件中這樣做,因爲我希望這是一個獨立的組件。用不同的漸變重畫迴旋按鈕
任何想法如何實現。
感謝
最簡單的方法是使用setPressedIcon()
,但你也可以覆蓋paint()
在ButtonUI
代表,如本example。
而另一個有趣的例子:
import java.util.List;
import javax.swing.*;
import javax.swing.plaf.ColorUIResource;
public class GradieltButton {
public static void main(String[] args) {
Object grad = UIManager.get("Button.gradient");
List gradient;
if (grad instanceof List) {
gradient = (List) grad;
System.out.println(gradient.get(0));
System.out.println(gradient.get(1));
System.out.println(gradient.get(2));
System.out.println(gradient.get(3));
System.out.println(gradient.get(4));
//gradient.set(2, new ColorUIResource(Color.blue));
//gradient.set(3, new ColorUIResource(Color.YELLOW));
//gradient.set(4, new ColorUIResource(Color.GREEN));
//gradient.set(2, new ColorUIResource(221, 232, 243));//origal Color
//gradient.set(2, new ColorUIResource(255, 255, 255));//origal Color
//gradient.set(2, new ColorUIResource(184, 207, 229));//origal Color
gradient.set(2, new ColorUIResource(190, 230, 240));
gradient.set(3, new ColorUIResource(240, 240, 240));
gradient.set(4, new ColorUIResource(180, 200, 220));
//UIManager.put("Button.background", Color.pink);
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GradieltButton().makeUI();
}
});
}
public void makeUI() {
JButton button = new JButton("Click");
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(button);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
@ShaggyInjun寫道出於某種原因,我UIManager.get將按( 「Button.gradient」) 返回空。你知道爲什麼嗎?我知道我正在使用MetalTheme。
此Key in UIManager
返回ColorUIResource
(more in UIManagerDefaults by @camickr)
[0.3,0.0,javax.swing.plaf.ColorUIResource [R = 221,G = 232,B = 243], javax.swing中。 plaf.ColorUIResource [R = 255,G = 255,b = 255], javax.swing.plaf.ColorUIResource [R = 184,G = 207,b = 229]]
是需要使用ColorUIResource
而不是Gradient
,Button.gradien
t返回arrays of Colors and Insets
== ColorUIResource
由於某種原因,我的UIManager.get(「Button.gradient」)'返回null。你知道爲什麼嗎?我知道我正在使用MetalTheme。 – ShaggyInjun 2013-02-25 06:55:24
@ShaggyInjun se我在這裏編輯的更多信息 – mKorbel 2013-02-25 08:28:18
實際的調用是'UIManager.getDefaults()。get(「Button.gradient」)'。 – ShaggyInjun 2013-03-01 03:45:03