我希望程序在按下按钮时等待2秒钟。 我有闪烁按钮的代码:
import javax.swing.*;
import java.awt.event.*;
import java.awt.Color;
public class a
{
static JFrame frame = new JFrame();
static JButton button = new JButton("Hello");
public static void main(String[] args) {
frame.add(button);
frame.pack();
frame.setVisible(true);
blinking(); //this is the blinking part
//this is where the waiting 2 seconds should be
JOptionPane.showMessageDialog(null,"Popup message", "Title", JOptionPane.PLAIN_MESSAGE);
//rest of the code of my program
}
private void blinking() {
button.setOpaque(true);
Timer blinkTimer = new Timer(500, new ActionListener() {
boolean on=false;
public void actionPerformed(ActionEvent e) {
// blink the button background on and off
button.setBackground( on ? Color.YELLOW : null);
on = !on;
}
});
blinkTimer.start();
}
}
我希望程序在2秒内闪烁,然后打开JOptionPane
。它正在做的是它打开JOptionPane
而不等待2秒。
我已经尝试使用Thread.sleep(2000)
进行等待,但它似乎不起作用,在2000毫秒的等待时间内按钮不会闪烁。
有什么建议?
注意: 我无法将JOptionPane移出main()。
答案 0 :(得分:3)
使用你已经拥有的计时器来帮助你识别2秒钟的时间,你可以通过计算你的计时器调用actionPerformed方法的次数来做到这一点。当它被调用4次(2秒)时,停止定时器。很简单:
Timer blinkTimer = new Timer(500, new ActionListener() {
private int count = 0;
private int maxCount = 4;
private boolean on = false;
public void actionPerformed(ActionEvent e) {
if (count >= maxCount) {
button.setBackground(null);
((Timer) e.getSource()).stop();
} else {
button.setBackground( on ? Color.YELLOW : null);
on = !on;
count++;
}
}
});
blinkTimer.start();
你也告诉我你想在这个闪烁期间以某种方式暂停你的某些程序的执行,为此我建议你创建一个方法,比如enableExecution(boolean)
为你做这个,你在启动Timer之前调用,并在Timer完成时再次调用,如:
Timer blinkTimer = new Timer(500, new ActionListener() {
private int count = 0;
private int maxCount = 4;
private boolean on = false;
public void actionPerformed(ActionEvent e) {
if (count >= maxCount) {
button.setBackground(null);
((Timer) e.getSource()).stop();
enableExecution(true); // ***** re-enable execution of whatever *****
} else {
button.setBackground( on ? Color.YELLOW : null);
on = !on;
count++;
}
}
});
enableExecution(false); // ***** disable execution of whatever *****
blinkTimer.start();
修改强>
关于您编辑的代码,我仍然要求最好的解决方案是再次调用显示对话框的代码。如果你的问题是调用计时器的类没有在对话框中正确显示所需的信息,那么考虑将这些信息传递给该类。例如:
import java.awt.Color;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class BetterA extends JPanel {
private static final int TIMER_DELAY = 500;
private JButton button = new JButton("Hello");
// information needed by the dialog:
private String message;
private String title;
public BetterA(String message, String title) {
add(button);
blinking();
// set the dialog information
this.message = message;
this.title = title;
}
private void blinking() {
button.setOpaque(true);
Timer blinkTimer = new Timer(TIMER_DELAY, new ActionListener() {
private int count = 0;
private int maxTime = 2000;
private boolean on = false;
public void actionPerformed(ActionEvent e) {
if (count * TIMER_DELAY >= maxTime) {
button.setBackground(null);
((Timer) e.getSource()).stop();
Window win = SwingUtilities.getWindowAncestor(BetterA.this);
JOptionPane.showMessageDialog(win, message, title,
JOptionPane.PLAIN_MESSAGE);
} else {
button.setBackground(on ? Color.YELLOW : null);
on = !on;
count++;
}
}
});
blinkTimer.start();
}
private static void createAndShowGui() {
String myMessage = "Popup message";
String myTitle = "Some Title";
BetterA mainPanel = new BetterA(myMessage, myTitle);
JFrame frame = new JFrame("BetterA");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
修改2
另一个选择是使用Swing的固有PropertyChangeListener支持。例如:
import java.awt.Color;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
public class BetterA2 extends JPanel {
private static final int TIMER_DELAY = 500;
private static final int MAX_TIME = 2000;
private static final String READY = "ready";
private JButton button = new JButton("Hello");
public BetterA2() {
add(button);
blinking();
}
private void blinking() {
button.setOpaque(true);
Timer blinkTimer = new Timer(TIMER_DELAY, new ActionListener() {
private int count = 0;
private boolean on = false;
public void actionPerformed(ActionEvent e) {
if (count * TIMER_DELAY >= MAX_TIME) {
button.setBackground(null);
((Timer) e.getSource()).stop();
// !!!
firePropertyChange(READY, READY, null);
} else {
button.setBackground(on ? Color.YELLOW : null);
on = !on;
count++;
}
}
});
blinkTimer.start();
}
private static void createAndShowGui() {
final BetterA2 mainPanel = new BetterA2();
mainPanel.addPropertyChangeListener(BetterA2.READY,
new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
JOptionPane.showMessageDialog(mainPanel, "Popup message",
"Title", JOptionPane.PLAIN_MESSAGE);
}
});
JFrame frame = new JFrame("BetterA");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}