如何添加在Java这个圆形按钮的金属背景? [英] How to add to this round button metal background in java?
问题描述
如何给这个按钮的背景是作为JButton的结果
我想给这个按钮的金属色或颜色grediant如何做的?
进口java.awt中的*。
java.awt.event中导入*。
RoundButton - 产生一个轻量级的按钮类
轻型组件可以有透明的地区,这意味着
可以看到这些区域后面的容器的背景
@燮pressWarnings(串行)
公共类RoundButton扩展组件{ActionListener的ActionListener的; //邮政操作事件听众
字符串标签; //按钮的文本
保护布尔pressed = FALSE; // true,如果按钮被带定位。
/ **
*构造不带标签的RoundButton。
* /
公共RoundButton(){
这个();
}/ **
*构造具有指定标签的RoundButton。
* @参数标记按钮的标签
* /
公共RoundButton(字符串标签){
this.label =标签;
enableEvents方法(AWTEvent.MOUSE_EVENT_MASK);
}/ **
*取标签
* @see setLabel
* /
公共字符串getLabel(){
返回标签;
}/ **
*设置标签
* @see getLabel
* /
公共无效setLabel(字符串标签){
this.label =标签;
无效();
重绘();
}/ **
*描绘了RoundButton
* /
公共无效漆(图形G){
INT S = Math.min(的getSize()宽度 - 1,的getSize()高度 - 1); //绘制按钮的内部
如果(pressed){
g.setColor(的getBackground()较暗()较暗());
}其他{
g.setColor(的getBackground());
}
g.fillArc(0,0,S,S,0,360); //绘制按钮的周边
g.setColor(的getBackground()较暗()较暗()较暗()。);
g.drawArc(0,0,S,S,0,360);
//绘制按钮中心的标签
字体F =的getFont();
如果(F!= NULL){
FontMetrics对象FM = getFontMetrics(的getFont());
g.setColor(getForeground());
g.drawString(标签,
S / 2 - fm.stringWidth(标签)/ 2,
S / 2 + fm.getMaxDescent());
}
}/ **
*按钮的preferred大小。
* /
公共尺寸的get preferredSize(){
字体F =的getFont();
如果(F!= NULL){
FontMetrics对象FM = getFontMetrics(的getFont());
INT最大= Math.max(fm.stringWidth(标签)+ 40,fm.getHeight()+ 40);
返回新尺寸(最大,最大值);
}其他{
返回新尺寸(100,100);
}
}/ **
*按钮的最小大小。
* /
公共尺寸getMinimumSize的(){
返回新尺寸(100,100);
}/ **
*添加指定的操作侦听器,以接收动作事件
*从这个按钮。
* @参数监听器动作监听
* /
公共无效addActionListener方法(ActionListener的监听器){
的ActionListener = AWTEventMulticaster.add(ActionListener的,监听器);
enableEvents方法(AWTEvent.MOUSE_EVENT_MASK);
} / **
*删除指定的动作监听者,使其不再接收
*从这个按钮的动作事件。
* @参数监听器动作监听
* /
公共无效removeActionListener(ActionListener的监听器){
的ActionListener = AWTEventMulticaster.remove(ActionListener的,监听器);
}/ **
*确定是否点击了圆形按钮里面。
* /
公共布尔为contains(int x,int y)对{
。INT MX =的getSize()宽/ 2;
。INT我=的getSize()高度/ 2;
返回(((MX-X)*(MX-X)+(我的-γ)*(我的-γ))≤= MX * MX);
} / **
*绘制按钮和动作事件分配给所有听众。
* /
公共无效processMouseEvent(的MouseEvent E){
@燮pressWarnings(未使用)
图形克;
开关(e.getID()){
案例MouseEvent.MOUSE_ preSSED:
//渲染自己倒....
pressed = TRUE; //重画可能会闪烁了一下。为了避免这种情况,你可以使用
//双缓冲(见计的例子)。
重绘();
打破;
案例MouseEvent.MOUSE_RELEASED:
如果(ActionListener的!= NULL){
actionListener.actionPerformed(新的ActionEvent(
此,ActionEvent.ACTION_PERFORMED,标记));
}
//再次渲染自己正常
如果(pressed ==真){
pressed = FALSE; //重画可能会闪烁了一下。为了避免这种情况,你可以使用
//双缓冲(见计的例子)。
重绘();
}
打破;
案例MouseEvent.MOUSE_ENTERED: 打破;
案例MouseEvent.MOUSE_EXITED:
如果(pressed ==真){
//取消!不要发送动作事件。
pressed = FALSE; //重画可能会闪烁了一下。为了避免这种情况,你可以使用
//双缓冲(见计的例子)。
重绘(); //注意:一个更完整的按钮实现,
//你不希望在此时取消,但
//而检测,当鼠标再往里面
//重新突出的按钮。有几个国家
//问题是,你需要处理,这是我们离开
//此为读者提供一个锻炼; Tibial一个(我总是
//想说的!)
}
打破;
}
super.processMouseEvent(E);
}}
java.awt.Component中
不支持不透明度/透明度,只有Swing组件做的。
您应该花时间通读
您有一些问题,开始与...
- 您应该从重量轻(摇摆)组件扩展,因为它们支持透明
- 有没有必要重写
processMouseEvent
,你应该使用的MouseListener
而不是 - 您
包含
方法应考虑到按钮的形 - 您应该添加新的侦听器时使用内置的事件管理API
- 您应该偏向
的paintComponent
在油漆
绘画轻量级组件时 - 您必须首先从您覆盖任何涂料的方法调用
super.paintXxx
,只有一个次极少数的时候,你不会,然后你成为负责接管那里工作 - 您需要标记通过调用组件作为透明
setOpaque(假)
联合国pressed / pressed
公共类TestRoundButton { 公共静态无效的主要(字串[] args){
新TestRoundButton();
} 公共TestRoundButton(){
EventQueue.invokeLater(新的Runnable(){
@覆盖
公共无效的run(){
尝试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}赶上(ClassNotFoundException的前){
}赶上(InstantiationException前){
}赶上(IllegalAccessException前){
}赶上(UnsupportedLookAndFeelException前){
} JFrame的帧=新的JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
。frame.getContentPane()的setBackground(Color.RED);
frame.setLayout(新的GridBagLayout());
frame.add(新RoundButton(:)));
frame.pack();
frame.setLocationRelativeTo(NULL);
frame.setVisible(真);
}
});
} 公共类RoundButton继承JPanel { ActionListener的ActionListener的; //邮政操作事件听众
字符串标签; //按钮的文本
保护布尔pressed = FALSE; // true,如果按钮被带定位。 私人的MouseListener监听; / **
*构造不带标签的RoundButton。
* /
公共RoundButton(){
这个();
} @覆盖
公共无效信息addNotify(){
super.addNotify();
如果(听众== NULL){
听众=新MouseHandler();
addMouseListener将(监听);
}
} @覆盖
公共无效的removeNotify(){
removeMouseListener(监听);
super.removeNotify();
} / **
*构造具有指定标签的RoundButton。
*
* @参数标记按钮的标签
* /
公共RoundButton(字符串标签){
this.label =标签;
// ...
setOpaque(假);
//使用一个鼠标监听器,而不是
// enableEvents方法(AWTEvent.MOUSE_EVENT_MASK);
} / **
*取标签
*
* @see setLabel
* /
公共字符串getLabel(){
返回标签;
} / **
*设置标签
*
* @see getLabel
* /
公共无效setLabel(字符串标签){
this.label =标签;
无效();
重绘();
} / **
*描绘了RoundButton
* /
// preFER的paintComponent过漆......
@覆盖
保护无效paintComponent(图形G){
//必须调用super.paintXxx没有任何借口不给,EVER !!
super.paintComponent方法(G);
INT S = Math.min(的getSize()宽度 - 1,的getSize()高度 - 1); Graphics2D的G2D =(Graphics2D的)g.create();
//绘制按钮的内部
g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING,RenderingHints.VALUE_COLOR_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_DITHERING,RenderingHints.VALUE_DITHER_ENABLE);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,RenderingHints.VALUE_STROKE_PURE); 颜色的亮点=的getBackground();
如果(pressed){
亮点= highlight.darker();
}
色darklight = highlight.darker(); LinearGradientPaint LGP =新LinearGradientPaint(
新点(0,0),
新点(O,S),
新的浮动[] {0,} 1F,
新的色彩[] {亮点,darklight}); Ellipse2D的形状=新Ellipse2D.Float(0,0,S,S);
g2d.setPaint(LGP);
g2d.fill(形状); //绘制按钮的周边
g2d.setColor(的getBackground()较暗()较暗()较暗()。);
g2d.draw(形状);
//绘制按钮中心的标签
字体F =的getFont();
如果(F!= NULL){
FontMetrics对象FM = getFontMetrics(的getFont());
g2d.setColor(getForeground());
g2d.drawString(标签,
S / 2 - fm.stringWidth(标签)/ 2,
S / 2 + fm.getMaxDescent());
}
g2d.dispose();
} / **
*按钮的preferred大小。
* /
公共尺寸的get preferredSize(){
字体F =的getFont();
如果(F!= NULL){
FontMetrics对象FM = getFontMetrics(的getFont());
INT最大= Math.max(fm.stringWidth(标签)+ 40,fm.getHeight()+ 40);
返回新尺寸(最大,最大值);
}其他{
返回新尺寸(100,100);
}
} / **
*按钮的最小大小。
* /
公共尺寸getMinimumSize的(){
返回新尺寸(100,100);
} / **
*添加指定的操作侦听器,以接收来自此动作事件
*按钮。
*
* @参数监听器动作监听
* /
公共无效addActionListener方法(ActionListener的监听器){
//的ActionListener = AWTEventMulticaster.add(ActionListener的,监听器);
// enableEvents方法(AWTEvent.MOUSE_EVENT_MASK);
listenerList.add(ActionListener.class,监听器);
} / **
*删除指定的动作监听者,使其不再接收动作
*从这个按钮事件。
*
* @参数监听器动作监听
* /
公共无效removeActionListener(ActionListener的监听器){
//的ActionListener = AWTEventMulticaster.remove(ActionListener的,监听器);
listenerList.add(ActionListener.class,监听器);
} / **
*确定是否点击了圆形按钮里面。
* /
公共布尔为contains(int x,int y)对{
//这需要更多的工作实际涂形状...
。INT MX =的getSize()宽/ 2;
。INT我=的getSize()高度/ 2;
收益率(((MX - X)*(MX - X)+(我的 - Y)*(我的 - y))为< = MX * MX);
}
/ **
*绘制按钮和动作事件分配给所有听众。
* /
//公共无效processMouseEvent(的MouseEvent E){
// @燮pressWarnings(未使用)
//图形克;
//开关(e.getID()){
//情况下MouseEvent.MOUSE_ preSSED:
// //渲染自己倒....
pssed // $ P $ = TRUE;
//
// //重画可能会闪烁了一下。为了避免这种情况,你可以使用
// //双缓冲(见计的例子)。
//重绘();
//突破;
//情况MouseEvent.MOUSE_RELEASED:
//如果(ActionListener的!= NULL){
// actionListener.actionPerformed(新的ActionEvent(
//这个,ActionEvent.ACTION_PERFORMED,标签));
//}
// //使自己恢复正常
//如果(pressed ==真){
pssed // $ P $ = FALSE;
//
// //重画可能会闪烁了一下。为了避免这种情况,你可以使用
// //双缓冲(见计的例子)。
//重绘();
//}
//突破;
//情况MouseEvent.MOUSE_ENTERED:
//
//突破;
//情况MouseEvent.MOUSE_EXITED:
//如果(pressed ==真){
// //取消!不要发送动作事件。
pssed // $ P $ = FALSE;
//
// //重画可能会闪烁了一下。为了避免这种情况,你可以使用
// //双缓冲(见计的例子)。
//重绘();
//
// //注意:一个更完整的按钮实现,
// //你不希望在此时取消,但
// //而检测,当鼠标再往里面
// //重新突出的按钮。有几个国家
// //问题是,你需要处理,这是我们离开
// //这是一个为读者锻炼; Tibial(我一直
// //想说的!)
//}
//突破;
//}
// super.processMouseEvent(E);
//} 公共类MouseHandler扩展MouseAdapter { @覆盖
公共无效鼠标pressed(的MouseEvent E){
pressed = TRUE;
重绘();
} @覆盖
公共无效的mouseReleased(的MouseEvent E){
pressed = FALSE;
重绘();
} } }
}
现在,已经做了这一切,你可能会喜欢在的 javax.swing.AbstractButton中你基本组件
How to give this button background to be as jbutton
i want to give this button metal color or grediant color how to make that?
import java.awt.*;
import java.awt.event.*;
RoundButton - a class that produces a lightweight button.
Lightweight components can have "transparent" areas, meaning that you can see the background of the container behind these areas.
@SuppressWarnings("serial")
public class RoundButton extends Component {
ActionListener actionListener; // Post action events to listeners
String label; // The Button's text
protected boolean pressed = false; // true if the button is detented.
/**
* Constructs a RoundButton with no label.
*/
public RoundButton() {
this("");
}
/**
* Constructs a RoundButton with the specified label.
* @param label the label of the button
*/
public RoundButton(String label) {
this.label = label;
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
}
/**
* gets the label
* @see setLabel
*/
public String getLabel() {
return label;
}
/**
* sets the label
* @see getLabel
*/
public void setLabel(String label) {
this.label = label;
invalidate();
repaint();
}
/**
* paints the RoundButton
*/
public void paint(Graphics g) {
int s = Math.min(getSize().width - 1, getSize().height - 1);
// paint the interior of the button
if(pressed) {
g.setColor(getBackground().darker().darker());
} else {
g.setColor(getBackground());
}
g.fillArc(0, 0, s, s, 0, 360);
// draw the perimeter of the button
g.setColor(getBackground().darker().darker().darker());
g.drawArc(0, 0, s, s, 0, 360);
// draw the label centered in the button
Font f = getFont();
if(f != null) {
FontMetrics fm = getFontMetrics(getFont());
g.setColor(getForeground());
g.drawString(label,
s/2 - fm.stringWidth(label)/2,
s/2 + fm.getMaxDescent());
}
}
/**
* The preferred size of the button.
*/
public Dimension getPreferredSize() {
Font f = getFont();
if(f != null) {
FontMetrics fm = getFontMetrics(getFont());
int max = Math.max(fm.stringWidth(label) + 40, fm.getHeight() + 40);
return new Dimension(max, max);
} else {
return new Dimension(100, 100);
}
}
/**
* The minimum size of the button.
*/
public Dimension getMinimumSize() {
return new Dimension(100, 100);
}
/**
* Adds the specified action listener to receive action events
* from this button.
* @param listener the action listener
*/
public void addActionListener(ActionListener listener) {
actionListener = AWTEventMulticaster.add(actionListener, listener);
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
}
/**
* Removes the specified action listener so it no longer receives
* action events from this button.
* @param listener the action listener
*/
public void removeActionListener(ActionListener listener) {
actionListener = AWTEventMulticaster.remove(actionListener, listener);
}
/**
* Determine if click was inside round button.
*/
public boolean contains(int x, int y) {
int mx = getSize().width/2;
int my = getSize().height/2;
return (((mx-x)*(mx-x) + (my-y)*(my-y)) <= mx*mx);
}
/**
* Paints the button and distribute an action event to all listeners.
*/
public void processMouseEvent(MouseEvent e) {
@SuppressWarnings("unused")
Graphics g;
switch(e.getID()) {
case MouseEvent.MOUSE_PRESSED:
// render myself inverted....
pressed = true;
// Repaint might flicker a bit. To avoid this, you can use
// double buffering (see the Gauge example).
repaint();
break;
case MouseEvent.MOUSE_RELEASED:
if(actionListener != null) {
actionListener.actionPerformed(new ActionEvent(
this, ActionEvent.ACTION_PERFORMED, label));
}
// render myself normal again
if(pressed == true) {
pressed = false;
// Repaint might flicker a bit. To avoid this, you can use
// double buffering (see the Gauge example).
repaint();
}
break;
case MouseEvent.MOUSE_ENTERED:
break;
case MouseEvent.MOUSE_EXITED:
if(pressed == true) {
// Cancel! Don't send action event.
pressed = false;
// Repaint might flicker a bit. To avoid this, you can use
// double buffering (see the Gauge example).
repaint();
// Note: for a more complete button implementation,
// you wouldn't want to cancel at this point, but
// rather detect when the mouse re-entered, and
// re-highlight the button. There are a few state
// issues that that you need to handle, which we leave
// this an an excercise for the reader (I always
// wanted to say that!)
}
break;
}
super.processMouseEvent(e);
}
}
java.awt.Component
does not support opacity/transparency, only Swing components do.
You should take the time to read through
You have a number issues to start with...
- You should extend from a light weight (Swing) component, as they support transparency
- There is no need to override
processMouseEvent
, you should use aMouseListener
instead - Your
contains
method should take into consideration the "shape" of the button - You should use the inbuilt event management API when adding new listeners
- You should favor
paintComponent
overpaint
when painting lightweight components - YOU MUST CALL
super.paintXxx
from any paint method you override, there is only a very small number of times when you wouldn't, and then you become responsible for taking over there work - You need to mark the component as transparent by call
setOpaque(false)
Unpressed/Pressed
public class TestRoundButton {
public static void main(String[] args) {
new TestRoundButton();
}
public TestRoundButton() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.RED);
frame.setLayout(new GridBagLayout());
frame.add(new RoundButton(":)"));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class RoundButton extends JPanel {
ActionListener actionListener; // Post action events to listeners
String label; // The Button's text
protected boolean pressed = false; // true if the button is detented.
private MouseListener listener;
/**
* Constructs a RoundButton with no label.
*/
public RoundButton() {
this("");
}
@Override
public void addNotify() {
super.addNotify();
if (listener == null) {
listener = new MouseHandler();
addMouseListener(listener);
}
}
@Override
public void removeNotify() {
removeMouseListener(listener);
super.removeNotify();
}
/**
* Constructs a RoundButton with the specified label.
*
* @param label the label of the button
*/
public RoundButton(String label) {
this.label = label;
//...
setOpaque(false);
// Use a mouse listener instead
// enableEvents(AWTEvent.MOUSE_EVENT_MASK);
}
/**
* gets the label
*
* @see setLabel
*/
public String getLabel() {
return label;
}
/**
* sets the label
*
* @see getLabel
*/
public void setLabel(String label) {
this.label = label;
invalidate();
repaint();
}
/**
* paints the RoundButton
*/
// Prefer paintComponent over paint...
@Override
protected void paintComponent(Graphics g) {
// YOU MUST CALL super.paintXxx THERE IS NO EXCUSE NOT TO, EVER!!
super.paintComponent(g);
int s = Math.min(getSize().width - 1, getSize().height - 1);
Graphics2D g2d = (Graphics2D) g.create();
// paint the interior of the button
g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
Color highlight = getBackground();
if (pressed) {
highlight = highlight.darker();
}
Color darklight = highlight.darker();
LinearGradientPaint lgp = new LinearGradientPaint(
new Point(0, 0),
new Point(0, s),
new float[]{0, 1f},
new Color[]{highlight, darklight});
Ellipse2D shape = new Ellipse2D.Float(0, 0, s, s);
g2d.setPaint(lgp);
g2d.fill(shape);
// draw the perimeter of the button
g2d.setColor(getBackground().darker().darker().darker());
g2d.draw(shape);
// draw the label centered in the button
Font f = getFont();
if (f != null) {
FontMetrics fm = getFontMetrics(getFont());
g2d.setColor(getForeground());
g2d.drawString(label,
s / 2 - fm.stringWidth(label) / 2,
s / 2 + fm.getMaxDescent());
}
g2d.dispose();
}
/**
* The preferred size of the button.
*/
public Dimension getPreferredSize() {
Font f = getFont();
if (f != null) {
FontMetrics fm = getFontMetrics(getFont());
int max = Math.max(fm.stringWidth(label) + 40, fm.getHeight() + 40);
return new Dimension(max, max);
} else {
return new Dimension(100, 100);
}
}
/**
* The minimum size of the button.
*/
public Dimension getMinimumSize() {
return new Dimension(100, 100);
}
/**
* Adds the specified action listener to receive action events from this
* button.
*
* @param listener the action listener
*/
public void addActionListener(ActionListener listener) {
// actionListener = AWTEventMulticaster.add(actionListener, listener);
// enableEvents(AWTEvent.MOUSE_EVENT_MASK);
listenerList.add(ActionListener.class, listener);
}
/**
* Removes the specified action listener so it no longer receives action
* events from this button.
*
* @param listener the action listener
*/
public void removeActionListener(ActionListener listener) {
// actionListener = AWTEventMulticaster.remove(actionListener, listener);
listenerList.add(ActionListener.class, listener);
}
/**
* Determine if click was inside round button.
*/
public boolean contains(int x, int y) {
// This needs to work more on the actual painted shape...
int mx = getSize().width / 2;
int my = getSize().height / 2;
return (((mx - x) * (mx - x) + (my - y) * (my - y)) <= mx * mx);
}
/**
* Paints the button and distribute an action event to all listeners.
*/
// public void processMouseEvent(MouseEvent e) {
// @SuppressWarnings("unused")
// Graphics g;
// switch (e.getID()) {
// case MouseEvent.MOUSE_PRESSED:
// // render myself inverted....
// pressed = true;
//
// // Repaint might flicker a bit. To avoid this, you can use
// // double buffering (see the Gauge example).
// repaint();
// break;
// case MouseEvent.MOUSE_RELEASED:
// if (actionListener != null) {
// actionListener.actionPerformed(new ActionEvent(
// this, ActionEvent.ACTION_PERFORMED, label));
// }
// // render myself normal again
// if (pressed == true) {
// pressed = false;
//
// // Repaint might flicker a bit. To avoid this, you can use
// // double buffering (see the Gauge example).
// repaint();
// }
// break;
// case MouseEvent.MOUSE_ENTERED:
//
// break;
// case MouseEvent.MOUSE_EXITED:
// if (pressed == true) {
// // Cancel! Don't send action event.
// pressed = false;
//
// // Repaint might flicker a bit. To avoid this, you can use
// // double buffering (see the Gauge example).
// repaint();
//
// // Note: for a more complete button implementation,
// // you wouldn't want to cancel at this point, but
// // rather detect when the mouse re-entered, and
// // re-highlight the button. There are a few state
// // issues that that you need to handle, which we leave
// // this an an excercise for the reader (I always
// // wanted to say that!)
// }
// break;
// }
// super.processMouseEvent(e);
// }
public class MouseHandler extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
pressed = true;
repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
pressed = false;
repaint();
}
}
}
}
Now, having done all that, you might like to take a serious look at javax.swing.AbstractButton as you base component
这篇关于如何添加在Java这个圆形按钮的金属背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!