首页 > 解决方案 > 无法同时显示我的 JFrame 和表格

问题描述

因此,我在 intellij 上创建了一个 gui 格式,当我取出与表格有关的代码时,它完全符合我的要求,但是当我将代码放入时,我得到的只是表格本身。我在使用 gui 表单本身添加列时遇到问题,并找到了一个关于如何使用代码添加列等的教程。我如何能够在表格中添加行和/或在表格中添加表格

public class App extends JFrame{
    private JButton createRecordButton;
    private JPanel panel1;
    private JPanel east;
    private JPanel west;
    private JPanel south;
    private JButton deleteRecordButton;
    private JButton viewStatisticsButton;
    private JButton downloadDataButton;
    public JTable TestDB;



    public App() {

    }

    public static void main(String[] args) {
        JFrame frame=new JFrame("App");
        frame.setContentPane(new App().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

        table gui=new table();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(600,200);
        gui.setVisible(true);
        gui.setTitle("The DataBase Table");




    }
}

标签: javaformsintellij-ideajframe

解决方案


您正在在线生成异常: frame.setContentPane(new App().panel1); 请注意,您创建了 App 对象,但 panel1 对象仍然为空。使用此代码,您将看到两个 JFrame 都会出现:

public class App extends JFrame{
private JButton createRecordButton;
private JPanel panel1;
private JPanel east;
private JPanel west;
private JPanel south;
private JButton deleteRecordButton;
private JButton viewStatisticsButton;
private JButton downloadDataButton;
public JTable TestDB;



public App() {
    panel1 = new JPanel();
}

public static void main(String[] args) {
    JFrame frame=new JFrame("App");
    frame.setContentPane(new App().panel1);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);

    JFrame gui=new JFrame();
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gui.setSize(600,200);
    gui.setVisible(true);
    gui.setTitle("The DataBase Table");




}

}


推荐阅读