Adding JPanel from another class to JPanel in JFrame

后端 未结 4 1704
执念已碎
执念已碎 2020-12-31 16:52

I can\'t get my JFrame from main class to display JPanel from another class. Everything compiles without errors.

JFrameTest.java:

pa         


        
4条回答
  •  春和景丽
    2020-12-31 17:23

    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.

提交回复
热议问题