gpt4 book ai didi

java - 如何将多个 JPanel 添加到 JScrollPane?

转载 作者:行者123 更新时间:2023-12-04 07:25:02 24 4
gpt4 key购买 nike

我正在努力添加多个 JPanel容器到单个 JScrollPane在 Java 摆动。我想要的输出是在 JFrame 上的基本面板上有一个滚动 Pane 。 ,并且现在让滚动 Pane 包含 2 个面板。
基本上,就像滚动 Pane 可以将其 View 设置为单个面板一样,我希望将其设置为 2 个面板。
这些面板上唯一存在的东西是在它们上面绘制的图形,没有 JComponent实例。我看过其他类似的链接:Java Swing: how to add multiple JPanels to a JScrollPane ,但我没有得到想要的输出。这是我的最新尝试,它只显示滚动 Pane 边缘并且面板不可见(最小可重现):

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

最佳答案

好的,所以我做了更多的实验,我添加了这几行,现在两个面板都正确显示在 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);
}
}

关于java - 如何将多个 JPanel 添加到 JScrollPane?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68261694/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com