Java 我能';我不能让我的孩子们展示任何东西

Java 我能';我不能让我的孩子们展示任何东西,java,swing,jtable,jpanel,jscrollpane,Java,Swing,Jtable,Jpanel,Jscrollpane,我不能让我的gui显示Jtable,为什么我不知道,我没有得到任何错误,当我在屏幕上打印一些东西时,我得到9列。所以我得到了数据。但我做错了什么我不知道 我的GUIOdreHandler看起来像这样 public GUIOrdreHandler(){ KaldSQL ks = new KaldSQL(); ResultSet rs; } public static DefaultTableModel build

我不能让我的gui显示Jtable,为什么我不知道,我没有得到任何错误,当我在屏幕上打印一些东西时,我得到9列。所以我得到了数据。但我做错了什么我不知道

我的GUIOdreHandler看起来像这样

public GUIOrdreHandler(){

            KaldSQL ks = new KaldSQL();
            ResultSet rs;

        }

        public static DefaultTableModel buildTableModel(ResultSet rs)
                throws SQLException {

            java.sql.ResultSetMetaData metaData = rs.getMetaData();

            // names of columns
            Vector<String> columnNames = new Vector<String>();
            int columnCount = metaData.getColumnCount();
            for (int column = 1; column <= columnCount; column++) {
                columnNames.add(metaData.getColumnName(column));
                System.out.println(columnCount);
            }

            // data of the table
            Vector<Vector<Object>> data = new Vector<Vector<Object>>();
            while (rs.next()) {
                Vector<Object> vector = new Vector<Object>();
                for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
                    vector.add(rs.getObject(columnIndex));
                }
                data.add(vector);
            }

            return new DefaultTableModel(data, columnNames);

            }
public GUIHentOrdre(){

        try {
            con = ks.connectNow();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        JPanel info = new JPanel();
        info.setLayout(new BorderLayout());
        button = new JButton("button");
        info.add(button, BorderLayout.CENTER);
        add(button);
        ResultSet rs = ks.Hentalleordreliste(con);
        GUIOrdreHandler gh = new GUIOrdreHandler();

        try {
            table = new JTable(gh.buildTableModel(rs));

            System.out.println(table);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        info.add(table, BorderLayout.CENTER);
        add(table);
    }
}
我已经尝试过谷歌的任何东西,书北的作品,所以请帮助我 :D


只是关于代码中的错误

JPanel info = new JPanel();
info.setLayout(new BorderLayout());
button = new JButton("button");
info.add(button, BorderLayout.CENTER);
add(button);
  • 删除关于
    add的代码行(按钮)到(不完全是关于代码的)

  • 更改
    info.add(按钮,BorderLayout.CENTER)
    北部
    南部

  • 您没有将
    JTable
    (在
    JScrollPane
    中)正确添加到
    JPanel

伪码

JPanel info = new JPanel();
info.setLayout(new BorderLayout());
button = new JButton("button");
info.add(button, BorderLayout.SOUTH);
JTable table = new JTable (ClassOrVoidOrModelNameReturnsTableModel)
JScrollPane scroll = new JScrollPane(table)
info.add(scroll, BorderLayout.CENTER);
  • 但这并不能解决上面的问题,因为您的问题应该是来自
    JDBC

  • 不要在
    try
    -
    catch
    块中创建
    JComponent
    s,在之前准备这个
    对象
    ,最好是作为
    局部变量

  • 不要在
    中为
    JComponent
    s创建
    XxxModel
    try
    -
    catch
    块,在之前准备这个
    对象
    ,最好是作为
    局部变量

  • 初始化
    XxxModel
    及其
    JComponent
    ,然后将数据从
    JDBC
    加载到
    XxxModel

  • rs.close()
    添加到
    finally
    块(
    try
    -
    catch
    -
    finally

  • 不要重新发明轮子,使用它


仅关于代码中的错误

JPanel info = new JPanel();
info.setLayout(new BorderLayout());
button = new JButton("button");
info.add(button, BorderLayout.CENTER);
add(button);
  • 删除关于
    add的代码行(按钮)到(不完全是关于代码的)

  • 更改
    info.add(按钮,BorderLayout.CENTER)
    北部
    南部

  • 您没有将
    JTable
    (在
    JScrollPane
    中)正确添加到
    JPanel

伪码

JPanel info = new JPanel();
info.setLayout(new BorderLayout());
button = new JButton("button");
info.add(button, BorderLayout.SOUTH);
JTable table = new JTable (ClassOrVoidOrModelNameReturnsTableModel)
JScrollPane scroll = new JScrollPane(table)
info.add(scroll, BorderLayout.CENTER);
  • 但这并不能解决上面的问题,因为您的问题应该是来自
    JDBC

  • 不要在
    try
    -
    catch
    块中创建
    JComponent
    s,在之前准备这个
    对象
    ,最好是作为
    局部变量

  • 不要在
    中为
    JComponent
    s创建
    XxxModel
    try
    -
    catch
    块,在之前准备这个
    对象
    ,最好是作为
    局部变量

  • 初始化
    XxxModel
    及其
    JComponent
    ,然后将数据从
    JDBC
    加载到
    XxxModel

  • rs.close()
    添加到
    finally
    块(
    try
    -
    catch
    -
    finally

  • 不要重新发明轮子,使用它


但我可以看到我的按钮。调用GUIHentOrdre().info.setViewportView(表)时;没有工作,但我可以看到我的按钮。调用GUIHentOrdre().info.setViewportView(表)时;不起作用