'How to go from one JFrame to another Jframe with a JButton
I have removed all of the code besides the JFrame stuff so it would be easier to see. I want to be able to click a JButton, and that should take me to an existing JFrame in another class. I will provide two classes below. I added the action listener however it does nothing.
package testjframe;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*
*
*/
public class TestJframe
{
public static void main(String[] args)
{
JFrame P1Frame = new JFrame ( "Project 1" );
P1Frame.pack();
P1Frame.getContentPane().setBackground(Color.BLACK);
P1Frame.setSize(new Dimension(800, 900));
P1Frame.setLayout(null);
P1Frame.setLocationRelativeTo(null);
P1Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
P1Frame.setLayout ( new FlowLayout () );
P1Frame.setVisible ( true );
P1Frame.setResizable(false);
JButton f2 = new JButton("Frame 2");
P1Frame.add(f2);
f2.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
new NewForm();
}
});
}
}
package testjframe;
import java.awt.*;
import javax.swing.*;
/**
*
*
*/
class NewForm
{
public static void main(String[] args)
{
JFrame P2Frame = new JFrame ( "Project 1 second Frame" );
P2Frame.pack();
P2Frame.getContentPane().setBackground(Color.RED);
P2Frame.setSize(new Dimension(800, 900));
P2Frame.setLayout(null);
P2Frame.setLocationRelativeTo(null);
P2Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
P2Frame.setLayout ( new FlowLayout () );
P2Frame.setVisible ( true );
P2Frame.setResizable(false);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|