Calling One JFrame from Another Using Timer without Buttons
Q: How can I call one JFrame from another using a timer without using any buttons?
A: While the provided question lacks clarity, it's not advisable to use multiple frames for GUI design. Instead, consider using a modeless dialog as described below.
Solution Using a Modeless Dialog
This solution uses a modeless dialog with an enclosed JOptionPane to display a countdown. The JOptionPane listens for a PropertyChangeEvent using javax.swing.Timer.
Model Class (JOptionTimeTest)
<code class="java">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(); // Main method public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { new JOptionTimeTest().createGUI(); } }); } // Create GUI and display countdown private void createGUI() {</code>
The above is the detailed content of How to Display a Countdown in a Separate Window Using a Timer without Buttons?. For more information, please follow other related articles on the PHP Chinese website!