[英]Setting multiple JPanels to JFrame
我需要将多个面板格式化为我的主面板,但是我对使用哪种布局以及如何在特定布局上进行布局感到困惑。
我需要的布局是这样的
到目前为止,我拥有的代码是这样的:
public TDPanel(){
this.setPreferredSize(new Dimension(1150,800));
this.setBackground(Color.gray);
this.tdWorld = towerOfDefenses;
this.setLayout(new FlowLayout());
game = new JPanel();
game.setBackground(Color.blue);
game.setPreferredSize(new Dimension(300,300));
textBox = new JTextField();
textBox.setBackground(Color.ORANGE);
textBox.setPreferredSize(new Dimension(400,300));
menuPanel = new JPanel();
menuPanel.setPreferredSize(new Dimension(100,800));
menuPanel.setBackground(Color.red);
this.add(game);
this.add(textBox);
this.add(menuPanel);
}
我将不胜感激!
我至少要结合3个BorderLayout
。 为什么? 因为您放置在CENTER
的组件将被最大化,而对于其他组件,您可以设置静态宽度(或高度),而无需进行进一步的配置即可获得所需的性能。
+-------------------------+-----------------------+
| BorderLayout.CENTER (1) | BorderLayout.EAST (2) |
+-------------------------+-----------------------+
在(1)中,放置游戏面板(3)和“游戏控件”(4):
+-------------------------+
| BorderLayout.CENTER (3) |
+-------------------------+
| BorderLayout.SOUTH (4) |
+-------------------------+
如果希望文本字段和(4)中的按钮具有相同的大小和最大的宽度(宽度),请使用GridLayout
,否则可以使用FlowLayout
使其在其后留有一些空间。 但是,我建议在此处进行的操作与(2)中的游戏和菜单面板相同:使用BorderLayout
并将要最大化的组件放在中间。
您可以使用更复杂的LayoutManager(例如BoxLayout
或GridBagLayout
但是对于这种简单的布局,它并不是真正需要的(猜想这只是个趣味问题)。
首先,我建议您使用Swing Desinger ,它是用于可视化您的操作的插件。 我正在使用GridBagLayout
格式化这些面板,这是一个示例。 这是效果图地址
public test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 685, 485);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{175, 40, 180, 217, 0};
gbl_contentPane.rowHeights = new int[]{15, 58, 220, 49, 55, 0};
gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
JPanel gamepanel = new JPanel();
GridBagConstraints gbc_gamepanel = new GridBagConstraints();
gbc_gamepanel.fill = GridBagConstraints.BOTH;
gbc_gamepanel.insets = new Insets(0, 0, 5, 5);
gbc_gamepanel.gridheight = 2;
gbc_gamepanel.gridwidth = 3;
gbc_gamepanel.gridx = 0;
gbc_gamepanel.gridy = 1;
contentPane.add(gamepanel, gbc_gamepanel);
gamepanel.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.insets = new Insets(0, 0, 5, 0);
gbc_scrollPane.gridx = 3;
gbc_scrollPane.gridy = 1;
contentPane.add(scrollPane, gbc_scrollPane);
JPanel panel = new JPanel();
GridBagConstraints gbc_panel = new GridBagConstraints();
gbc_panel.fill = GridBagConstraints.BOTH;
gbc_panel.gridheight = 3;
gbc_panel.gridx = 3;
gbc_panel.gridy = 2;
contentPane.add(panel, gbc_panel);
panel.setLayout(null);
textField = new JTextField();
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.BOTH;
gbc_textField.insets = new Insets(0, 0, 0, 5);
gbc_textField.gridx = 0;
gbc_textField.gridy = 4;
contentPane.add(textField, gbc_textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("New button");
GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
gbc_btnNewButton.fill = GridBagConstraints.BOTH;
gbc_btnNewButton.insets = new Insets(0, 0, 0, 5);
gbc_btnNewButton.gridx = 2;
gbc_btnNewButton.gridy = 4;
contentPane.add(btnNewButton, gbc_btnNewButton);
}
如果希望GUI可以调整大小,则可以设置weghitx
和weghity
属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.