0

I want to know how do you go to another panel by pressing a button.

The codes for my main GUI is below:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

public class MainMenu extends JFrame {

    private JPanel contentPane, confirmPage_Panel;
    private JTextField NumberofSoups_TEXTFIELD;
    private JTextField NumberofSandwiches_TEXTFIELD;
    private JTextField totalCost_TEXTFIELD;
    private JTextField OrderNumber_TEXTFIELD;
    private int Soupclicks = 0;
    private int Sandwichclicks = 0;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainMenu frame = new MainMenu();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MainMenu() {
        super("Welcome Yo!");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 1268, 716);
        contentPane = new JPanel();
        contentPane.setBackground(Color.DARK_GRAY);
        contentPane.setBorder(new LineBorder(new Color(255, 200, 0), 4, true));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JPanel Header_Panel = new JPanel();
        Header_Panel.setBackground(Color.DARK_GRAY);
        Header_Panel.setBounds(145, 11, 977, 35);
        contentPane.add(Header_Panel);

        JLabel Header_Label = new JLabel("Super Sandwich Store");
        Header_Label.setForeground(Color.PINK);
        Header_Label.setFont(new Font("Tahoma", Font.PLAIN, 22));
        Header_Panel.add(Header_Label);

        JPanel Soup_Panel = new JPanel();
        Soup_Panel.setBackground(Color.PINK);
        Soup_Panel.setBounds(10, 71, 459, 339);
        contentPane.add(Soup_Panel);
        Soup_Panel.setLayout(null);

        JButton Confirm_Button = new JButton("Confirm Now");
        Confirm_Button.setFont(new Font("Tahoma", Font.PLAIN, 14));
        Confirm_Button.setBounds(511, 558, 121, 23);
        contentPane.add(Confirm_Button);

        JButton Exit_Button = new JButton("Exit");
        Exit_Button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.exit(0);
            }
        });
        Exit_Button.setFont(new Font("Tahoma", Font.PLAIN, 13));
        Exit_Button.setBounds(641, 558, 111, 23);
        contentPane.add(Exit_Button);
    }// end of MainMenu()

}

And when i clicked the confirm button it will invoke this page :

public class ConfirmationGUI extends JFrame {

    private JPanel contentPane;
    private JTextField ConfirmedOrder_Field;
    private JTextField totalCost_Field;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ConfirmationGUI frame = new ConfirmationGUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public ConfirmationGUI() {
        super("Confirmation Yo!");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 668, 457);
        contentPane = new JPanel();
        contentPane.setBackground(Color.DARK_GRAY);
        contentPane.setBorder(new LineBorder(Color.ORANGE, 4, true));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JPanel Top_Panel = new JPanel();
        Top_Panel.setBackground(Color.DARK_GRAY);
        Top_Panel.setBounds(5, 5, 637, 93);
        contentPane.add(Top_Panel);
        Top_Panel.setLayout(null);

        JLabel lblNewLabel = new JLabel("Super Sandwich Store");
        lblNewLabel.setForeground(Color.PINK);
        lblNewLabel.setBounds(245, 11, 185, 45);
        Top_Panel.add(lblNewLabel);
        lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));

    }

}

It would be much of a help,

Thank you :)

Braj
  • 46,415
  • 5
  • 60
  • 76
  • 2
    Never use `null` layout and read [Swing tutorial on it](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) carefully. – Braj Jul 26 '14 at 18:27
  • 2
    [CardLayout](http://docs.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) is intended to switch components (such as [JPanel](http://docs.oracle.com/javase/8/docs/api/javax/swing/JPanel.html)), not [top-level containers](http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html) (such as [JFrame](http://docs.oracle.com/javase/8/docs/api/javax/swing/JFrame.html) or [JDialog](http://docs.oracle.com/javase/8/docs/api/javax/swing/JDialog.html)). See [How to Use CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) – dic19 Jul 26 '14 at 18:28
  • 1
    Don't use multiple `JFrame` as well. create multiple `JPanel` and switch between them. – Braj Jul 26 '14 at 18:30
  • so how do i use cardlayout(I've read the given link, still confused though). I converted my jframe to jpanel already. :( – OhwellHardwell Jul 28 '14 at 18:05

1 Answers1

0

To switch between JFrames, call setVisible(true) for the JFrame you want to reveal and setVisible(false) for the one you want to hide. CardLayout doesn't apply here.

Suggestions: read the Swing tutorial on layouts, don't use null layouts with absolutely positioning, and familiarize yourself with the differences between ordinary containers and top-level containers.

  • 1
    [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice) (bad practice) – dic19 Jul 26 '14 at 19:07