2
\$\begingroup\$

I want to make like a sort of main menu (in GUI).
When the user clicks the start button, the screen transition into another "screen" (JPanel). This image will make it easier to understand.
https://i.sstatic.net/gEwkq.png

Currently, I have a MainMenu extends JPanel and that gets added into a driver class with a JFrame. I can't figure how to switch to another class like Game extends JPanel.
So when the user clicks the start button in MainMenu, I want it to somehow hide itself and the Game to show itself.
Thanks.

\$\endgroup\$

4 Answers 4

2
\$\begingroup\$

Something like that?

enter image description here enter image description here

I made the SlidingLayout library for that purpose. It's a very small and easy-to-use library which aim is to create smooth transitions between two layouts of some components. Thus, making a transition between two screens is very easy to do.

The difference between the two transitions shown in these gifs relies on two lines of code. You can also create more fancy transitions by applying a different delay on each component, so they appear not all at once but with some timing variations between them.

I hope it may be useful to you :)

\$\endgroup\$
0
\$\begingroup\$

If you just want a different JPanel to be displayed when the user clicks on the menu then you should give your JFrame's content pane a java.awt.CardLayout. You then add your MainMenu and Game as layout components. Whenever you want to switch between the two, you can just call the next method of your CardLayout after you detected a click.

MainMenu mainMenu = new MainMenu();
Game game = new Game();

JFrame frame = new JFrame();
Container contentPane = frame.getContentPane();
CardLayout cardLayout = new CardLayout();

contentPane.setLayout(cardLayout);
contentPane.add(mainMenu, "MainMenu");
contentPane.add(game, "Game");

frame.setSize(600, 400);
frame.setVisible(true);

while (true) {
    try {
        Thread.sleep(1000);
    } catch (Exception e) {
        e.printStackTrace();
    }

    cardLayout.next(contentPane);
}

However, this will not give you a smooth animated transition, it just flips from one JPanel to another one.

\$\endgroup\$
0
\$\begingroup\$

Fancy animations between panels isn't really recommended for Swing layouts, as you'll have to run all the offsets and positions yourself, running an extra thread to update them and send repaint requests.

I'd recommend writing your own UI, as that would provide you with the most flexibility for designing your interface.

If you'd rather use Swing, I highly recommend writing your own layout manager.
(it's not that hard, really, though you'll have to make sure that it interacts with a thread somewhere for the animations to work)

\$\endgroup\$
0
\$\begingroup\$

The easiest to do this is to add BOTH JPanels or JComponents to the JFrame and set only ONE of then visible. Then you would have a function that would call the visible component to be invisible and the selected component to be visible.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .