【问题标题】:Issue with JPanel as scrollableJPanel 可滚动的问题
【发布时间】: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();

}

【问题讨论】:

  • 1) 请对代码块使用一致且符合逻辑的缩进。 2) 为了尽快获得更好的帮助,请发布SSCCE。 3) checkboxes = new Checkbox[150]; 不要将 Swing 组件与 AWT 混合使用。
  • 也可以考虑JTable

标签: java swing applet jpanel jscrollpane


【解决方案1】:

如果您的示例可信,那么您就是在混合重量级和重量级的组件。我的基本建议是,不要。他们在一起玩得不好。

将您对 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

肖恩

【讨论】:

    猜你喜欢
    • 2010-11-26
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多