Adding JPanel from another class to JPanel in JFrame

空扰寡人 提交于 2019-11-30 09:05:43

First to answer your question, you need to add an instance of your panel to the frame with something like this in your JFrameTest constructor:

add(new JPanelOne());

You also need to add your button directly to JPanelOne itself:

public class JPanelOne extends JPanel {   

  public JPanelOne() {   
    JButton button = new JButton("test");   
    add(button);   
  }   
}

Second, I believe there is a problem with these lines of code:

    FlowLayout mainLayout = new FlowLayout(); 
    // snip...
    setLayout(mainLayout); 

    JPanel panelMain = new JPanel(mainLayout); 

Each container should have its own instance of a layout manager. Otherwise your GUI will do strange things:

setLayout(new FlowLayout());
JPanel panelMain = new JPanel(mainLayout);

With some more help (user "Hilek" from some other site) I menaged to get the JPanel from another class to be displeyed in main class. Here is the code:

JFrameTest.java:

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JFrameTest extends JFrame {

        private JButton testButton;
        private JPanel panelMain;
        private JPanelOne panel;

        public JFrameTest() {

                // setting up JFrame
                setLayout(null);
                setPreferredSize(new Dimension(420, 90));
                setResizable(false);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                // creating main JPanel (white)
                panelMain = new JPanel();
                panelMain.setBackground(Color.WHITE);
                panelMain.setBounds(0, 0, 420, 90);
                panelMain.setPreferredSize(new Dimension(200, 40));
                add(panelMain);

                // creating JButton in the main JPanel (white)
                testButton = new JButton("Button from main class");
                panelMain.add(testButton);

                // creating new JPanelOne object from JPanelOne class containing black JPanel
                panel = new JPanelOne();

                // adding black JPanel to main JPanel (white)
                panelMain.add(panel);

                pack();

        }

        public static void main(String[] arguments) {

                // creating JFrame object and setting it visible
                JFrameTest frame = new JFrameTest();
                frame.setVisible(true);

        }

}

JPanelOne.java:

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class JPanelOne extends JPanel
{

        public JPanelOne()
        {
                // setting up black JPanel
                JPanel panel = new JPanel();
                panel.setPreferredSize(new Dimension(220, 40));
                panel.setBackground(Color.BLACK);

                // creating button on external JPanel
                JButton button = new JButton("Button (+JPanel) from external class");

                // adding button to the black JPanel
                panel.add(button);

                // adding blackJPanel
                add(panel);
        }
}

Print screen of working example:

http://i.stack.imgur.com/qKeBp.jpg

Maybe someone will find it helpful in their problem.

The problem comes from the JPanelOne class. It inherits JPanel but in the constructor, you create a new JPanel and then you add a button to it. If you do this instead:

public class JPanelOne extends JPanel {

   public JPanelOne() {
       JButton button = new JButton("test");
       add(button);
   }
}

it should work as you expect it.

Don't call setSize() on your JFrame. Instead let the layouts set the proper sizes of their components and the GUI. After adding all components to your GUI, call pack() on the JFrame, then call setVisble(true). Note that most layouts respect a component's preferredSize more so than its size.

Also, your calling setVisible(true)on your individual components is unnecessary (unless you are changing their visibility after the GUI is up and running for some reason). You'll also want to read up more on using layout managers and will probably use FlowLayout less once you've studied them.

Edit
Regarding your recent Edit:

Maybe I am overthinking it, so in the end is it ok to have big GUI classes and to control visiblity of diffrent GUI areas by setting them visible or not?

I will answer that sometimes this is helpful, for instance if you want to change a standard calculator into a scientific calculator, sometimes it's good to simply show an already created JPanel filled with the advance calculation buttons by using setVisible(true). If on the other hand you want to swap "views" of your GUI to reflect a substantial change in its state, for instance a word processor changing from edit mode to print mode, you could swap JPanels easily to do this using a CardLayout.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!