2011-12-23 62 views
0

f.e.我有一個電子郵件客戶端,它收到新消息,帶有傳入消息的按鈕開始執行某些操作,直到用戶單擊它以查看最新消息。Java swing:選擇/取消選擇JButton來模仿脈衝

我試圖通過選擇,等待然後取消選擇按鈕來吸引注意力,但這無能爲力!

do{ 
     button.setSelected(true); 
       Thread oThread = new Thread() { 
        @Override 
        public void run() { 
         synchronized (this) { 
          try { 
           wait(1000); 
          } catch (InterruptedException e1) { 
           e1.printStackTrace(); 
          } 
         } 
         button.setSelected(false); 
        } 
       }; 
       oThread.start(); 
     }while(true); 
+1

不要通過抓住重點指出的重要性。嘗試改爲將按鈕圖標從紅色版本更改爲綠色(或一些不太可怕的顏色組合)。 – 2011-12-23 10:31:52

+0

@ bunnyjesse112問題--->你想抖動與JButton內的文本或與JButton,或與JButton和測試抖? – mKorbel 2011-12-23 10:35:23

+1

如何用脈衝動畫GIF替換按鈕圖標? – 2011-12-23 10:39:42

回答

5

你應該使用Swing定時器。不要與來自外部線程的GUI對象交互。

Java教程中有一些文檔:How to use Swing timers

下面是一個示例方法,您可以使用按鈕的圖標進行播放。

// member var 
Icon buttonIcon; 
Timer timer; 
// in constructor for example 
    buttonIcon = new ImageIcon("resources/icon.png"); 
    button.setIcon(buttonIcon); 

    timer = new Timer(1000, this); 
    timer.start(); 
// in the actionPerformed handler 
    if (button.getIcon() == null) 
    button.setIcon(icon); 
    else 
    button.setIcon(null); 

你的類將需要實現ActionListener此類似工作。添加一些邏輯,以在需要時停止閃爍。

4

hafl_workaround您的問題

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

public class ShakingButtonDemo implements Runnable { 

    private JButton button; 
    private JRadioButton radioWholeButton; 
    private JRadioButton radioTextOnly; 

    public static void main(String[] args) throws Exception { 
     SwingUtilities.invokeLater(new ShakingButtonDemo()); 
    } 

    @Override 
    public void run() { 
     radioWholeButton = new JRadioButton("The whole button"); 
     radioTextOnly = new JRadioButton("Button text only"); 
     radioWholeButton.setSelected(true); 
     ButtonGroup bg = new ButtonGroup(); 
     bg.add(radioWholeButton); 
     bg.add(radioTextOnly); 
     button = new JButton(" Shake with this Button "); 
     button.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       shakeButton(radioWholeButton.isSelected()); 
      } 
     }); 
     JPanel p1 = new JPanel(); 
     p1.setBorder(BorderFactory.createTitledBorder("Shake Options")); 
     p1.setLayout(new GridLayout(0, 1)); 
     p1.add(radioWholeButton); 
     p1.add(radioTextOnly); 
     JPanel p2 = new JPanel(); 
     p2.setLayout(new GridLayout(0, 1)); 
     p2.add(button); 
     JFrame frame = new JFrame(); 
     frame.setTitle("Shaking Button Demo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(p1, BorderLayout.NORTH); 
     frame.add(p2, BorderLayout.SOUTH); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    private void shakeButton(final boolean shakeWholeButton) { 
     final Point point = button.getLocation(); 
     final Insets margin = button.getMargin(); 
     final int delay = 75; 
     Runnable r = new Runnable() { 

      @Override 
      public void run() { 
       for (int i = 0; i < 30; i++) { 
        try { 
         if (shakeWholeButton) { 
          moveButton(new Point(point.x + 5, point.y)); 
          Thread.sleep(delay); 
          moveButton(point); 
          Thread.sleep(delay); 
          moveButton(new Point(point.x - 5, point.y)); 
          Thread.sleep(delay); 
          moveButton(point); 
          Thread.sleep(delay); 
         } else {// text only 
          setButtonMargin(new Insets(margin.top, margin.left + 3, margin.bottom, margin.right - 2)); 
          Thread.sleep(delay); 
          setButtonMargin(margin); 
          Thread.sleep(delay); 
          setButtonMargin(new Insets(margin.top, margin.left - 2, margin.bottom, margin.right + 3)); 
          Thread.sleep(delay); 
          setButtonMargin(margin); 
          Thread.sleep(delay); 
         } 
        } catch (InterruptedException ex) { 
         ex.printStackTrace(); 
        } 
       } 
      } 
     }; 
     Thread t = new Thread(r); 
     t.start(); 
    } 

    private void moveButton(final Point p) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       button.setLocation(p); 
      } 
     }); 
    } 

    private void setButtonMargin(final Insets margin) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       button.setMargin(margin); 
      } 
     }); 
    } 
}