1

我有这个面板:

public class MyPanel extends JPanel {

    public MyPanel() {
        super();
        this.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        JTable leftTable = new JTable();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0.1;
        gbc.weighty = 1.0;
        add(leftTable, gbc);

        JTextPane textPane = new JTextPane();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.weightx = 0.4;
        gbc.weighty = 1.0;
        add(textPane, gbc);

        JTable rightTable = new JTable();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.weightx = 0.5;
        gbc.weighty = 1.0;
        add(rightTable, gbc);
    }
}

然后,当我在 JTextPane 中输入一些字符时,它会自动调整大小。由于同一行上有 200 个字符(在文本窗格上),这 2 个 JTable 不再可见。

如何修复 JTextPane 的宽度?

谢谢。

4

2 回答 2

3

只需gbc.fill = GridBagConstraints.NONE;用于 JTextPane

于 2012-08-20T09:58:57.343 回答
1

最后我使用了JGoodiesFormLayout

private void initItemsPanel() {
    JPanel itemsPanel = new JPanel();
    itemsPanel.setLayout(new FormLayout("left:pref, fill:400px, pref:grow", "fill:20px, fill:4px, fill:max(20px;pref)"));
    CellConstraints cc = new CellConstraints();

    // the left table
    JTable leftTable = new JTable();
    itemsPanel.add(leftTable.getTableHeader(), cc.xywh(1, 1, 1, 2));
    itemsPanel.add(leftTable, cc.xywh(1, 3, 1, 1));

    // the text pane
    itemsPanel.add(new JTextPane(), cc.xywh(2, 2, 1, 2));

    // the right table
    JTable rightTable = new JTable();
    rightTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
    itemsPanel.add(rightTable.getTableHeader(), cc.xywh(3, 1, 1, 2));
    itemsPanel.add(rightTable, cc.xywh(3, 3, 1, 1));

    // Adding the items panel to dialog panel
    JScrollPane itemsScrollPane = new JScrollPane(itemsPanel);
    itemsScrollPane.getVerticalScrollBar().setUnitIncrement(3);

    dialogPanel.add(itemsScrollPane, BorderLayout.CENTER); // with dialogPanel previously instantiated
}

对于我测试的所有情况(调整大小、清空/填充组件等),它更容易预测。

请注意,您必须getTableHeader()显式调用,因为 JTable 不直接在 JScrollPane 中。

谢谢。

于 2012-09-05T15:39:25.757 回答