import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class ScrollJPanelDemo extends JFrame {
public ScrollJPanelDemo(){
setSize(480, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Select one or more options : ");
JCheckBox jcb1 = new JCheckBox("Chandigarh");
JCheckBox jcb2 = new JCheckBox("Mohali");
JCheckBox jcb3 = new JCheckBox("Delhi");
JCheckBox jcb4 = new JCheckBox("Noida");
JCheckBox jcb5 = new JCheckBox("Mumbai");
JCheckBox jcb6 = new JCheckBox("Kolkata");
//creating JPanel to hold the checkboxes
JPanel jpnl = new JPanel();
jpnl.setLayout(null);
jpnl.setOpaque(true);
jcb1.setBounds(0,0,100,40);
jcb2.setBounds(0,60,100,40);
jcb3.setBounds(0,120,100,40);
jcb4.setBounds(0,180,100,40);
jcb5.setBounds(0,240,100,40);
jcb6.setBounds(0,300,100,40);
//adding check boxes and label to the JPanel
jpnl.add(label);
jpnl.add(jcb1);
jpnl.add(jcb2);
jpnl.add(jcb3);
jpnl.add(jcb4);
jpnl.add(jcb5);
jpnl.add(jcb6);
//creating the scroll pane that will scroll the panel.
JScrollPane jscrlPane = new JScrollPane(jpnl);
jscrlPane.setBounds(0,0,300,300);
jscrlPane.setHorizontalScrollBarPolicy
(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS) ;
jscrlPane.setVerticalScrollBarPolicy
(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//adding that scroll pane to the frame.
getContentPane().add(jscrlPane);
setVisible(true);
}
public static void main(String args[]){
new ScrollJPanelDemo();
}
}
我是 Java Swing 的新手,并尝试在我的 Java 代码上使用滚动窗格,但它不起作用。滚动窗格以垂直方向添加到框架上,但不起作用。
您应该创建自己的面板,该面板扩展JPanel
包含所有复选框,并在此面板中覆盖getPreferredSize()
方法,例如:
@Override
public Dimension getPreferredSize()
{
return new Dimension( 300,300 );
}
并在代码中使用它:
...
// creating the scroll pane that will scroll the panel.
JScrollPane jscrlPane = new JScrollPane( new MyPanelWithCheckboxes() );
jscrlPane.setBounds( 0, 0, 300, 300 );
...
我看到 OP 已经为他设置了所需/正确的答案,但该解决方案不适用于动态内容(在我的情况下在 Y 轴上垂直(,所以我"修复"或更新 - 如果你愿意 - Mateusz 的原始答案,所以现在它实际上甚至可以处理动态 JPanel 内容(就像当你在运行时添加其他组件时,我就是这种情况(。
这是我的代码(有效,我自己使用它(:
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JPanel;
public class JPanelForNullLayoutScroll extends JPanel {
int h;
@Override
public Dimension getPreferredSize() {
if (getComponentCount() > 0) {
h = 0;
for (Component c : getComponents()) {
h += c.getHeight();
}
} else {
h = 0;
}
// as I do not need width recount
//I set it to be taken directly from the component itself
Dimension d = new Dimension(getWidth(), h);
return d;
}
}
然后,您可以通过实现它在代码中使用它,如下所示:
int tableW = 300;
int tableH = 300;
// JPANEL WITH NULL LAYOUT
JPanelForNullLayoutScroll container = new JPanelForNullLayoutScroll();
container.setLayout(null);
container.setVisible(true);
container.setEnabled(true);
container.setBounds(0, 0, tableW, tableH);
// SCROLLER
JScrollPane scrollPane = new JScrollPane(container);
scrollPane.setAlignmentX(JLabel.LEFT_ALIGNMENT);
scrollPane.getVerticalScrollBar().setUnitIncrement(8);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(0, 0, tableW, tableH);