簡體   English   中英

不使用任何按鈕使用Timer從另一個JFrame調用

[英]Calling one JFrame from another using Timer without any buttons

使用Timer不帶任何按鈕的方式從另一個JFrame調用一個JFrame:減少時間,然后打開不帶任何按鈕的另一個JFrame。 請幫忙。 用於netbeans

您的問題尚不清楚,但是不建議使用多個框架。 或者,考慮如下所示的無模式對話框。 對話框附帶的JOptionPane偵聽PropertyChangeEvent ,同時使用javax.swing.TimerTIME_OUT JOptionPane按鈕很方便,但不是必需的

在此處輸入圖片說明

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.Timer;

/**
 * @see https://stackoverflow.com/a/12451673/230513
 */

public class JOptionTimeTest implements ActionListener, PropertyChangeListener {

    private static final int TIME_OUT = 10;
    private int count = TIME_OUT;
    private final Timer timer = new Timer(1000, this);
    private JDialog dialog = new JDialog();
    private final JOptionPane optPane = new JOptionPane();

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                new JOptionTimeTest().createGUI();
            }
        });
    }

    private void createGUI() {
        JFrame frame = new JFrame("Title");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        timer.setCoalesce(false);
        optPane.setMessage(message());
        optPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
        optPane.setOptionType(JOptionPane.DEFAULT_OPTION);
        optPane.addPropertyChangeListener(this);
        dialog.add(optPane);
        dialog.pack();
        frame.add(new JLabel(frame.getTitle(), JLabel.CENTER));
        frame.pack();
        frame.setVisible(true);
        dialog.setLocationRelativeTo(frame);
        dialog.setVisible(true);
        timer.start();
    }

    public void propertyChange(PropertyChangeEvent e) {
        String prop = e.getPropertyName();
        if (JOptionPane.VALUE_PROPERTY.equals(prop)) {
            thatsAllFolks();
        }
    }

    public void actionPerformed(ActionEvent e) {
        count--;
        optPane.setMessage(message());
        if (count == 0) {
            thatsAllFolks();
        }
        timer.restart();
    }

    private String message() {
        return "Closing in " + count + " seconds.";
    }

    private void thatsAllFolks() {
        dialog.setVisible(false);
        dialog.dispatchEvent(new WindowEvent(
            dialog, WindowEvent.WINDOW_CLOSING));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM