如何将多个 JPanel 添加到 JScrollPane?
How to add multiple JPanels to a JScrollPane?
我正在努力将多个 JPanel
容器添加到 Java swing 中的单个 JScrollPane
。我想要的输出是在 JFrame
上的基础面板上有一个滚动窗格,并且现在让该滚动窗格包含 2 个面板。
基本上,就像滚动窗格可以将其视图设置为单个面板一样,我想将它设置为 2 个面板。
这些面板上唯一存在的是在其上绘制的图形,没有 JComponent
个实例。我查看了其他类似链接:Java Swing: how to add multiple JPanels to a 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);
}
}
好吧,所以我做了更多的实验并添加了这几行,现在两个面板都正确显示在 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);
}
}
我正在努力将多个 JPanel
容器添加到 Java swing 中的单个 JScrollPane
。我想要的输出是在 JFrame
上的基础面板上有一个滚动窗格,并且现在让该滚动窗格包含 2 个面板。
基本上,就像滚动窗格可以将其视图设置为单个面板一样,我想将它设置为 2 个面板。
这些面板上唯一存在的是在其上绘制的图形,没有 JComponent
个实例。我查看了其他类似链接:Java Swing: how to add multiple JPanels to a 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);
}
}
好吧,所以我做了更多的实验并添加了这几行,现在两个面板都正确显示在 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);
}
}