无法使用 JScrollPane 滚动包含其他几个面板的 JPanel

Not able to scroll my JPanel which contains several other panels using JScrollPane

我有一个主要的 JPanel,其中我使用了其他几个面板。我想在我的面板中添加一个滚动窗格,以便当子面板超出框架时它们应该可以滚动,但我无法实现。

如何实现面板内面板的正确滚动功能?

我的子面板中的代码:

public class Page13111SubPanel extends JPanel {

    /**
     * Create the panel.
     */
    public Page13111SubPanel() {
//      setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
        setSize(1000,130);
        setLayout(new GridLayout(3, 3, 40, 35));

        JLabel label= new JLabel();
        label.setText("Vehicle Companies:asdddddddddddddddd1");
        label.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
        add(label);

        JLabel label1= new JLabel();
        label1.setText("Vehicle Companies:asdddddddddddddddd2");
        label1.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
        add(label1);

        JLabel label2= new JLabel();
        label2.setText("Vehicle Companies:asdddddddddddddddd3");
        label2.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
        add(label2);

        JLabel label3= new JLabel();
        label3.setText("Vehicle Companies:asdddddddddddddddd4");
        label3.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
        add(label3);

//      JLabel label3= new JLabel();
//      label3.setText("Vehicle Companies:asdddddddddddddddd4");
//      label3.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
//      add(label3);

        JLabel label4= new JLabel();
        label4.setText("Vehicle Companies:asdddddddddddddddd5");
        label4.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
        add(label4);

        JLabel label5= new JLabel();
        label5.setText("Vehicle Companies:asdddddddddddddddd6");
        label5.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
        add(label5);

        JLabel label6= new JLabel();
        label6.setText("Vehicle Companies:asdddddddddddddddd7");
        label6.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
        add(label6);
    }

}

我的主面板中的代码:

public class Page1311Test extends JPanel {
//  private JTable table;
    public JScrollPane pane=null;
    public JPanel panel=null;
    public JButton back=null;
    /**
     * Create the panel.
     */
    public Page1311Test() {

        JPanel panel=new JPanel();
        panel.setLayout(new GridLayout(7,1,1,40));
        Page13111SubPanel[] panel1=new Page13111SubPanel[7];
            panel1[0]=new Page13111SubPanel();
            panel.add(panel1[0]);
            panel1[1]=new Page13111SubPanel();
            panel.add(panel1[1]);
            panel1[2]=new Page13111SubPanel();
            panel.add(panel1[2]);
            panel1[3]=new Page13111SubPanel();
            panel.add(panel1[3]);
            panel1[4]=new Page13111SubPanel();
            panel.add(panel1[4]);
            panel1[5]=new Page13111SubPanel();
            panel.add(panel1[5]);
            panel1[6]=new Page13111SubPanel();
            panel.add(panel1[6]);
        pane=new JScrollPane(panel,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        add(pane);

    }
public static void main(String[] args) {
    JFrame frame=new JFrame();
    frame.setExtendedState(frame.MAXIMIZED_BOTH);
    frame.setLocation(0, 0);
    frame.add(new Page1311Test());
    frame.setVisible(true);
}
}

我认为您的问题是主面板不可滚动,因此您根本无法滚动。

尝试在那里使用 JScrollPane 并查看 this tutorial 以获取有关 JScrollPanes 的更多信息。还有很多关于框架和面板等其他 GUI 对象的信息,因此我认为您可以找到解决问题的方法!

我猜问题出在您的 GridLayoutsetSize 调用上。当您调用时,基本上您的布局管理器将覆盖您的 setSize 调用。

在下面的示例中,我设置了首选尺寸和最小尺寸,滚动窗格工作正常。

    JFrame frame = new JFrame("Grids of Grids");

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(3, 3));
    for(int i = 0; i<9; i++){
        JPanel localGrid = new JPanel();
        localGrid.setMinimumSize(new Dimension(250, 250));
        localGrid.setPreferredSize(new Dimension(250, 250));

        localGrid.setLayout(new GridLayout(5, 5));
        for(int j = 0; j<25; j++){
            localGrid.add(new JLabel("" + i + ":: " + j));
        }
        panel.add(localGrid);
    }
    JScrollPane scroll = new JScrollPane(panel);
    frame.add(scroll);

    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

否则,您的可滚动区域实际上会非常小,因为布局会尝试将其放入可用区域 space。

将主面板从 FlowLayout(默认)更改为使用面板的子组件填充可用 space(此处使用 GridLayout)的布局解决了问题。例如。

import java.awt.*;
import javax.swing.*;

public class Page1311Test extends JPanel {

    public JScrollPane pane = null;
    public JPanel panel = null;

    /**
     * Create the panel.
     */
    public Page1311Test() {
        setLayout(new GridLayout());
        //JPanel panel = new JPanel(); //shadowed class attribute
        panel = new JPanel(new GridLayout(0, 1, 1, 40));

        for (int ii = 0; ii < 17; ii++) {
            panel.add(new Page13111SubPanel(ii));
        }

        pane = new JScrollPane(panel,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        add(pane);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setExtendedState(frame.MAXIMIZED_BOTH);
        frame.setLocation(0, 0);
        frame.add(new Page1311Test());
        frame.pack();
        frame.setVisible(true);
    }
}

class Page13111SubPanel extends JPanel {

    /**
     * Create the panel.
     */
    public Page13111SubPanel(int num) {
        //setSize(1000, 130); // Don't set sizes using 'magic numbers'
        setLayout(new GridLayout(3, 3, 40, 35));

        add(new JLabel(num + " Vehicle Companies:asdddddddddddddddd1"));
        add(new JLabel("Vehicle Make:"));
        add(new JLabel("Vehicle Model"));
        add(new JLabel("Vehicle Number"));
        add(new JLabel("Vehicle Driver:"));
        add(new JLabel("Vehicle Wheels"));
        add(new JLabel("Vehicle Air Con"));
    }
}