Java 将JPanel设置为可滚动的问题

Java 将JPanel设置为可滚动的问题,java,swing,applet,jpanel,jscrollpane,Java,Swing,Applet,Jpanel,Jscrollpane,我正在开发一个JavaJapplet,以便在可滚动的窗格中显示几个10秒的复选框。我在JPanel中包含了这些复选框,并在JScrollPane上添加了这个面板,该面板是在小程序的当前内容窗格中添加的。内容窗格也有一些其他组件,如JTextArea、按钮和标签。我会看到滚动条,但当我滚动时,复选框会滚动到scrollpane之外,并覆盖在其他相邻组件上。我尝试了setPreferredSize(),但没有成功。滚动会有什么问题 我的代码片段如下所示: public void init(){ co

我正在开发一个JavaJapplet,以便在可滚动的窗格中显示几个10秒的复选框。我在JPanel中包含了这些复选框,并在JScrollPane上添加了这个面板,该面板是在小程序的当前内容窗格中添加的。内容窗格也有一些其他组件,如JTextArea、按钮和标签。我会看到滚动条,但当我滚动时,复选框会滚动到scrollpane之外,并覆盖在其他相邻组件上。我尝试了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();

}
public void init(){
contentPane=this.getContentPane();
GridBagLayout grrdbag=新的GridBagLayout();
GridBagConstraints components=新的GridBagConstraints();
contentPane.setLayout(gridbag);
//按钮、文本区域和标签组件位于此处
//这里的复选框
components=新的GridBagConstraints();
components.anchor=GridBagConstraints.EAST;
添加(新标签(“数据:”,标签.右),组件);
components=新的GridBagConstraints();
components.gridwidth=GridBagConstraints.Requirement;
分量权重y=1;
components.fill=GridBagConstraints.BOTH;
checkboxesPanel.setLayout(新的BoxLayout(checkboxesPanel,BoxLayout.Y_轴));
conflictScrollPane=新的JScrollPane(选中BoxesPanel,JScrollPane.需要时垂直滚动条,需要时水平滚动条);
添加(冲突滚动窗格,组件);
}
//创建复选框
公共void显示框(){
复选框=新复选框[150];

对于(int j=0;j,如果你的例子可信,你是在混合重分量和轻分量。我的基本建议是,不要。它们不能很好地结合在一起

将对复选框的引用改为JCheckBox

查看更多信息

< P >尚恩·斯蒂芬·菲南< /P > 1)请使用代码块的一致和逻辑缩进。2)更快的帮助,后3。<代码>复选框=新的复选框[150 ];< /C> >不要将摆动组件与AWT混合。也可以考虑。