我正在使用从 JButton bui 扩展的类调用 KButton 我添加了一些代码使其更美观,例如更改字体、设置圆角边框、使用 Graphics 和 Graphics2D 更改背景。但是,当我想添加代码以使其在移动时改变颜色时,它不起作用!我的代码在这里
public class KButton extends JButton implements MouseMotionListener{
private static final long serialVersionUID = 1L;
public KButton(){
setStyle();
}
public KButton(String text){
super(text);
this.setText(text);
setStyle();
addMouseMotionListener(this);
}
public void setStyle(){
setFont(new Font("San Serif",Font.PLAIN,12));
setContentAreaFilled(false);
setBorder(new RoundedBorder(3));
}
@Override
protected void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D)g.create();
g2.setPaint(new GradientPaint(new Point(0, 0), Color.WHITE, new Point(0, getHeight()), Color.LIGHT_GRAY));
g2.fillRect(0, 0, getWidth(), getHeight());
g2.dispose();
super.paintComponent(g);
}
@Override
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent arg0) {
Graphics g=this.getGraphics();
Graphics2D g2 = (Graphics2D)g.create();
g2.setPaint(new GradientPaint(new Point(0, 0), Color.WHITE, new Point(0, getHeight()), Color.BLUE.brighter()));
g2.fillRect(0, 0, getWidth(), getHeight());
g2.dispose();
super.setText(getText());
setBorder(new RoundedBorder(3));
super.paintComponent(g);
}
}
它似乎不起作用!