【问题标题】:How to set JTable on JFrame?如何在 JFrame 上设置 JTable?
【发布时间】:2021-07-01 05:07:44
【问题描述】:

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

    table_1 = new JTable();
    table_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
    table_1.setBorder(new LineBorder(new Color(0, 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);
    

【问题讨论】:

  • 1) 为了尽快获得更好的帮助,edit 添加minimal reproducible exampleShort, Self Contained, Correct Example。表的硬编码数据。 2) Java GUI 必须在不同的语言环境中使用不同的 PLAF 在不同的操作系统、屏幕尺寸、屏幕分辨率等上工作。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them 以及 white space 的布局填充和边框。
  • 不要将 JTable 直接放在 JFrame 上。您将 JTable 放置在 JScrollPane 中。您将 JScrollPane 放置在 JPanel 中。您将 JPanel 放置在 JFrame 中。 Oracle 教程Creating a GUI With JFC/Swing 将向您展示创建 Swing GUI 的所有步骤。跳过 Netbeans 部分。

标签: java swing jframe jtable


【解决方案1】:

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

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

也通常将JTable 添加到JScrollPane

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

的假设

【讨论】:

  • 谢谢。现在我必须更改负值。然后表格可见,但列不可见。
  • 您需要将表格添加到 JScrollPane 中,并将 JScrollpane 添加到 contentPane 中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-20
  • 2014-04-11
  • 1970-01-01
  • 2018-12-23
  • 1970-01-01
  • 2011-03-20
  • 2012-10-29
相关资源
最近更新 更多