启用JLabel闪烁3次,然后保持不可见/消失

我打算编写控制JLabel闪烁三次的java代码,然后在第三次闪烁后启用其中的文本保持透明/“消失”。

从下面的代码中可以看出,我已经能够编写一个持续闪烁的JLabel,但是想创建一个只闪烁三次的JLabel,然后使其中的文本保持透明。

public class BlinkLabel extends JLabel { private static final long serialVersionUID = 1L; private static final int BLINKING_RATE = 1000; // in ms private boolean blinkingOn = true; public Timer timer; public BlinkLabel(String text) { super(text); timer = new Timer( BLINKING_RATE , new TimerListenerTwo()); timer.setInitialDelay(0); timer.start(); } public void setBlinking(boolean flag) { this.blinkingOn = flag; } public boolean getBlinking(boolean flag) { return this.blinkingOn; } public class TimerListenerTwo implements ActionListener{ int counter = 1; public TimerListenerTwo() { } public void actionPerformed(ActionEvent evt){ if(counter == 3){//3 = YOUR MAX timer.stop(); } counter++; } } } 

我将上述函数调用如下:

 BlinkLabel label = new BlinkLabel(""); label.setText("Blink blink"); 

如何编辑上面的代码以使JLabel闪烁三次并使文本消失。

任何想法/建议都非常感谢。

它非常简单,在JFrame或JDialog中创建一个子类。

 class LbBlink implements ActionListener { private javax.swing.JLabel label; private java.awt.Color cor1 = java.awt.Color.red; private java.awt.Color cor2 = java.awt.Color.gray; private int count; public LbBlink(javax.swing.JLabel label){ this.label = label; } @Override public void actionPerformed(ActionEvent e) { if(count % 2 == 0) label.setForeground(cor1); else label.setForeground(cor2); count++; } } 

在类中声明一个变量。

 private Timer timerLB; 

之后,在您的类构造中设置变量。

 timerLB = new Timer(1000, new "Your Class".LbBlink("Your jLabel")); 

最后,在您的应用程序中,开始眨眼

 timerLB.start(); 

并停止:

 timerLB.stop(); 

尝试使用counter并在每次blink增加counter ,如果计数器为3,则清除标签的文本setText("")

编辑

 class TimerListener implements ActionListener{ int counter = 1; public void actionPerformed(ActionEvent evt){ if(counter == 3){//3 = YOUR MAX timer.stop(); } counter++; } } 
  • 方法谈论使用Swing Action而不是ActionListener , Swing Action实现了isEnabled()

意思是代码行

  public void setBlinking(boolean flag) { this.blinkingOn = flag; } public boolean getBlinking(boolean flag) { return this.blinkingOn; } 
  • 对于(已经可见的)Swing GUI的每个更改都将在Swing ActionActionListener中完成

  • 那里(也许)没有理由inheritanceJLabel,为JLabel创建局部变量, Swing Timer

  • 我想念代码Timer.setRepeats(boolean)

计数闪烁,当它足够时,在您的计时器上停止呼叫。

不需要计时器。只需使用THREAD即可完成。

 import java.awt.Color; import javax.swing.*; class Sample { int i; private JFrame f; private JLabel l; Sample() throws InterruptedException { f = new JFrame("test"); f.setSize(400, 400); l = new JLabel("Testing"); f.add(l); f.setVisible(true); for (i = 0; i < 6; i++) { if (i % 2 == 0) { l.setForeground(Color.red); } else { l.setForeground(Color.BLUE); } Thread.sleep(500); } l.setEnabled(false); } public static void main(String a[]) throws InterruptedException { new Sample(); } 

}

有人建议使用displayHelpMessage(参考: https ://blog.ajduke.in/2014/03/31/java-how-to-schedule-a-task-to-run-in-an-interval/)

因此,我使用它并开发了Java代码,该代码在下面的JLabel(messageLabel)上闪烁3次给定文本。 希望这会帮助那里的人。

  static int counter = 0; static ScheduledExecutorService service; void displayHelpMessage(String message) { Runnable runnable = new Runnable() { public void run() { // task to run goes here if (counter++ >= 6) { service.shutdown(); counter = 0; } else { if (counter % 2 == 1) { messageLabel.setForeground(Color.red); } else { messageLabel.setForeground(Color.black); } } } }; messageLabel.setText(message); service = Executors.newSingleThreadScheduledExecutor(); service.scheduleAtFixedRate(runnable, 0, 750, TimeUnit.MILLISECONDS); } // Call examples: displayHelpMessage("How to select panel"); displayHelpMessage("Key to use complete create/modify");