简体   繁体   中英

Setting multiple JPanels to JFrame

I need to format my multiple panels into my main panel, however I'm troubled with which layout to use and more so on how to do it with a specific layout.

The layout I need is like this

So far the code I have is this:

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

I would appreciate any help given!

I would combine at least 3 BorderLayout s. Why? Because the component you put in CENTER will be maximized and for the others you can set a static width (or height) and don't have to do further configuration to get the desired behaviour.

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

In (1) you put the game panel (3) and the "game controls" (4):

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

If you want the text field and the button in (4) to have the same size and maximized (width) then use a GridLayout , othewise you can use FlowLayout to have them layed out with some space after it. But what I recommend doing here is the same as for the game and menu panel in (2): use a BorderLayout and put the component you want to be maximized in the center.

You could use more sophisticated LayoutManagers like BoxLayout or GridBagLayout but it is not really needed for this simple layout (guess it's a matter of taste).

First,I recommend you use Swing Desinger ,it's a plug-in unit to visualize your operation. I'm using GridBagLayout to format these panel,and here is an example. 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);
}

If you want the GUI can be resizable,you can set the weghitx and weghity properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM