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

时间:2017-06-19 13:53:01

标签: java swing jpanel jscrollpane layout-manager

I am having one main JPanel in which I had used several other panels. I want to add a scroll pane to my panel so that when child panel go out of frame they should be scrollable, but I am unable to achieve this.

How to achieve correct scrolling functionality for panels within a panel?

Code inside my child panel:

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);
    }

}

Code inside my main panel:

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);
}
}

3 个答案:

答案 0 :(得分:2)

将主面板从FlowLayout(默认)更改为使用面板的子组件填充可用空间(此处使用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"));
    }
}

答案 1 :(得分:1)

我猜测问题出在您的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);

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

答案 2 :(得分:0)

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

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