简体   繁体   中英

how to start, developing swing based application with few panels with next buttons for each

I'm new to java.I'm creating a swing based UI. I've created 2 frames, each one in separate.java file inside same package.

These two frames represents 2 screens (panels) of application. When Next button in first frame is clicked, it should move to second frame.

When I checked, these two classes are having main method, I think it should be correct way for creating applications. there should be only one main method.

When Next is clicked, I'm trying to make setVisible(false) for main panel of first frame and setVisible(true) for main panel of second frame. But this cannot be done, since the panels within a class are private. Any resolution for the above problem?

As I'm beginner, Can somebody suggest me in how to start up with these kind of applications? what are the guidelines that need to be followed? And please help me in finding documentation related to starting up with the development of such applications.

After going through the answers, My comments are:

I used the following code to go to next panel from first panel, but didn't worked.

private void gotoNextPanel(){
//    jPanelFirstScreen.setVisible(false);
      JPanelSecondScreen jpanelSecondScreen= new JPanelSecondScreen();
      jpanelSecondScreen.setVisible(true);
      UpgradeUtilityGUI upgradeUtilityGUI = new UpgradeUtilityGUI();
      upgradeUtilityGUI.removeAll();
      validate();
      repaint();
//      upgradeUtilityGUI.add(jpanelSecondScreen);
            upgradeUtilityGUI.getContentPane().add(jpanelSecondScreen, "card2");
      jpanelSecondScreen.setVisible(true);
      validate();
      repaint();

    }

I'm using.netbeans, and 've added two panels to the cardlayout of frame. And when I use the above code to change panels, Nothing is happening, the first panel is still appearing. Can somebody tell me, how to write code for moving from one panel to another when both the panels 've been added to cardlayout of jFrame?

Use a CardLayout , as shown here (and one frame ) as mentioned by others.

游戏视角高分视图

When Next is clicked, I'm trying to make setVisible(false) for main panel of first frame and setVisible(true) for main panel of second frame. But this cannot be done, since the panels within a class are private. Any resolution for the above problem?

Make the panels public access level and they will be available from other packages.


One problem in that code snippet is implied by the line:

UpgradeUtilityGUI upgradeUtilityGUI = new UpgradeUtilityGUI();  

It goes out of scope before ever being added to a container. Also, their should be no need to remove anything when adding a new card to the layout, and no need to call repaint() .

If your application is as simple as having only two panels you shouldn't create two JFrames. You should create a JFrame with two JPanel each of them contains the neccessary information for you. If you are ready with your first panel you can call setVisible(false) on it, and call setVisible(true) on the 2nd frame. It is the one of the most easy-to-understand solution.
But, it only depends on you if it is good for you or you would like to use some more detailed solution.

What you should do is have a single JFrame for the application, then you add and remove JPanels as you want to move between screens.

Each of your JPanels should basically have the following... 1. A JButton called "Next" 2. A ButtonListener for each button, that tells the JFrame to load panel2, panel3, etc.

As part of the ButtonListener , you basically just want to call something like JFrame.removeAll() to remove the existing panel, then JFrame.add(JPanel) to add the next panel.

By having 1 JFrame , you also only have 1 main() method.

Don't use two or more JFrames , nor with separated and compiled Jar files , this is road to the hell, better would be look atCardLayout ,

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