user3921533
user3921533

Reputation: 37

Java - change from Panel1 to Panel2

I wanna create a simple java application, and I have some problems. This is my main class:

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MainWindow {

    private JFrame frame;

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

    /**
     * Create the application.
     */
    public MainWindow() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        frame.getContentPane().add(panel, BorderLayout.CENTER);

        JButton btnNewButton = new JButton("First B");
        panel.add(btnNewButton);
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SecWindow SW = new SecWindow();
                //-----
            }           
        });                     
    }    
}

Secound class:

import javax.swing.JButton;
import javax.swing.JPanel;

public class SecWindow {

    public SecWindow() {
        SecPanel();
    }

    public void SecPanel() {            
        JPanel panel2 = new JPanel();
        JButton btnNewButton_2 = new JButton("Sec B");
        panel2.add(btnNewButton_2);     
    }
}

How can I do this: when I press the "First B" I wanna delete the first panel and create a new one class SecWindow().

Upvotes: 0

Views: 129

Answers (2)

Bellator
Bellator

Reputation: 11

try{
secWindow secondWindow = new secWindow();
secondWindow.frame.setVisible(true);
window.frame.setVisible(false);
} catch (Exception e) {
                e.printStackTrace();

This will hide first window and show second one. You can not completely "delete" object that has main method in it. your app will start and end in main method.

instead you can make new class and transfer main method over there

Upvotes: 0

camickr
camickr

Reputation: 324118

How can I do this: when I press the "First B" I wanna delete the first panel and create a new one class SecWindow().

You should be using a CardLayout. The CardLayout will allow you to swap panels in the frame.

Read the section from the Swing tutorial on How to Use CardLayout for more information and working examples.

The example uses a combo box to swap the panels so you just need to move that code to the ActionListener of your button.

Upvotes: 4

Related Questions