启用JLabel闪烁3次,然后保持不可见/消失 [英] Enable blinking of JLabel 3 times and then remain invisible/disappear
问题描述
我打算编写控制JLabel闪烁三次的java代码,然后在第三次闪烁后启用其中的文本保持透明/消失。
I intend on writing java code which controls a JLabel to blink three times and then after the third blink enable the text within it to remain transparent/"disappear."
如下面的代码所示,我已经能够编写一个连续闪烁的JLabel,但是想创建一个只闪烁三次然后使其中的文本保持透明的JLabel。
As indicated from the code below, I've been able to write a JLabel which continuously blinks but would like to create one that blinks only three times and then enable the text within it to remain transparent.
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++;
}
}
}
我将上述函数称为如下:
I call the above function as follows:
BlinkLabel label = new BlinkLabel("");
label.setText("Blink blink");
如何编辑上面的代码以使JLabel闪烁三次并使文本消失。
How can I edit my above code to enable the JLabel to blink three time and have the text disappear.
非常感谢任何想法/建议。
Any ideas/suggestions are greatly appreciated.
推荐答案
非常简单,在JFrame或JDialog中创建一个子类。
Its very simple, create a sub class below in your JFrame or 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++;
}
}
在你的班级中声明一个变量。
Declare a variable in your class.
private Timer timerLB;
之后,在你的类构造中设置变量。
After, in your class construct set the variable.
timerLB = new Timer(1000, new "Your Class".LbBlink("Your jLabel"));
最后,在您的应用程序中,开始闪烁
Finally, in your application, for start blink
timerLB.start();
停止:
timerLB.stop();
这篇关于启用JLabel闪烁3次,然后保持不可见/消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!