2014-03-25 25 views
5

我想創建一個Java桌面應用程序,我使用兩個按鈕。我想在這些按鈕中添加懸停效果。我想要:當我點擊任何按鈕時,它應該改變它的背景顏色。如何將懸停效果放在jbutton上?

我該如何實現它?

這裏是我的代碼:

public class Party1Party2 extends JFrame 
{ 
    JButton b1; 
    JButton b2; 
    Container pane; 

    public Party1Party2() 
    { 
     getContentPane().setBackground(new java.awt.Color(255, 255, 255)); 

    b2.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent ae) 
     { 
      JOptionPane.showMessageDialog(frame, "Welcome to allhabad High Court"); 
     } 
    }); 

    b1.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent ae) 
     { 
      JOptionPane.showMessageDialog(frame, "Welcome to allhabad High Court"); 

     } 
    }); 
    } 
} 
+0

@peeskillet你可以告訴我如何使jbutton作爲一個圓角rectangel – user3456343

+0

老實說,我只是看着改變外觀和感覺。我不喜歡弄亂組件外觀。請參閱[本文](http://stackoverflow.com/a/22166047/2587435)並參閱[修改外觀](http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/index。 html) –

+0

關於鼠標監聽器的所有嘗試都是錯誤的,帶有副作用,忽略這些答案 – mKorbel

回答

12

您可以使用滑鼠EnteredExitedJButton,和你什麼都想要。

例如:

jButton1.addMouseListener(new java.awt.event.MouseAdapter() { 
    public void mouseEntered(java.awt.event.MouseEvent evt) { 
     jButton1.setBackground(Color.GREEN); 
    } 

    public void mouseExited(java.awt.event.MouseEvent evt) { 
     jButton1.setBackground(UIManager.getColor("control")); 
    } 
}); 
+1

答案是錯誤的,所有鼠標事件都在JButtons API中實現並正確使用它們代替鼠標監聽器 – mKorbel

+6

Oh eeeeasy,我想你試過說有更好的方法來做到這一點,但這不是一個錯誤的答案,至少對我來說是因爲我做了很多次,請在你的評論中更現代化! – Salah

+1

1.'請在您的評論中更現代一些!!!' - 不要拘謹,使用MouseListener for JButton是很簡單的錯誤,所有這些事件都在JButtons API中實現,並且正確地與MouseListener比較,看看發生了什麼在JButton上移動鼠標的情況下,事件在那裏實現,只是爲了覆蓋這些事件2.「至少對我來說是因爲我做了很多次」 - 不要爲Jbuttons組件使用MosueListener,3。我的評論是關於「編輯問題,在哪裏將使用適當的事件,以建議使用適當的事件」 – mKorbel

1

我一次寫了一個定製的JButton用來改變其透明度級別時將小鼠通過動畫盤旋它。下面是該按鈕的代碼:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 

public class HoverButton extends JButton 
{ 
    float alpha = 0.5f; 

    public HoverButton(String text) 
    { 
    super(text); 
    setFocusPainted(false); 
    setBorderPainted(false); 
    setContentAreaFilled(false); 
    addMouseListener(new ML()); 
    } 

    public float getAlpha() 
    { 
    return alpha; 
    } 

    public void setAlpha(float alpha) 
    { 
    this.alpha = alpha; 
    repaint(); 
    } 

    public void paintComponent(java.awt.Graphics g) 
    { 
    java.awt.Graphics2D g2 = (java.awt.Graphics2D) g; 
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha)); 
    super.paintComponent(g2); 
    } 

    public class ML extends MouseAdapter 
    { 
    public void mouseExited(MouseEvent me) 
    { 
     new Thread(new Runnable() 
     { 
     public void run() 
     { 
      for (float i = 1f; i >= .5f; i -= .03f) 
      { 
      setAlpha(i); 
      try 
      { 
       Thread.sleep(10); 
      } 
      catch (Exception e) 
      { 
      } 
      } 
     } 
     }).start(); 
    } 

    public void mouseEntered(MouseEvent me) 
    { 
     new Thread(new Runnable() 
     { 
     public void run() 
     { 
      for (float i = .5f; i <= 1f; i += .03f) 
      { 
      setAlpha(i); 
      try 
      { 
       Thread.sleep(10); 
      } 
      catch (Exception e) 
      { 
      } 
      } 
     } 
     }).start(); 
    } 

    public void mousePressed(MouseEvent me) 
    { 
     new Thread(new Runnable() 
     { 
     public void run() 
     { 
      for (float i = 1f; i >= 0.6f; i -= .1f) 
      { 
      setAlpha(i); 
      try 
      { 
       Thread.sleep(1); 
      } 
      catch (Exception e) 
      { 
      } 
      } 
     } 
     }).start(); 
    } 
    } 
} 

而這裏的HoverButton的快速演示:

import javax.swing.*; 
import java.awt.*; 

public class Demonstration 
{ 
    public Demonstration() 
    { 
    JFrame frame = new JFrame("Hover Button Demonstration"); 
    frame.setLayout(new GridBagLayout()); 
    frame.add(new HoverButton("Hover Button!!")); 

    frame.pack(); 
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true); 
    } 

    public static void main(String args[]) 
    { 
    SwingUtilities.invokeLater(new Runnable() 
    { 
     @Override 
     public void run() 
     { 
     new Demonstration(); 
     } 
    }); 
    } 
} 

好處是,你可以調整代碼更改按鈕的背景顏色爲好,這也是動畫的方式。

+1

有時,少就是多..你的回答沒問題,但太大了.. – Volazh

0

哇。老問題,我知道,但是...

要改變的背景下,使用:

b1.setBackground(new java.awt.Color(r, g, b)); 
中的ActionListener

對於一個翻滾效果,你可以使用:

b1.setRolloverEnabled(true); 

,但你需要提供你的按鈕之間翻轉圖標。

否則,對於其他懸停效果,您確實需要使用mouseListener。