简体   繁体   中英

Java - How to make contents of JFrame appear in the same Window instead of multiple windows

I am new to java and am getting to the advanced level of it, i have a problem in the GUI Controls, i made a button that when clicked opens up a new window like this:

     JButton b = new JButton("Open New Window");
     b.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {
     Window w = new Window();
     w.setVisible(true);
     }
     });

this window contains other objects but i have been thinking of making the button in such a way that instead of opening a new JFrame, it opens everything in that same window without opening a new window, honestly i dont know how to do so please could i get some professional help

I think you want a card layout for this situation. Here is some code which should point you in the right direction.

class MyFrame extends JFrame {

    public MyFrame() {

        JComponent allMyStuff = new JComponent();
        JComponent allMyOtherStuff = new JComponent();

        this.getContentPane().setLayout(new CardLayout());

        this.getContentPane().add(allMyStuff, "1");
        this.getContentPane().add(allMyOtherStuff, "2");

        CardLayout cl = (CardLayout) (this.getContentPane().getLayout());
        cl.show(this.getContentPane(), "1");

        JButton b = new JButton("Open New Window"); //add somewhere to first compoonent
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                CardLayout cl = (CardLayout) (this.getContentPane().getLayout());
                cl.show(this.getContentPane(), "2");
            }
        });
    }
}

I doubt the code runs but generally it holds the idea. You have stuff in one panel, and stuff in another panel, and you just want to switch between the two. The button of course needs to be added in the first panel (allMyStuff) somewhere.

I"m not clear on what it is exactly that you want to show in the GUI when the button is pressed, but perhaps you should consider creating different JPanel "views" and swap these views in the GUI using a CardLayout.

For example, check out these StackOverflow questions and answers:

Within the action listener that you have introduced, you have the possibility to access to instance variables. Therefore you can add further elements to your GUI if you want. I've done a small demo, maybe this is kind of, what you want to do. In order to make your GUI better, you should consider of using layout managers.

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

public class GUI {

JFrame frame; 
JButton btn;
JButton compToAdd;

public GUI() {
    frame = new JFrame("Testwindow");
    frame.setSize(500, 500);
    frame.setLayout(null);

    btn = new JButton("test btn");
    btn.setBounds(20, 20, 200, 200);


    btn.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            compToAdd = new JButton("new Button");
            compToAdd.setBounds(20, 220, 200, 200);
            frame.add(compToAdd);
            frame.repaint();
        }
    });

    frame.add(btn);
    frame.setVisible(true);
}

public static void main(String[] args) {
    GUI gui = new GUI();
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM