我在jscrollpane中动态的添加了一组radiobutton,但添加后的东西超过jscrollpane的大小 范围,超出的部分就看不到了,也没有出现可以拖拽的滚动条
ButtonGroup buttongroup = new ButtonGroup();
JscrollPane jscrollpane = new JscrollPane();
while(int i=0;i<15:i++){
JRadioButton button = new JRadioButton();
button.setName(Integer.toString(i));
button.setText(Integer.toString(i));
button.setBounds(20,(20*(i+1)),60,30);
button.setVisible(true);
buttongroup.add(button);
jscrollpane.add(button);
}
jscrollpane.updateUI();
jscrollpane.validate();
ButtonGroup buttongroup = new ButtonGroup();
JscrollPane jscrollpane = new JscrollPane();
while(int i=0;i<15:i++){
JRadioButton button = new JRadioButton();
button.setName(Integer.toString(i));
button.setText(Integer.toString(i));
button.setBounds(20,(20*(i+1)),60,30);
button.setVisible(true);
buttongroup.add(button);
jscrollpane.add(button);
}
jscrollpane.updateUI();
jscrollpane.validate();
我现在先把一个pane加入到jscrollpane中,然后再把这些radiobutton放入到这个pane中,可以实现滚动条了,但是现在又有一个问题,我的这个pane不能根据我的radiobutton的数量来自动调节其大小 ,以使所有的radiobutton都能正常显示出来
那是用什么呢
setviewPort()吗,这个我试过也不行
完整的代码在另外一台电脑上面
JscrollPane和Jpanel都是用netbean自动生成的,所以一些初始化的东西我就不写了JscrollPane jscrollpane = new JscrollPane();
Jpanel panel = new Jpanel();
jscrollpane.setViewportView(panel);//这是动态生成并显示radiobutton的代码
ButtonGroup buttongroup = new ButtonGroup();
panel.removeall();
while(int i=0;i<15:i++){
JRadioButton button = new JRadioButton();
button.setName(Integer.toString(i));
button.setText(Integer.toString(i));
button.setBounds(20,(20*(i+1)),60,30);
button.setVisible(true);
buttongroup.add(button);
panel.add(button);
}
panel.updateUI();
panel.validate();
jscrollpane.updateUI();
jscrollpane.validate();
Box vbox = Box.createVerticalBox();
jscrollpane.setViewportView(vbox);//这是动态生成并显示radiobutton的代码
ButtonGroup buttongroup = new ButtonGroup();
vbox.removeall();
for(int i=0;i<15:i++){
JRadioButton button = new JRadioButton(Integer.toString(i));
buttongroup.add(button);
vbox.add(button);
}
vbox.revalidate();
jscrollpane.updateUI();
jscrollpane.validate();
JScrollPane jscrollpane = new JScrollPane(vbox);
ButtonGroup buttongroup = new ButtonGroup();//这是动态生成并显示radiobutton的代码
vbox.removeall();
vbox.revalidate();for(int i=0;i<15:i++){
JRadioButton button = new JRadioButton(Integer.toString(i));
buttongroup.add(button);
vbox.add(button);
vbox.revalidate();
}
其他都是多余的代码