0

我有一个显示良好的 JTable。如果我将表格放在 JScrollPane 中,它将不再显示。为什么不?我想在自己的 JScrollPane 中添加两个表。这是我的代码,我只是尝试将 JScrollPane 添加到第一个表中:

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 1000, 800);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JProgressBar progressBar = new JProgressBar();
    progressBar.setBounds(50, 68, 700, 14);
    frame.getContentPane().add(progressBar);

    JTextPane txtpnAutomotiveHmi = new JTextPane();
    txtpnAutomotiveHmi.setText("Automotive HMI");
    txtpnAutomotiveHmi.setBounds(362, 21, 205, 20);
    frame.getContentPane().add(txtpnAutomotiveHmi);


    testcase_table = new JTable();
    testcase_table.setBounds(50, 125, 350, 426);
    JScrollPane scroll_testcase = new JScrollPane(testcase_table);
    frame.getContentPane().add(scroll_testcase);

    teststep_table = new JTable();
    teststep_table.setBounds(399, 125, 350, 426);
    frame.getContentPane().add(teststep_table);
}

谢谢。

4

2 回答 2

1

不要设置布局。它不是“默认布局”,也不是“最简单的实现”。实际上很难做到正确。

JFrame默认使用BorderLayout。添加JProgressBarwith BorderLayout.NORTH, JTextPanewithBorderLayout.SOUTHJScrollPanewith BorderLayout.CENTER。删除所有setBounds. 组件将出现。如果您需要以某种复杂的方式定位它们,请阅读GridBagLayout.

于 2013-06-05T10:29:33.080 回答
0

定义表格的大小并将滚动窗格设置为相同的大小。

scrollPane.setSize(table.getSize());
于 2013-06-05T09:06:42.460 回答