JScrollPane 中的 JTable 单元格大小和 table 大小

JTable inside JScrollPane cell size and table size

我有这段代码可以显示 JTable 中的一些数据:

public class ShowResults extends JFrame {

    public ShowResults(List<String> list) {

        setSize(1000, 1000);
        setLocationRelativeTo(null); //center
        setVisible(true);

        String[][] table_data = new String[pics.size()][table_header.length];

        for (int i = 0; i < pics.size(); i++) {   
            //Fill table with data
        }

        JTable table = new JTable(new DefaultTableModel(table_data, table_header)) {
            @Override
            public boolean isCellEditable(int row, int column) {
                //disable table editing
                return false;
            }
        };
        JScrollPane scroll_pane = new JScrollPane(table);

        table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        table.getTableHeader().setReorderingAllowed(false);
        table.getTableHeader().setResizingAllowed(false);

        this.setLayout(new FlowLayout());
        this.add(scroll_pane);
    }
}

但是 table 看起来不像我预期的那样。我希望滚动条只在垂直方向(现在是水平方向)并且 table 的 header 显示整个 header 名称(现在 table 由于某种我不知道的原因被包裹了). 我该怎么办?

I would that scroll bar in only vertical (now is horizontal) and header of table show entire header names (now table is wrapped for some reason i don't know).

原因是您设置了新的 FlowLayout as the content pane's layout manager. This layout manager honors the components preferred size and therefore prevents the scroll pane resizes as you wish. I'd leave the default layout manager, which is BorderLayout,并将滚动窗格添加到 CENTER 位置。

另一方面,您应该在将所有组件添加到其内容窗格后使框架可见。

我会使用网格布局。也尝试暂时删除 scroll_pane 并查看是否有所不同。我 运行 遇到了自己使用 JScrollPane 的问题。