2012-06-19 46 views
0

我有下面的代碼片段,其中TestClass正在擴展jPanel使用SpringLayout,其中我無法添加垂直滾動條。如何將垂直滾動條添加到Java中具有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(); 
      } 
    } 

 

回答

0

我會sugest使用ScrollPane

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

,但你將不得不使用另一個對象比你TestClass的看法,這裏將是mainWindow

您也可以使用Swing的JScrollPane:tutorial for JScrollPane

相關問題