简体   繁体   中英

Swing menu in Java

I'm wondering, how can I make a menu system (not JMenuBar) with swing components (like in games, where a new 'screen' comes in)? Maybe switching between panels? How? I've heard about card layout but i'm not sure it's for me, and I don't really understand how should I implement it. Example (sorry for ugly drawing): http://i57.tinypic.com/14mckkk.png

Based on the screenshot you've provided, you should look into using Swing Dialogs . Dialogs are little pop up windows that can be activated upon button presses, and that can contain as many or as few components as you like.

So your grey JFrame in the image would have three buttons; the action on clicking one would be to open a dialog populated with "Content A". The "Button X" in that dialog could either be a vanilla "Close" button that the dialog API will provide for you for free, or it could be some other button of your design.

If I'm not mistaken, what you are asking is for something like this:

在此处输入图片说明

In this case you can split the frame with using a layout of your choice that can fit to the size you want (personally I'd opt for a BoxLayout). Then you can go on with adding the buttons to the side panel and add that to the box panel so you end up with something like this:

在此处输入图片说明

Now add a JPanel and you'll have your content section. You'll have to make sure you add Listeners to the buttons so that the content that is displayed is updated. It'll look something like this:

button.addActionListener(
new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        if(content) {
             content.removeAll();
        } 
        populateContent();
        content.validate();
    }
});

I could be way off because of the little amount of information at hand, this is just my interpretation of the question. I would have commented but I don't have the rep so I'm sorry in advance if this is completely off!

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