具有固定大小的 JScrollPane 中的 JTree

JTree in a JScrollPane with fixed size

我在 JPanel 中的 JScrollPane 中有一个 JTree。

我遇到的问题是 width,当我用节点或长名称的节点填充 JTree 时,它​​没有修复。

举个例子:

如你所见,左边的比右边的长。

我的目标是让它们的大小完全相等,将主要 window 各占 50%。

这里是用来生成window的代码。

有没有办法让JScrollPane的宽度保持固定? 谢谢

public void initialize() {

    this.frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    JMenuBar menuBar = new JMenuBar();
    frame.setJMenuBar(menuBar);

    JMenu mnNewMenu = new JMenu("Services");
    mnNewMenu.setHorizontalAlignment(SwingConstants.CENTER);
    menuBar.add(mnNewMenu);

    frame.getContentPane().setLayout(new MigLayout("", "[grow][grow]", "[grow][grow]"));

    JPanel left_JPanel = new JPanel();
    frame.getContentPane().add(left_JPanel, "cell 0 0,grow");
    left_JPanel.setLayout(new MigLayout("", "[grow]", "[grow]"));

    left_ScrollPane = new JScrollPane();
    left_JPanel.add(left_ScrollPane, "cell 0 0,grow");

    JLabel left_Label = new JLabel("Left Scroll Pane");
    left_Label.setFont(new Font("Tahoma", Font.BOLD, 12));
    left_Label.setForeground(Color.BLUE);
    left_Label.setHorizontalAlignment(SwingConstants.CENTER);
    left_ScrollPane.setColumnHeaderView(left_Label);

    JTree left_tree = new JTree();
    left_ScrollPane.setViewportView(left_tree);

    JPanel right_JPanel = new JPanel();
    frame.getContentPane().add(right_JPanel, "cell 1 0,grow");
    right_JPanel.setLayout(new MigLayout("", "[grow]", "[grow]"));

    JScrollPane right_ScrollPane = new JScrollPane();
    right_JPanel.add(right_ScrollPane, "cell 0 0,grow");

    right_JTree = new JTree(phModel);
    right_JTree.setVisibleRowCount(8);
    right_ScrollPane.setViewportView(right_JTree);

    JLabel right_Label = new JLabel("Right Scroll Pane");
    right_Label.setFont(new Font("Tahoma", Font.BOLD, 12));
    right_Label.setForeground(Color.BLUE);
    right_Label.setHorizontalAlignment(SwingConstants.CENTER);
    right_ScrollPane.setColumnHeaderView(right_Label);
}

My goal is to keep them exactly equal in size, splitting the main window at 50% each.

使用来自 JDK 的带有标准布局管理器的嵌套面板。

GridLayout 使组件大小相同。

类似于:

JPanel left = new JPanel( new BorderLayout() );
left.add(leftLabel, BorderLayout.PAGE_START);
left.add(listScrollPane, BorderLayout.CENTER);

JPanel right = ...

JPanel main = new JPanel( new GridLayout(0, 2) );
main.add( left );
main.add( right );

frame.add( main );