簡體   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