public static void main(String[] args) {
// TODO code application logic here
JFrame frame = new JFrame();
SpringLayout layout = new SpringLayout();
frame.getContentPane().setLayout(layout);
TitledBorder border = null;
border = BorderFactory.createTitledBorder("Group1");
frame.setSize(620, 620);
JPanel mainPanel = new JPanel();
mainPanel.setBorder(border);
//mainPanel.setPreferredSize( new Dimension(600,600));
layout.putConstraint(SpringLayout.EAST, mainPanel, -15, SpringLayout.EAST, frame.getContentPane());
layout.putConstraint(SpringLayout.WEST, mainPanel, 15, SpringLayout.WEST, frame.getContentPane());
layout.putConstraint(SpringLayout.SOUTH, mainPanel, -15, SpringLayout.SOUTH, frame.getContentPane());
layout.putConstraint(SpringLayout.NORTH, mainPanel, 15, SpringLayout.NORTH, frame.getContentPane());
frame.getContentPane().add(mainPanel);
frame.setVisible(true);
}
我正在使用面板及其父级自动调整大小的效果,而其父级在Java 1.4.2中调整大小。
我使用SpringLayout进行此操作,但是我发现,如果在WEST和NORTH之后定义SpringLayout.East和SpringLayout.South,则面板(mainPanel)将不会位于(15,15)。如果在WEST和SOUTH前面定义了EAST和NORTH,则将执行此操作。
这是为什么?
最佳答案
如此相关question中所述,约束指定的顺序可能很重要,尤其是在结果布局为over-constrained的情况下。订单WEST, NORTH, EAST, SOUTH
是导致重置的示例,相关的bug report的“变通评估”部分中提到了该订单。