In this post, we will see Java Swing Multi Frame Example | Swing Trading Multiple Time Frames | Java Swing Multiple Windows.
Watch following video:
Watch on YouTube: https://www.youtube.com/watch?v=lbirAW1jDN4
Go through the following program.
Program (SwingMultiFrame.java)
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class SwingMultiFrame { public static void main(String args[]) { Frame1 obj1=new Frame1(); } } class Frame1 extends JFrame implements ActionListener { JButton b=new JButton("New Frame"); public Frame1() { setVisible(true); setSize(250,400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new FlowLayout()); add(b); b.addActionListener(this); } public void actionPerformed(ActionEvent ae) { Frame2 obj2=new Frame2(); //dispose(); } } class Frame2 extends JFrame { public Frame2() { setVisible(true); setSize(250,400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new FlowLayout()); JLabel l1=new JLabel("Welcome To New Frame"); add(l1); } } |
How To Run on Terminal:
parag@parag-Inspiron-N4010:~/Desktop/swing$ javac SwingMultiFrame.java parag@parag-Inspiron-N4010:~/Desktop/swing$ java SwingMultiFrame |
Output:
Note:
If you wish that first frame should get disappeared when second frame appear on screen, then call dispose() method after creating object of Frame2.
If you want that after clicking on CROSS button on second frame, first frame should remain on screen, then remove statement setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); from the constructor of Frame2.
While if you want that after clicking on CROSS button of first frame, second frame should remain on screen, then remove setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); from the constructor of Frame1.
No comments:
Post a Comment