Java 将多个小面板添加到框架

Java 将多个小面板添加到框架,java,swing,jpanel,layout-manager,null-layout-manager,Java,Swing,Jpanel,Layout Manager,Null Layout Manager,我试图用java制作一个简单的界面,但在一个框架中添加多个面板时遇到了一个问题。我希望它是一个cafe软件,这样会有多个表。这是我的代码 public class CafeView extends JFrame{ private JButton takeMoneyBtn = new JButton("Hesabı Al"); public CafeView(){ JPanel table1 = new JPanel(); this.setSize(800,600);

我试图用java制作一个简单的界面,但在一个框架中添加多个面板时遇到了一个问题。我希望它是一个cafe软件,这样会有多个表。这是我的代码

public class CafeView extends JFrame{

private JButton takeMoneyBtn = new JButton("Hesabı Al");

public CafeView(){
    JPanel table1 = new JPanel();
    this.setSize(800,600);
    this.setLocation(600, 200);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);

    table1.setBounds(100, 100, 150, 150);
    table1.setLayout(null);
    this.add(table1);
    table1.add(takeMoneyBtn);

}
}

当我运行它时,我只看到一个空的框架。在这段代码中,我只是尝试添加一个简单的面板,如果我可以这样做,我想我也可以添加其他面板。那么我如何解决这个问题,并将小而多的面板添加到一个框架中,我缺少了什么?感谢您的帮助。(没有主方法,因为我从另一个类调用此接口类。)

我将使用LayoutManager放置所有控件并直接访问内容窗格


您正在使用
.setBounds(…,…)
安排组件的位置,在本例中是
JPanel
,您应该将其用于顶级容器(
JFrame
JWindow
JDialog
JApplet
),而不是
JPanel
。 因此,请删除:

table1.setBounds(100, 100, 150, 150);
我们由
LayoutManager
提供来安排组件,请参见


也许你的相框根本不是空的。尝试使用setBorder()为面板添加边框,这样您就可以真正看到JPanel。此外,在将所有可视组件放在一起之前调用setVisible(true)也是错误的Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作。因此,它们不利于像素完美布局。而是使用布局管理器,或与布局填充和边框一起使用。+1。进一步说明。应在EDT上创建和更新Swing GUI。有关更多详细信息,请参阅。
table1.setBounds(100, 100, 150, 150);
   public class CafeView extends JFrame{

private JButton takeMoneyBtn = new JButton("Hesabı Al");

public CafeView(){
    JPanel table1 = new JPanel();
    this.setSize(800,600);
    this.setLocation(600, 200);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);

   //table1.setBounds(100, 100, 150, 150);
    //table1.setLayout(null);
    this.add(table1,BorderLayout.CENTER);
    table1.add(takeMoneyBtn);

}