为什么我的 JPanel 的首选大小没有改变,而其包含的 JScrollPane 在 GroupLayout 中却发生了变化?
使用 GroupLayout 和下面的代码,我注意到 leftPanel 没有更新它的首选大小。我认为这应该是自动的,并且似乎可以在其他布局管理器中工作。我怎样才能通过 GroupLayout 获得这种行为?
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SizeTesting {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
JSplitPane splitPane = new JSplitPane();
final JPanel leftPanel = new JPanel();
final DefaultListModel listModel = new DefaultListModel();
final JList list = new JList(listModel);
final JScrollPane jsp = new JScrollPane(list);
leftPanel.add(jsp);
splitPane.setLeftComponent(leftPanel);
GroupLayout panel1Layout = new GroupLayout(leftPanel);
leftPanel.setLayout(panel1Layout);
panel1Layout.setHorizontalGroup(
panel1Layout.createParallelGroup()
.addComponent(jsp, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
);
panel1Layout.setVerticalGroup(
panel1Layout.createParallelGroup()
.addGroup(GroupLayout.Alignment.TRAILING, panel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jsp, GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE))
);
JPanel rightPanel = new JPanel();
JButton button = new JButton("Add Long Item");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("Preferred Size of List BEFORE: " + list.getPreferredSize());
System.out.println("Preferred Size of ScorllPane BEFORE: " + jsp.getPreferredSize());
System.out.println("Preferred size of LeftPanel BEFORE: " + leftPanel.getPreferredSize());
System.out.println();
listModel.addElement("Really long, new Item in List");
System.out.println("Preferred Size of List AFTER: " + list.getPreferredSize());
System.out.println("Preferred Size of ScorllPane AFTER: " + jsp.getPreferredSize());
System.out.println("Preferred size of LeftPanel AFTER: " + leftPanel.getPreferredSize());
}
});
rightPanel.add(button);
splitPane.setRightComponent(rightPanel);
listModel.addElement("Test");
listModel.addElement("Test2");
listModel.addElement("Test3");
frame.add(splitPane);
frame.setVisible(true);
}
} // end class SizeTesting
Using GroupLayout and the code below, I noticed that the leftPanel is not updating it's preferred size. I thought this was supposed to be automatic and appears to work in other layout managers. How can I get that behaviour with GroupLayout?
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SizeTesting {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
JSplitPane splitPane = new JSplitPane();
final JPanel leftPanel = new JPanel();
final DefaultListModel listModel = new DefaultListModel();
final JList list = new JList(listModel);
final JScrollPane jsp = new JScrollPane(list);
leftPanel.add(jsp);
splitPane.setLeftComponent(leftPanel);
GroupLayout panel1Layout = new GroupLayout(leftPanel);
leftPanel.setLayout(panel1Layout);
panel1Layout.setHorizontalGroup(
panel1Layout.createParallelGroup()
.addComponent(jsp, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
);
panel1Layout.setVerticalGroup(
panel1Layout.createParallelGroup()
.addGroup(GroupLayout.Alignment.TRAILING, panel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jsp, GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE))
);
JPanel rightPanel = new JPanel();
JButton button = new JButton("Add Long Item");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("Preferred Size of List BEFORE: " + list.getPreferredSize());
System.out.println("Preferred Size of ScorllPane BEFORE: " + jsp.getPreferredSize());
System.out.println("Preferred size of LeftPanel BEFORE: " + leftPanel.getPreferredSize());
System.out.println();
listModel.addElement("Really long, new Item in List");
System.out.println("Preferred Size of List AFTER: " + list.getPreferredSize());
System.out.println("Preferred Size of ScorllPane AFTER: " + jsp.getPreferredSize());
System.out.println("Preferred size of LeftPanel AFTER: " + leftPanel.getPreferredSize());
}
});
rightPanel.add(button);
splitPane.setRightComponent(rightPanel);
listModel.addElement("Test");
listModel.addElement("Test2");
listModel.addElement("Test3");
frame.add(splitPane);
frame.setVisible(true);
}
} // end class SizeTesting
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
组件的优选大小基于添加到其中的组件的优选大小。
是的,当您向列表中添加项目时,JList 的首选大小将会改变。但是,JList 被添加到 JScrollPane 中,因此 JScrollPane 的首选大小用于确定 JPanel 的首选大小。
由于您使用 IDE 来创建 GUI,因此看起来 JScrollPane 的首选大小是在设计时确定的并硬编码到约束中。当调整框架大小或向 JList 添加项目时,JScrollPane 的首选大小不会改变。 JScrollPane 和 JPanel 的“大小”将随着框架大小的调整而改变。
The preferred size of a component is based on the preferred size of the components added to it.
Yes, the preferred size of the JList will change as you add items to the list. However, the JList is added to a JScrollPane, so its the preferred size of the JScrollPane that is used to determine the preferred size of the JPanel.
Since you are using an IDE to create your GUI is looks like the preferred size of the JScrollPane is determined at design time and hardcoded into the constraint. The preferred size of the JScrollPane will not change as the frame is resized or as you add items to the JList. The "size" of the JScrollPane and JPanel will change as the frame is resized.
并非所有 LayoutManager 在进行布局时都会考虑 Component.preferredSize()。据我所知,有两个是 FlowLayout 和 BoxLayout (其他人可能会,但他们可能不会)。
Not all LayoutManagers take Component.preferredSize() into account when doing their layout. The two that I know of that do are FlowLayout and BoxLayout (others might, but they might not).