1
我正在开发一个Java JApplet在可滚动窗格中显示几十个复选框。我将这些复选框包含在JPanel中,并将此面板添加到JScrollPane中,该JScrollPane添加到当前Applet的ContentPane中。内容窗格还有其他几个组件,如JTextArea,Button和Label。我会看到滚动条,但当我滚动时,复选框将滚动到滚动窗格之外,并放置在其他相邻组件上。我尝试setPreferredSize()没有成功。滚动有什么问题?JPanel作为可滚动问题
我的代码叮咬的样子:
public void init(){
contentPane = this.getContentPane();
GridBagLayout grrdbag = new GridBagLayout();
GridBagConstraints components = new GridBagConstraints();
contentPane.setLayout(gridbag);
//button, textarea and label components here
//checkboxes here
components = new GridBagConstraints();
components.anchor = GridBagConstraints.EAST;
contentPane.add(new Label("Data:", Label.RIGHT), components);
components = new GridBagConstraints();
components.gridwidth = GridBagConstraints.REMAINDER;
components.weighty = 1;
components.fill = GridBagConstraints.BOTH;
checkboxesPanel.setLayout(new BoxLayout(checkboxesPanel, BoxLayout.Y_AXIS));
conflictScrollPane = new JScrollPane(checkboxesPanel,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
contentPane.add(conflictScrollPane, components);
}
//create check boxes
public void displayboxes(){
checkboxes = new Checkbox[150];
for(int j=0;j<150;j++){
checkboxes[j] = new Checkbox("This is test data for check box here.",null,false);
checkboxesPanel.add(checkboxes[j]);
checkboxesPanel.revalidate();
}
repaint();
validate();
}
//start method
public void start() {
displayboxes();
repaint();
validate();
}
1)请使用代码块一致性和逻辑缩进。 2)为了更快得到更好的帮助,请发布[SSCCE](http://sscce.org/)。 3)'checkboxes = new Checkbox [150];'不要将Swing组件与AWT混合使用。 – 2012-04-02 19:36:22
另请考虑['JTable'](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html)。 – trashgod 2012-04-02 21:42:00