JScrollPane 阻止与内容的交互

JScrollPane prevents interaction with contents

我有一个 JScrollPane,它的客户端是 Container,布局为 BoxLayout

BoxLayout 内有多个(动态生成的)JPanels。但是,JScrollPane 不滚动(滚动条显示并调整大小,但您实际上不能移动它们),而且我也无法与 JScrollPane.[=21 的内容进行交互=]

代码如下:

public static void setupOrderTable(){
        orderTable = new Container(); 
        scroller = new JScrollPane(orderTable ,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scroller.setPreferredSize(new Dimension(810,500));
        orderTable.setLayout(new BoxLayout(orderTable,BoxLayout.Y_AXIS));
        if (orderTable!=null)
            mainContainer.remove(orderTable);
        for (Order o: OrderManager.getList()){
            orderTable.add(new ControlRowItem(o));
        }
        mainContainer.add(scroller,BorderLayout.CENTER);
        frame.pack();
    }

如果我只是替换行就可以正常工作

mainContainer.add(scroller,BorderLayout.CENTER);

mainContainer.add(orderTable,BorderLayout.CENTER);

但它显然不会滚动。否则,正如我所说,滚动条不起作用,我无法与 orderTable 中的任何 JPanel 交互。

不要将重量级 (Container) 组件与轻量级 (JScrollPane) 组件混合使用,它们混合得不好。将 orderTable 更改为 JPanel 而不是

虽然它应该已在 Java 6 中得到修复,但我已经看到太多奇怪的事情来尝试它,以至于它完全值得任何努力。简单规则,不要在 Swing 容器中混合重量级组件 (AWT)