【发布时间】:2012-04-16 11:25:10
【问题描述】:
我正在开发一个 java JApplet 以在可滚动窗格中显示几十个复选框。我将这些复选框包含在 JPanel 中,并将此面板添加到 JScrollPane 上,该面板已添加到 applet 的当前 ContentPane 中。内容窗格也很少有其他组件,如 JTextArea、Button 和 Label。我会看到滚动条,但是当我滚动时,复选框会滚动到滚动窗格之外并覆盖在其他相邻组件上。我尝试了 setPreferredSize() 没有成功。滚动可能是什么问题?
我的代码片段看起来像:
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();
}
【问题讨论】:
标签: java swing applet jpanel jscrollpane