如何为放置在jtoolbar中的Jbutton添加渐变颜色 [英] How to add gradient color to Jbuttons placed in jtoolbar
问题描述
我正在使用netbeans平台模块来开发此桌面应用程序.我正在开发中使用netbeans中的拖放功能.
I am using netbeans platform module to develop this desktop application. I am using drag and drop facility in netbeans in the developement.
我需要创建一个只有几个按钮的工具栏.我需要为这些按钮创建渐变颜色.
I needed to create a toolbar which is having few buttons..I need to create a gradient color for these buttons.
我拖放了JToolBar
,在其上拖放了JButton
对象.
在按钮的属性中,我选择了一种我想要阴影的颜色.在自定义代码中,我进行了修改.
I dragged and dropped JToolBar
, over it I dragged and placed JButton
objects.
In the properties of the button I have selected a color for which I want a shaded color. In the custom code I have modified.
jbutton = new javax.swing.Jbutton();
如下
jbutton = new javax.swing.JButton(){
@Override
protected void paintComponent(Graphics grphcs) {
Graphics2D g2d = (Graphics2D) grphcs;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
GradientPaint gp = new GradientPaint(0, 0,
getBackground().brighter().brighter().brighter(), 0, getHeight(),
getBackground().darker());
g2d.setPaint(gp);
g2d.fillRect(0, 0, getWidth(), getHeight());
super.paintComponent(grphcs);
}};
当我在项目中将上述代码用于JPanel
时,它可以工作,但是当我将其用于按钮时,它没有显示任何效果.
When I used the above code for a JPanel
in my project it worked but it is not showing any effect when I used it for a button.
如何为工具栏中的按钮获取渐变颜色?
How to get a gradient color for a button placed in a toolbar?
推荐答案
按钮具有contentAreaFilled
属性,该属性确定外观是否应绘制按钮的内容区域.当您调用super.paintComponent
时,外观委托将在您所做的事情上作画.
The button has a contentAreaFilled
attribute which determines if the look and feel should paint the content area of the button. When you call super.paintComponent
, the look and feel delegate will paint over what you have done.
您可以将此属性设置为false
,然后它应该可以工作.
You can set this property to false
and it should then work.
例如...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class GradientButtons {
public static void main(String[] args) {
new GradientButtons();
}
public GradientButtons() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.add(new RoundButton("Click me"));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class RoundButton extends JButton {
public RoundButton(String text) {
super(text);
setBorderPainted(false);
setContentAreaFilled(false);
setFocusPainted(false);
setOpaque(false);
}
@Override
public Dimension getPreferredSize() {
Dimension size = super.getPreferredSize();
int radius = Math.max(size.width, size.height);
size.width = radius;
size.height = radius;
return size;
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
GradientPaint gp = new GradientPaint(0, 0,
Color.RED, 0, getHeight(),
Color.YELLOW);
g2d.setPaint(gp);
g2d.fillOval(0, 0, getWidth(), getHeight());
super.paintComponent(g);
}
}
}
您可能希望检查ButtonModel
的armed
和/或pressed
状态,以便作为建议,也可以更改单击按钮时绘制按钮的方式.
You may want to check the ButtonModel
's armed
and/or pressed
state so you can also change the way that the button is painted when clicked, as a suggestion...
这篇关于如何为放置在jtoolbar中的Jbutton添加渐变颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!