0

我有下面的代码片段,其中 TestClass 正在扩展使用 SpringLayout 的 jPanel,我无法在其中添加垂直滚动条。

你们能帮帮我吗?



        public class TestClass extends JPanel {

            private SpringLayout layout;
            private Spring s, sprWest;
            private JComboBox comboDevice = new JComboBox();
            private JComboBox comboCommand = new JComboBox();
            private JLabel lblDevice = new JLabel("Select the Device:");
            private JLabel lblCommand = new JLabel("Select Command:");
            private JButton btnCommand = new JButton("Save");

            public TestClass () {
                layout = new SpringLayout();
                s = Spring.constant(0, 60, 60);
                setLayout(layout);
            }

            public void populateFields(){
                add(lblCommand);
                add(comboCommand);


                sprLblEast = Spring.sum(s, Spring.max(layout.getConstraints(lblCommand).getWidth(), layout.getConstraints(lblDevice).getWidth()));
                Spring strut = Spring.constant(10);

                layout.putConstraint(SpringLayout.NORTH, lblCommand, strut, SpringLayout.SOUTH, comboDevice);
                layout.putConstraint(SpringLayout.NORTH, comboCommand, strut, SpringLayout.SOUTH, comboDevice);
                layout.putConstraint(SpringLayout.EAST, lblCommand, sprLblEast, SpringLayout.WEST, this);
                layout.putConstraint(SpringLayout.WEST, comboCommand, Spring.sum(s, layout.getConstraints(lblCommand).getWidth()), SpringLayout.WEST, lblCommand);
                layout.putConstraint(SpringLayout.EAST, this, sprLblEast, SpringLayout.WEST, comboCommand);

                List cmdList = getCommandList();
                for (int index = 0; index < cmdList.size(); index++) {
                    comboCommand.addItem(cmdName);
                }
                validate();
                repaint();
            }
    }


4

1 回答 1

0

我建议使用ScrollPane

ScrollPane mainWindow = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED); mainWindow.add(this);

但是你必须使用另一个对象而不是你TestClass的视图,这里是mainWindow.

您也可以使用 Swing中的 JScrollPane:JScrollPane 教程

于 2012-06-19T12:55:31.060 回答