0

This is for a school project where we implement a GUI for our previously made system, problem is our teacher only taught us JFrame.

So in the end, what I have is, a JFrame for the MainScreen, and when I click a button to go the the OtherScreen, it will simply close one screen and open another, in the default position, which isn't ideal obviously :

// @ MainScreen :
otherScreenButton.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent ae) {
        MainController.otherScreen();
    }
});

// @ MainController :

public void otherScreen(){
    mainScreen.setVisible(false);
    otherScreen.setVisible(true);
}

My question is, how programmers go about doing GUIs? Adding JPanels inside a main JFrame and setting them visible/not visible on demand? If someone could also provide a link with more detailed information that would be great.

1
  • There are several ways: using tabbed panes instead of JPanels, create dialogs, using CardLayout. Everything depends in your needs. Also note that applications have usually only one JFrame. Commented Jun 8, 2015 at 13:20

1 Answer 1

3

Take a look at CardLayout, it will help you with 'switching screens'.

Here's the documentation.

Sign up to request clarification or add additional context in comments.

1 Comment

That is really helpful, but if I have 10+ JPanel classes to add to the main JFrame with CardLayout, how would I go about doing it with low coupling?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.