JFrame whose content changes as we click on the di

2019-05-10 17:20发布

I am using Java's Swing here to make a UI application. I have a created a JFrame, with some buttons. When I click on this button, I want a new JFrame with some different content at this place. However, I do not want a new JFrame to load here.

One approach, I know is of setting the visbility of the second JFrame to be True in the actionPerformed(ActionEvent obj) method of the button in the first JFrame. But it again loads a new JFrame and I don't want that.

public class FirstUI extends JFrame {
    JButton but1;

    public FirstUI(){
        but1= new JButton("Click here");
        add(but1);

    XYZ obj= new XYZ():
    but1.addActionListener(obj);
    }

    public class XYZ implements ActionListener{
        public void actionPerformed(ActionEvent obj1){

             // WHAT TO DO HERE  
        } 
    }
}

I only want a single JFrame whose content changes as we click on different buttons. How can I achieve that ?

4条回答
疯言疯语
2楼-- · 2019-05-10 18:01

One approach is to change the JFrame's Content Pane. which is basically a JPanel. You can do that byframe.setContentPane( <your new panel> );
The Second approach is to do what @Peter Lang did. and that is to use a Layout Manager which could change different content groups.

查看更多
冷血范
3楼-- · 2019-05-10 18:14

You can also dynamically manipulate the contents of your JFrame at runtime. You can use add(...), remove(...), removeAll(...) methods to add and remove the contents as you do before showing the frame. After you're done you need to call revalidate() and repaint() methods of the modified container to make everything settle down and displayed properly.

However I think the right solution depends on the actual concept you are trying to implement. If you want to add or remove a couple of GUI elements to emphasize a functionality, then the correct way is to manipulate the container as I outlined. But if you want slightly different GUI depending on system state (not more then 2-3) then CardLayout would be a better suited choice

查看更多
你好瞎i
4楼-- · 2019-05-10 18:14

You can set visibility of parent class false.

Then you get only one Frame at a time with your required content.

You have to create static object of the frame and setVisible(fase) at the click event of the Button.

Ex.

public class demo  {
static JFrame jf;
public static void main(String a[])
{
    JButton b=new JButton("OK");
    JPanel jp=new JPanel();
    jf=new JFrame();
    jf.setVisible(true);
    jf.setSize(200,200);
    jf.add(jp);
    jp.add(b);

    b.addActionListener( new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
     jf.setVisible(false);
     JFrame f= new JFrame();
     f.setSize(200,200);
     f.setVisible(true);
    }
    });
}

}

It will help you.

you got my point?

查看更多
姐就是有狂的资本
5楼-- · 2019-05-10 18:20

Have a look at CardLayout, this would allow to switch the content of your frame:

A CardLayout object is a layout manager for a container. It treats each component in the container as a card. Only one card is visible at a time, and the container acts as a stack of cards. The first component added to a CardLayout object is the visible component when the container is first displayed.

See How to Use CardLayout for an example.

查看更多
登录 后发表回答