跟进@MadProgrammer评论:
您真的需要学习如何使用布局管理器.设置尺寸不是可行的方法,因为它们在不同的机器上执行不同的操作.
关于布局管理器的一个重要事项是哪些布局尊重其内部组件的首选大小.那些不尊重尺寸的产品会拉伸部件.某些布局可能无法拉伸其组件,但会在主容器拉伸时将它们放置在开放空间内的默认位置.
为了获得所需的结果,有时还需要嵌套具有不同布局的容器,这利用了两个或更多布局.
我知道这对你的问题并不是一个很好的答案,但我认为你仍然可以通过使用布局管理器来了解你的问题,以及如何实现你想要的目标.
下面我简单介绍了一些主要布局管理器的不同性质.你可以玩它.请注意,主JFrame使用默认的BorderLayout.我只是将布局明确设置为BorderLayout,以便您可以看到哪种布局会产生效果.
另请查看Laying out Components Withing a Container以了解有关如何使用不同布局管理器的更多信息.避免使用null布局并尝试自己定位所有内容.让布局为您完成,因为Swing是为与布局管理器一起使用而构建的.
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.*;
public class TestingLayoutManagers {
private JPanel northFlowLayoutPanel;
private JPanel southBorderLayoutPanel;
private JPanel centerGridBagLayoutPanel;
private JPanel westBoxLayoutPanel;
private JPanel eastGridLayoutPanel;
private final JButton northButton = new JButton("North Button");
private final JButton southButton = new JButton("South Button");
private final JButton centerButton = new JButton("Center Button");
private final JButton eastButton = new JButton("East Button");
private final JButton menuButton1 = new JButton("Menu Item 1");
private final JButton menuButton2 = new JButton("Menu Item 2");
private final JButton menuButton3 = new JButton("Menu Item 3");
private final JButton menuButton4 = new JButton("Menu Item 4");
private final JButton menuButton5 = new JButton("Menu Item 5");
public TestingLayoutManagers() {
northFlowLayoutPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
southBorderLayoutPanel = new JPanel(new BorderLayout());
centerGridBagLayoutPanel = new JPanel(new GridBagLayout());
eastGridLayoutPanel = new JPanel(new GridLayout(1, 1));
Box box = Box.createVerticalBox();
westBoxLayoutPanel = new JPanel();
northFlowLayoutPanel.add(northButton);
northFlowLayoutPanel.setBorder(BorderFactory.createTitledBorder("Flow Layout"));
southBorderLayoutPanel.add(southButton);
southBorderLayoutPanel.setBorder(BorderFactory.createTitledBorder("Border Layout"));
centerGridBagLayoutPanel.add(centerButton);
centerGridBagLayoutPanel.setBorder(BorderFactory.createTitledBorder("GridBag Layout"));
eastGridLayoutPanel.add(eastButton);
eastGridLayoutPanel.setBorder(BorderFactory.createTitledBorder("Grid Layout"));
box.add(menuButton1);
box.add(menuButton2);
box.add(menuButton3);
box.add(menuButton4);
box.add(menuButton5);
westBoxLayoutPanel.add(box);
westBoxLayoutPanel.setBorder(BorderFactory.createTitledBorder("Box Layout"));
JFrame frame = new JFrame("Test Layout Managers");
frame.setLayout(new BorderLayout()); // This is the deafault layout
frame.add(northFlowLayoutPanel, BorderLayout.PAGE_START);
frame.add(southBorderLayoutPanel, BorderLayout.PAGE_END);
frame.add(centerGridBagLayoutPanel, BorderLayout.CENTER);
frame.add(eastGridLayoutPanel, BorderLayout.LINE_END);
frame.add(westBoxLayoutPanel, BorderLayout.LINE_START);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException
| InstantiationException
| IllegalAccessException
| UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
new TestingLayoutManagers();
});
}
}