【问题标题】:How can i display the JTable?如何显示 JTable?
【发布时间】:2020-07-02 07:54:12
【问题描述】:

我在JScrollPane 中创建了一个JTable,但它没有显示在程序中。

这是我的代码:

        scrollPaneTable = new JScrollPane();
        scrollPaneTable.setBounds(10, 507, 854, -480);
        
        table = new JTable();
        table.setForeground(Color.BLACK);
        table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        table.setCellSelectionEnabled(true);
        table.setFillsViewportHeight(true);
        table.setBackground(Color.LIGHT_GRAY);
        table.setBorder(new EtchedBorder(EtchedBorder.RAISED, Color.GRAY, null));
        table.setModel(new DefaultTableModel(
            new Object[][] {
                {"PC", "Computer", "Dominik", "Test", "Test"},
            },
            new String[] {
                "Name", "Beschreibung", "Nutzer", "Ort", "Seriennummer"
            }
        ));
        table.setColumnSelectionAllowed(true);
        table.setVisible(true);
        scrollPaneTable.setViewportView(table);
        jFW.getContentPane().add(scrollPaneTable);

【问题讨论】:

  • 当然你的 JTable 没有出现。没有 main 方法,也没有定义 JFrame。您的代码甚至无法编译。
  • 这里是完整代码:hastebin.com/aqafafuray.java
  • 1) “这里是完整代码:” 不要在外部站点发布代码链接。为了尽快获得更好的帮助,edit 添加minimal reproducible exampleShort, Self Contained, Correct Example。 2) Java GUI 必须在不同的语言环境中使用不同的 PLAF 在不同的操作系统、屏幕尺寸、屏幕分辨率等上工作。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them 以及 white space 的布局填充和边框。

标签: java swing jframe jtable


【解决方案1】:

你没有main方法,你在Gui类的Constructor中初始化UI和创建JTable等。

您需要在 main 中创建新的 Gui 对象以查看 Frame Table 等。(不一定在 Gui 类中)

【讨论】:

  • 我创建了一个主窗口,窗口正在显示。但不是表。每个按钮、标签等都在工作
猜你喜欢
  • 1970-01-01
  • 2017-04-06
  • 2015-10-16
  • 2016-09-24
  • 1970-01-01
  • 1970-01-01
  • 2012-01-05
  • 1970-01-01
相关资源
最近更新 更多