
I am making a system that requires to connect two already made J Frames such that by clicking on a button on the first J Frame, the second J Frame appears and the first one disappears.what is the code to do this?

1)Frame1.java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Frame1{
public static void main(String[] args){
Frame1 sr = new Frame1();
}
public Frame1(){
final JFrame frame = new JFrame("Frame1");
frame.setLayout(null);
JButton b = new JButton("Click");
b.setBounds(20,20,80,20);
frame.add(b);
frame.setSize(300,100);
frame.setVisible(true);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Frame2 f=new Frame2();
frame.setVisible(false);
f.setVisible(true);
f.setSize(300,100);
}
});
}
}
2)Frame2.java:
import javax.swing.*;
class Frame2 extends JFrame
{
Frame2()
{
setTitle("Frame2");
add(new JLabel("Hello World"));
setVisible(true);
pack();
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.