如何将多个JPanel添加到jscrollpane?

iecba09b  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(394)

我正在努力添加多个 JPanel 集装箱到单个集装箱 JScrollPane 在JavaSwing中。我想要的输出是在一个屏幕上的基本面板上有一个滚动窗格 JFrame ,并使滚动窗格包含2个面板,目前。
基本上,就像滚动窗格可以将其视图设置为单个面板一样,我希望将其设置为两个面板。
这些面板上唯一存在的东西是在上面绘制的图形,不是吗 JComponent 示例。我看过其他类似的链接:javaswing:如何将多个jpanel添加到jscrollpane,但我没有得到想要的输出。这是我的最新尝试,它只显示滚动窗格边缘,而面板不可见(最小可复制):

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

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        JFrame frame = new JFrame("Test"); //initialize frame
        frame.setSize(500, 500);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel base = new JPanel(); //make base panel with SpringLayout
        SpringLayout baseLayout = new SpringLayout();
        base.setLayout(baseLayout);

        JPanel panel1 = new JPanel() { //initialize JPanels
            public void paintComponent(Graphics tool) {
                super.paintComponent(tool);
                tool.drawRect(50, 50, 100, 100);
            }
        };

        JPanel panel2 = new JPanel() {
            public void paintComponent(Graphics tool) {
                super.paintComponent(tool);
                tool.drawRect(75, 75, 50, 50);
            }
        };

        Container cont = new Container(); //create a container to hold panels
        SpringLayout contLayout = new SpringLayout();
        cont.setLayout(contLayout);

        contLayout.putConstraint(SpringLayout.WEST, panel1, 0, SpringLayout.WEST, cont); 
        contLayout.putConstraint(SpringLayout.NORTH, panel1, 0, SpringLayout.NORTH, cont);
        cont.add(panel1); //add panel1 to top left corner of container

        contLayout.putConstraint(SpringLayout.WEST, panel2, 0, SpringLayout.WEST, cont); 
        contLayout.putConstraint(SpringLayout.NORTH, panel2, 0, SpringLayout.NORTH, cont);
        cont.add(panel2); //add panel2 to top left corner of container

        cont.setPreferredSize(new Dimension(201, 201));

        JScrollPane scroll = new JScrollPane(cont); //add container to scroll pane
        scroll.setPreferredSize(new Dimension(200, 200));

        baseLayout.putConstraint(SpringLayout.HORIZONTAL_CENTER, scroll, 0, SpringLayout.HORIZONTAL_CENTER, base); 
        baseLayout.putConstraint(SpringLayout.VERTICAL_CENTER, scroll, 0, SpringLayout.VERTICAL_CENTER, base);
        base.add(scroll); //add scroll pane to center of base JPanel

        frame.add(base);
        frame.setVisible(true);
    }
}
6za6bjd0

6za6bjd01#

好的,我做了更多的实验,添加了这几行,现在两个面板都正确地显示在jscrollpane中:

panel1.setPreferredSize(cont.getPreferredSize());
panel2.setPreferredSize(cont.getPreferredSize());

panel1.setOpaque(false);
panel2.setOpaque(false);

不知道它为什么会起作用,如果有人能解释的话,那就太好了。以下是工作代码:

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

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        JFrame frame = new JFrame("Test"); //initialize frame
        frame.setSize(500, 500);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel base = new JPanel(); //make base panel with SpringLayout
        SpringLayout baseLayout = new SpringLayout();
        base.setLayout(baseLayout);

        JPanel panel1 = new JPanel() { //initialize JPanels
            public void paintComponent(Graphics tool) {
                super.paintComponent(tool);
                tool.drawRect(50, 50, 100, 100);
            }
        };

        JPanel panel2 = new JPanel() {
            public void paintComponent(Graphics tool) {
                super.paintComponent(tool);
                tool.drawRect(75, 75, 50, 50);
            }
        };

        Container cont = new Container(); //create a container to hold panels
        SpringLayout contLayout = new SpringLayout();
        cont.setLayout(contLayout);

        contLayout.putConstraint(SpringLayout.WEST, panel1, 0, SpringLayout.WEST, cont); 
        contLayout.putConstraint(SpringLayout.NORTH, panel1, 0, SpringLayout.NORTH, cont);
        cont.add(panel1); //add panel1 to top left corner of container

        contLayout.putConstraint(SpringLayout.WEST, panel2, 0, SpringLayout.WEST, cont); 
        contLayout.putConstraint(SpringLayout.NORTH, panel2, 0, SpringLayout.NORTH, cont);
        cont.add(panel2); //add panel2 to top left corner of container

        cont.setPreferredSize(new Dimension(201, 201));
        panel1.setPreferredSize(cont.getPreferredSize());
        panel2.setPreferredSize(cont.getPreferredSize());

        panel1.setOpaque(false);
        panel2.setOpaque(false);

        JScrollPane scroll = new JScrollPane(cont); //add container to scroll pane
        scroll.setPreferredSize(new Dimension(200, 200));

        baseLayout.putConstraint(SpringLayout.HORIZONTAL_CENTER, scroll, 0, SpringLayout.HORIZONTAL_CENTER, base); 
        baseLayout.putConstraint(SpringLayout.VERTICAL_CENTER, scroll, 0, SpringLayout.VERTICAL_CENTER, base);
        base.add(scroll); //add scroll pane to center of base JPanel

        frame.add(base);
        frame.setVisible(true);
    }
}

相关问题