我想在 JWindow 中显示一条消息一段时间,所以我尝试使用 while() 和 sleep() 函数,但它不起作用。这是应该调用 JWindow ( 消息窗口 ) 的函数。有没有其他方法可以显示此窗口 2 秒钟?
private void showJWindow() {
boolean flag = true;
final MessageWindow window = new MessageWindow( playerName, playerInAction );
window.setVisible( true );
try {
synchronized( window ) {
while( flag ) {
window.wait( 3000 );
flag = false;
window.setVisible( false );
}
}
} catch( InterruptedException ie ) {
Thread.currentThread().interrupt();
}
}
下面是一个使用 Swing 计时器的简短示例。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class TestGUI {
public TestGUI() {
final JFrame frame = new JFrame("Test");
JButton press = new JButton("Press Me");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(press);
press.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
final JDialog dialog = new JDialog();
dialog.add(new JLabel("Here for 2 seconds"));
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
Timer timer = new Timer(2000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
dialog.dispose();
}
});
timer.setRepeats(false);
timer.start();
}
});
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestGUI();
}
});
}
}
这是我
为另一个项目整理的一个类。它更接近您要查找的内容:
public class Splash extends JWindow implements Runnable
{
private static final long serialVersionUID = -3945334716967506918L;
public void run()
{
showSplash(10000);
}
private void showSplash(int duration)
{
JPanel content = (JPanel)getContentPane();
//Set the windows bounds, centering the window
int width = 558;
int height = 430;
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screen.width-width)/2;
int y = (screen.height-height)/2;
setBounds(x,y,width,height);
//build the splash screen
//SPECIAL NOTE: WHEN ADDING A NEW IMAGE TO THE PROJECT, REFRESH ECLIPSE
//WITH F5 SO THAT THE FILE GETS RECOGNIZED. OTHERWISE YOU'LL GET NULLPOINTEREXCEPTION.
JLabel label = new JLabel (new ImageIcon(getClass().getResource("data/images/World_Magnifier_Splash.jpg")));
JLabel copyrt = new JLabel("Copyright 2012, Tactical Enterprises Ltd.", JLabel.CENTER);
copyrt.setFont(new Font("Sans-Serif", Font.BOLD, 12));
content.add(label, BorderLayout.CENTER);
content.add(copyrt, BorderLayout.SOUTH);
content.setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
//display
setVisible(true);
try
{
Thread.sleep(duration);
}
catch(Exception e){}
setVisible(false);
}
}
您可以通过创建线程在 main 方法中运行它,如下所示:
Thread t = new Thread(new Splash());
t.start();
希望这有帮助。
尝试使用 JDialog 而不是 JWindow。下面的代码显示了 3 秒钟的对话框。
public static void main(String[] args) {
JDialog dialog = new JDialog();
dialog.getContentPane().add(new JLabel("test"));
dialog.pack();
dialog.setVisible(true);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
dialog.setVisible(false);
}