问题
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."
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");
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.
回答1:
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();
And for stop:
timerLB.stop();
回答2:
try using a counter
and increase the counter
on every blink
and if the counter is 3 then
clear the label's text setText("")
EDIT
class TimerListener implements ActionListener{
int counter = 1;
public void actionPerformed(ActionEvent evt){
if(counter == 3){//3 = YOUR MAX
timer.stop();
}
counter++;
}
}
回答3:
- methods talking about usage of Swing Action instead of
ActionListener
, Swing Action has implemented isEnabled()
meaning code lines
public void setBlinking(boolean flag) {
this.blinkingOn = flag;
}
public boolean getBlinking(boolean flag) {
return this.blinkingOn;
}
every changes to the (already visible) Swing GUI will be done inside
Swing Action
,ActionListener
in your casethere (maybe) not reason to subclassing JLabel, create an local variable for
JLabel
,Swing Timer
,I miss there code
Timer.setRepeats(boolean)
回答4:
Count blinks and when it is enough call stop on your timer.
回答5:
No timer is need.Simply using THREAD we can done it.
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();
}
}
回答6:
Someone suggested using displayHelpMessage (Reference: https://blog.ajduke.in/2014/03/31/java-how-to-schedule-a-task-to-run-in-an-interval/)
So, I used it and developed Java code that blinks given text 3 times on a JLabel(messageLabel) below. Hope this will help somebody out there.
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");
来源:https://stackoverflow.com/questions/17524080/enable-blinking-of-jlabel-3-times-and-then-remain-invisible-disappear