Java:JScrollPane装满后会消失
作者:互联网
我想查看一些有关程序某些对象的信息.为此,我将为其每个信息创建一个JPanel.这些面板打包在一起,并添加到ScrollPane中.
如果只有几个对象,则效果很好,因此无需滚动即可显示所有信息.一旦有更多,整个面板就会以最小化的方式消失.
很抱歉,我无法提取一个小的代码段来重现该错误.我使它尽可能的小.所以我必须给你三个班:
程序:创建对象的TreeMap并对其进行处理.在这里,它只是初始化一个整数列表.
import java.util.TreeMap;
public class Program {
// Set ObjectNumber here!
static int numberObjects = 32;
static TreeMap<Integer, Integer> objects = new TreeMap<Integer, Integer>();
static TestFrame frame;
public static void main(String[] args) {
init();
}
public static void init() {
for (int i=0; i < numberObjects; i++) {
objects.put(i, i);
}
frame = new TestFrame(objects);
frame.repaint();
}
}
TestFrame:它扩展了JFrame,将其自身设置为最大化并设置了Border Layout.之后,它从我的类TestPanel向东部添加一个JPanel.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.util.TreeMap;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.util.TreeMap;
import javax.swing.JFrame;
public class TestFrame extends JFrame {
TreeMap<Integer, Integer> objects;
TestPanel testPanel;
public TestFrame(TreeMap<Integer, Integer> objects) {
this.objects = objects;
setTitle("Program");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setExtendedState(Frame.MAXIMIZED_BOTH);
init();
}
public void init() {
setLocationRelativeTo(null);
setLayout(new BorderLayout());
testPanel = new TestPanel(objects);
this.add(testPanel, BorderLayout.EAST);
setVisible(true);
}
}
TestPanel:它扩展了JPanel.遍历所有对象,并将带有整数值的标签添加到数组名称[].然后再为每个对象创建一个仅用于此对象的面板,添加一个带有文本“ Name:”和名称[] -array中的标签的面板.与GridBagLayout.
现在,它将对象的所有面板打包在一个面板“ innerPanel”中.这将添加到滚动窗格.滚动窗格被添加到类(TestPanel)中.
import java.util.TreeMap;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Label;
import javax.swing.BorderFactory;
public class TestPanel extends JPanel {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
TreeMap<Integer, Integer> objects;
Label[] names;
JPanel[] objectPanel;
JPanel innerPanel;
Font fontLabelName = new Font("Arial", Font.BOLD, 20);
public TestPanel (TreeMap<Integer, Integer> objects) {
this.objects = objects;
names = new Label[objects.size()];
objectPanel = new JPanel[objects.size()];
this.setLayout(new GridBagLayout());
for(int i=0; i<objects.size(); i++) {
Label tempLabel = new Label(Integer.toString(objects.get(i)), SwingConstants.LEFT);
tempLabel.setFont(fontLabelName);
names[i]=tempLabel;
}
for (int i=0; i<objects.size(); i++) {
JPanel tempPanel = new JPanel();
tempPanel.setLayout(new GridBagLayout());
objectPanel[i] = tempPanel;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.gridheight = 1;
Label nameLabel = new Label("Name: ", SwingConstants.LEFT);
nameLabel.setFont(fontLabelName);
objectPanel[i].add(nameLabel, gridBagConstraints);
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.gridheight = 1;
objectPanel[i].add(names[i], gridBagConstraints);
}
innerPanel = new JPanel();
innerPanel.setLayout(new GridLayout(0,1));
for (int i = 0; i<objects.size(); i++) {
innerPanel.add(objectPanel[i]);
}
JScrollPane scrollPane = new JScrollPane(innerPanel);
// scrollPane.setBorder( BorderFactory.createEmptyBorder() );
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.add(scrollPane);
}
}
如屏幕截图所示,在我的屏幕分辨率为31个对象的情况下,效果很好.但是,如果我使用32个对象,则滚动窗格几乎消失了.
如果有人可以帮助我解决问题或链接匹配的帖子,我将非常感谢-我在Google /搜索中找不到任何帖子.
解决方法:
尝试在TestPanel中设置其他布局.如果要在面板中仅添加一个对象(案例中为scollpane),并且希望该对象占据面板的所有空间,则BorderLayout是一个不错的选择.
this.setLayout(new BorderLayout());
标签:swing,jpanel,jscrollpane,java 来源: https://codeday.me/bug/20191119/2033491.html