2014-09-27 329 views
2

有沒有辦法讓應用程序或Applet中的彈出窗口在一段時間後自動關閉(例如5秒)?JAVA:如何使彈出窗口自動關閉?


我已經找到了解決方案:

對於任何人都可能會尋找相同:

public static void main(String[] args) { 
    JFrame f = new JFrame(); 
    final JDialog dialog = new JDialog(f, "Test", true); 
    Timer timer = new Timer(2000, new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      dialog.setVisible(false); 
      dialog.dispose(); 
     } 
    }); 
    timer.setRepeats(false); 
    timer.start(); 

    dialog.setVisible(true); // if modal, application will pause here 

    System.out.println("Dialog closed"); 
} 

謝謝您的回答傢伙。

+0

可能會有用:http://stackoverflow.com/questions/1306868/can-i-set-a-timer-on-a-java-swing-jdialog-box-to-close-after-a-number - 毫米 – 2014-09-27 14:12:39

+0

非常感謝你的鏈接,它包含我正在尋找的答案;) – 2014-09-27 19:48:12

回答

1

爲了自動關閉彈出窗口,您必須設置新線程並在其中設置計時器。

下面

public class FrmPopUpInfo extends JDialog{ 
public boolean isCancel = false; 

private String trackHeader = "Pop Up is:"; 
private final String message; 

public FrmPopUpInfo(String message){ 
    this.message = message; 
    initComponents(); 
} 

private void initComponents() { 
    Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();// size of the screen 
    Insets toolHeight = Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration());// height of the task bar 
    setLocation(scrSize.width - 275, scrSize.height - toolHeight.bottom - 120); 
    ImageIcon image; 

    setSize(225,120); 
    setLayout(null); 
    setUndecorated(true); 
    setLayout(new GridBagLayout()); 

    GridBagConstraints constraints = new GridBagConstraints(); 
    constraints.gridx = 0; 
    constraints.gridy = 0; 
    constraints.weightx = 1.0f; 
    constraints.weighty = 1.0f; 
    constraints.insets = new Insets(5, 5, 5, 5); 
    constraints.fill = GridBagConstraints.BOTH; 

    JLabel headingLabel = new JLabel(trackHeader + message); 
    image = new ImageIcon(Toolkit.getDefaultToolkit().getImage(FrmPopUpInfo.class.getResource("/images/yourImage.jpg"))); 
    headingLabel .setIcon(image); 
    headingLabel.setOpaque(false); 

    add(headingLabel, constraints); 

    constraints.gridx++; 
    constraints.weightx = 0f; 
    constraints.weighty = 0f; 
    constraints.fill = GridBagConstraints.NONE; 
    constraints.anchor = GridBagConstraints.NORTH; 
    JButton cloesButton = new JButton(new AbstractAction("x") { 
     @Override 
     public void actionPerformed(final ActionEvent e) { 
       dispose(); 
     } 
    }); 
    cloesButton.setMargin(new Insets(1, 4, 1, 4)); 
    cloesButton.setFocusable(false); 

    add(cloesButton, constraints); 

    setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); 
    setVisible(true); 
    setAlwaysOnTop(true); 

    new Thread(){ 
      @Override 
      public void run() { 
       try { 
         Thread.sleep(5000); // time after which pop up will be disappeared. 
         dispose(); 
       } catch (InterruptedException e) { 
         e.printStackTrace(); 
       } 
      }; 
    }.start(); 
} 
} 

try代碼你撥打上面類從其他幀像這樣

FrmPopUpInfo frm = new FrmPopUpInfo(); 

彈出對話框,將在5秒消失。