将多个JPanel设置为JFrame

时间:2015-05-16 01:14:16

标签: java swing jpanel layout-manager

我需要将多个面板格式化为我的主面板,但是我对使用哪种布局感到困扰,更多的是如何使用特定的布局。

我需要的布局是这样的

到目前为止,我的代码是:

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);
}

我很感激任何帮助!

2 个答案:

答案 0 :(得分:1)

我会至少合并3 BorderLayout秒。为什么?因为放在CENTER中的组件将最大化,而对于其他组件,您可以设置静态宽度(或高度),而不必进行进一步配置以获得所需的行为。

+-------------------------+-----------------------+
| BorderLayout.CENTER (1) | BorderLayout.EAST (2) |
+-------------------------+-----------------------+

在(1)中你放了游戏面板(3)和“游戏控制”(4):

+-------------------------+
| BorderLayout.CENTER (3) |
+-------------------------+
| BorderLayout.SOUTH (4)  |
+-------------------------+

如果您希望文本字段和(4)中的按钮具有相同的大小和最大化(宽度),请使用GridLayout,否则您可以使用FlowLayout将它们与它之后的一些空间。但我建议在这里做的与(2)中的游戏和菜单面板相同:使用BorderLayout并将想要最大化的组件放在中心。

您可以使用更复杂的LayoutManagers,例如BoxLayoutGridBagLayout,但这种简单布局并不需要它(猜测它是品味的问题)。

答案 1 :(得分:-1)

首先,我建议您使用 Swing Desinger ,它是一个插件,可视化您的操作。   我使用GridBagLayout格式化这些面板,这是一个示例。    This is the effect drawing adress

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可以调整大小,可以设置weghitxweghity属性。