如何在 JFrame 上设置 JTable?

问题描述

这是我的 Java 代码。在此代码中,我试图在 JTable添加 JFrame,但表格未在框架上显示我的代码中的问题是什么?

    table_1 = new JTable();
    table_1.setFont(new Font("Tahoma",Font.PLAIN,14));
    table_1.setBorder(new LineBorder(new Color(0,0)));
    table_1.setModel(new DefaultTableModel(
        new Object[][] {
            },new String[] {
            "Last Name","First Name"
            }
        ) {
            Class[] columnTypes = new Class[] {
                String.class,String.class
            };
            public Class getColumnClass(int columnIndex) {
                return columnTypes[columnIndex];
            }
        });
        table_1.setBounds(20,314,1254,-74);
        contentPane.add(table_1);
    

解决方法

很难尝试代码,非常不完整,但我猜
table_1.setBounds(20,314,1254,-74);
负高度很难显示。

最好不要使用null布局管理器1...
查看 Oracle 的 Laying Out Components Within a Container 教程。

通常将 JTable 添加到 JScrollPane

1 - 由于缺少相应的代码而使用 setBounds

的假设