So what can be done about it? Well, this is one layout scenario that GridBagLayout cannot do. I would try using a SpringLayout:SpringLayout layout = new SpringLayout();JPanel textAndUsers = new JPanel(layout);SpringLayout.Constraints scrollPaneConstraints = new SpringLayout.Constraints(scrollPane);Spring scrollPaneWidth = scrollPaneConstraints.getWidth();SpringLayout.Constraints listConstraints = new SpringLayout.Constraints(scrollPaneWidth, scrollPaneConstraints.getY(), Spring.scale(scrollPaneWidth, 0.25f), scrollPaneConstraints.getHeight());layout.putConstraint(SpringLayout.EAST, textAndUsers, 0, SpringLayout.EAST, frmUserList);layout.putConstraint(SpringLayout.SOUTH, textAndUsers, 0, SpringLayout.SOUTH, scrollPane);textAndUsers.add(scrollPane, scrollPaneConstraints);textAndUsers.add(frmUserList, listConstraints);请注意,创建listConstraints时指定的宽度参数为Spring.scale(scrollPaneWidth, 0.25f).这样可以确保列表的宽度始终是包含JTextArea的scrollPane的四分之一.Notice that the creation of listConstraints specifies a width argument which is Spring.scale(scrollPaneWidth, 0.25f). This ensures the list is always one-fourth as wide as the scrollPane containing the JTextArea. SpringLayout使用起来很棘手,因为必须确保将布局容器的远端明确地链接到子组件,因为SpringLayout不会增长为自动容纳所有子组件.这就是putConstraint调用正在做的事情.SpringLayout is tricky to use, in that you have to make sure to link the far edges of the layout container to child components explicitly, because SpringLayout won't grow to accommodate all the child components automatically. That's what the putConstraint calls are doing. 这篇关于GridBagLayout不稳定的大小调整和显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-17 17:07