JPanel的问题是可滚动的

时间:2012-04-02 19:28:29

标签: java swing applet jpanel jscrollpane

我正在开发一个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();

}

1 个答案:

答案 0 :(得分:0)

如果你相信你的例子,那么你正在混合重量和重量轻的组件。我的基本建议是,不要。他们不能很好地在一起玩。

改为将您对Checkbox的引用更改为JCheckBox。

查看更多信息

http://java.sun.com/products/jfc/tsc/articles/mixing/

http://java-antony.blogspot.com.au/2007/07/swing-vs-awt-or-lightweight-vs.html

沙恩

相关问题