繁体   English   中英

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

[英]How to change background an extended JButton in java when mouse over

我正在使用从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);
    }

}

看来行不通!

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

您也应该永远不要自己调用任何paintXxx方法(除非您试图将组件呈现为图像)

而是使用状态标志来更改paintComponent工作方式,并在您要更新状态时调用repaint

就您而言,至少有两件事破坏了mouseMoved方法的绘画工作: setText和鼠标移动本身。 两者都会导致repaint

就个人而言,我将改用MouseListener#mouseEnteredMouseListener#mouseExited并更改按钮模型的状态(例如,将MouseListener#mouseEntered ),然后在paintComponent方法中检查该值以做出绘画决定

另外,请注意, super.paintComponent将尝试清除图形上下文,以准备绘画,因此应首先调用

不要在mouseMoved进行绘制,只需将属性设置为paint,然后重新绘制component即可。 另外,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); 

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM