简体   繁体   English

将多个JPanels设置为JFrame

[英]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. 我至少要结合3个BorderLayout 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. 因为您放置在CENTER的组件将被最大化,而对于其他组件,您可以设置静态宽度(或高度),而无需进行进一步的配置即可获得所需的性能。

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

In (1) you put the game panel (3) and the "game controls" (4): 在(1)中,放置游戏面板(3)和“游戏控件”(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. 如果希望文本字段和(4)中的按钮具有相同的大小和最大的宽度(宽度),请使用GridLayout ,否则可以使用FlowLayout使其在其后留有一些空间。 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. 但是,我建议在此处进行的操作与(2)中的游戏和菜单面板相同:使用BorderLayout并将要最大化的组件放在中间。

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). 您可以使用更复杂的LayoutManager(例如BoxLayoutGridBagLayout但是对于这种简单的布局,它并不是真正需要的(猜想这只是个趣味问题)。

First,I recommend you use Swing Desinger ,it's a plug-in unit to visualize your operation. 首先,我建议您使用Swing Desinger ,它是用于可视化您的操作的插件。 I'm using GridBagLayout to format these panel,and here is an example. 我正在使用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);
}

If you want the GUI can be resizable,you can set the weghitx and weghity properties. 如果希望GUI可以调整大小,则可以设置weghitxweghity属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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