Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中鼠标悬停时更改扩展JButton的背景_Java_Image_Swing_Jbutton_Mouselistener - Fatal编程技术网

如何在java中鼠标悬停时更改扩展JButton的背景

如何在java中鼠标悬停时更改扩展JButton的背景,java,image,swing,jbutton,mouselistener,Java,Image,Swing,Jbutton,Mouselistener,我正在使用我的类调用KButton,它是从JButton bui扩展而来的。我添加了一些使它更漂亮的代码,例如更改字体、设置圆角边框、使用图形和Graphics2D更改背景。然而,当我想添加代码使其在移动时改变颜色时,它是不起作用的!我的密码在这里 public class KButton extends JButton implements MouseMotionListener{ private static final long serialVersionUID = 1L;

我正在使用我的类调用KButton,它是从JButton bui扩展而来的。我添加了一些使它更漂亮的代码,例如更改字体、设置圆角边框、使用图形和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);
    }

}

而且它似乎不起作用

不要使用
getGraphics
。执行自定义绘制的合适位置在
paintComponent
方法中
getGraphics
是对上次用于绘制组件的图形上下文的临时引用,当重新绘制组件时,任何更改都将被各种
paintXxx
方法中的更改覆盖

您也不应该自己调用任何
paintXxx
方法(当然,除非您试图将组件渲染为图像)

相反,使用状态标志来更改
paintComponent
的工作方式,并在需要更新状态时调用
repaint

在你的例子中,至少有两件事会破坏你在
mouseMoved
方法中的绘画效果,
setText
和鼠标移动本身。这两种情况都会导致重新绘制

就我个人而言,我会使用
MouseListener#mouseenterned
MouseListener#mouseExited
来代替,并更改按钮模型的状态(例如翻转),然后在
paintComponent
方法中检查该值以做出绘制决策


另外,请注意
super.paintComponent
将尝试清除图形上下文,准备绘制,并且应首先调用不要在
mouseMoved
中进行绘制,只需将属性设置为“绘制”,然后重新绘制组件。此外,MouseListener还提供mouseEntered和mouseExited事件,这两种事件在本用例中工作得更好:

public class KButton extends JButton {
  private Color bottomBg = Color.LIGHT_GRAY;

  public KButton(String text) {
    super(text);
    addMouseListener(this);
  }

  @Override
  protected void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    g2.setPaint(new GradientPaint(new Point(0, 0), Color.WHITE, new Point(0, getHeight()), this.bottomBg));
    g2.fillRect(0, 0, getWidth(), getHeight());
  }

  public void mouseEntered(MouseEvent evt) {
    this.bottomBg = Color.BLUE.brighter();
    this.repaint();
  }

  public void mouseExited(MouseEvent evt) {
    this.bottomBg = Color.LIGHT_GRAY;
    this.repaint();
  }

  // add other MouseListener methods, or use a MouseAdapter 
  // with just those two methods overridden

}

尽管@MadProgrammer的建议是正确的,但您可以通过设置按钮滚动图像来忽略所有这些负担:

//In constructor
setRolloverImage(myRolloverImage); 

但我不确定这是确切的方法名称,请做一些研究

您可以考虑使用定制的PLAF。