I want to close Jframe1
and open Jframe2
as soon as soon as any of 3 buttons (Easy, Medium, Hard) are clicked.
I have 4 classes:
- Game (that contains jframe 1)
- Action(actionlistener for easy button , also creates frame2)
- actionmedium (actionlistener for medium button , also creates frame2)
- actionhard(actionlistener for hard button , also creates frame2)
This is Game Class :
/*
package game;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
/**
*
* @author ali
*/
public class Game {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(400, 400));
frame.setTitle("Free The Corrupt");
// Level Grid buttons
frame.setLayout(new GridLayout(1, 3));
//easy button
JButton button1 = new JButton();
button1.setText("Easy");
button1.setBackground(Color.YELLOW);
button1.setSize(10,10);
ActionListener listener1 = new Action();
button1.addActionListener(listener1);
frame.add(button1);
// medium button
JButton button2 = new JButton();
button2.setText("Medium");
button2.setBackground(Color.ORANGE);
button2.setSize(10,10);
ActionListener listener2 = new actionmedium();
button2.addActionListener(listener2);
frame.add(button2);
// hard button
JButton button3 = new JButton();
button3.setText("Hard");
button3.setBackground(Color.RED);
button3.setSize(10,10);
ActionListener listener3 = new actionhard();
button3.addActionListener(listener3);
frame.add(button3);
// In Game Name
JPanel name = new JPanel(new FlowLayout());
name.add(new JLabel("Player Name : "));
name.add(new JTextField(10));
frame.add(name, BorderLayout.SOUTH);
frame.add(new JLabel(new ImageIcon("C:\\Users\\ali\\Desktop\\unnamed.png")));
frame.setVisible(true);
frame.setLayout(new FlowLayout());
}
}
This is action class : public class Action implements ActionListener {
public void actionPerformed(ActionEvent event){
JOptionPane.showMessageDialog(null,"You Selected Easy Level ! Get Ready To Play ");
JFrame frame2 = new JFrame();
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setSize(new Dimension(600, 600));
frame2.setTitle("Case 1");
frame2.setVisible(true);
frame2.setLayout(new FlowLayout());
}
}
This is actionmedium : public class actionmedium implements ActionListener {
public void actionPerformed(ActionEvent event){
JOptionPane.showMessageDialog(null,"You Selected Medium Level ! Get Ready To Play ");
JFrame frame2 = new JFrame();
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setSize(new Dimension(600, 600));
frame2.setTitle("Case 1");
frame2.setVisible(true);
frame2.setLayout(new FlowLayout());
frame2.setVisible(true);
}
}
This is actionhard : public class actionhard implements ActionListener {
public void actionPerformed(ActionEvent event){
JOptionPane.showMessageDialog(null,"You Selected Hard Level ! Get Ready To Play ");
JFrame frame2 = new JFrame();
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setSize(new Dimension(600, 600));
frame2.setTitle("Case 1");
frame2.setVisible(true);
frame2.setLayout(new FlowLayout());
}
}
Appologies for any formatting errors