I have two java files (classes) PMotion.java and Menu.java.
Menu.java has a button. When I press that button, I want it to dispose the current Menu.java frame and go to the PMotion.java class. As you can see in the file Menu.java, I have
new PMotion().setVisible(true);
frame.dispose();
The frame.dispose(); works, as it closes Menu.java. But instead of opening PMotion.java, it just opens a blank JFrame. Please note: I only copied the relevant code from PMotion.java - in reality, it is a fully working application. Help is appreciated!
PMotion.java
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.JTextArea;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JSeparator;
import javax.swing.JScrollBar;
public class PMotion extends JFrame {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
PMotion window = new PMotion();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public PMotion() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame("Projectile Motion");
frame.setBounds(100, 100, 800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setResizable(false);
Menu.java
JButton btnProjectile = new JButton("Projectile");
btnProjectile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new PMotion().setVisible(true);
frame.dispose();
}
});
btnProjectile.setBounds(260, 137, 117, 29);
frame.getContentPane().add(btnProjectile);
}
}