显示JWindow一段时间

时间:2014-04-06 12:35:24

标签: java multithreading swing concurrency jwindow

我想在JWindow中显示一条消息一段时间,所以我尝试使用while()和sleep()函数,但它不起作用。这是应该调用JWindow(MessageWindow)的函数。有没有其他方法可以显示此窗口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();
    }
}

3 个答案:

答案 0 :(得分:1)

以下是使用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();
            }
        });
    }
}

答案 1 :(得分:0)

这是我为另一个项目组装的课程。它更接近你正在寻找的东西:

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);
  }
}

您可以通过创建一个Thread来在main方法中运行它,如下所示:

Thread t = new Thread(new Splash());
t.start();

希望这有帮助。

答案 2 :(得分:0)

尝试使用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);
}