JScrollPane 在空布局中不起作用

JScrollPane not working in null layout

  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 代码中使用滚动窗格,但它不起作用。 Scroll Pane在垂直方向上添加到框架上但不起作用。

您应该创建自己的扩展 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 已经为他设置了 desired/correct 答案,但该解决方案不适用于动态内容(在我的情况下垂直于 Y 轴)所以我 "repaired" 或更新 - 如果你愿意- 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);