java swing gridbaglayout中添加两个竖着的滚动条jscrollpanel 1C

图片说明
如图,我需要垂直添加两个JScrollPane。而他们俩分配的大小不一样,所以我用了GridBagLayout布局。但我在实际操作的时候发现,GridBagLayout只能设置相对的大小。而JScrollPane必须超过大小才显示滚动条,所以他们俩矛盾了。最后直接不显示了。求教
代码如图:
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.ScrollPane;
import java.awt.Toolkit;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

/**

  • @author JiaWenRuo
  • @date 2016-10-18 下午12:42:17
  • @version
  • @TODO
    */
    public class Test1 extends JFrame{

    /**

    • @param args
      */
      JScrollPane jScrollPane;
      JPanel jPanel;
      JLabel jLabel;
      JSplitPane jSplitPane;
      public Test1(){
      setSize(Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height/6);
      GridBagLayout gridBagLayout = new GridBagLayout();
      setLayout(gridBagLayout);
      GridBagConstraints c = new GridBagConstraints();
      jLabel = new JLabel("ss");//上边随便用标签代替
      c.weightx =100;
      c.weighty =100;
      c.gridwidth = GridBagConstraints.REMAINDER;
      c.gridheight = 1;
      c.gridx= 0;
      c.gridy = 0;
      gridBagLayout.setConstraints(jLabel, c);
      add(jLabel);

      jPanel = new JPanel(new GridLayout(10,1));
      for (int i = 0; i < 10; i++) {
      jPanel.add(new JButton("ss"));
      }
      //下边滚动显示10个按钮,出不来效果
      jScrollPane = new JScrollPane(jPanel,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
      c.weightx =100;
      c.weighty =0;
      c.gridwidth = GridBagConstraints.REMAINDER;
      c.gridheight = 1;
      c.gridx= 0;
      c.gridy = 1;
      gridBagLayout.setConstraints(jScrollPane, c);
      add(jScrollPane);

    }
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    new Test1().setVisible(true);
    }

}
现在直接不显示,是这样的图片说明

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问