简体   繁体   English

简单的GUI Java布局问题

[英]Simple GUI Java layout issue

I'm trying to do the following and I'm not sure what type of Java layout to use. 我正在尝试执行以下操作,但不确定使用哪种类型的Java布局。

I want a JFrame with a single panel totalPanel . 我想要一个带有单个面板totalPanelJFrame totalPanel should contain two panels which are custom classes I write, PanelA and PanelB. totalPanel应该包含两个面板,它们是我编写的自定义类,即PanelA和PanelB。 PanelA starts out with height 200 and PanelB starts out with height 400. When the user expands the window or resizes the window, only PanelB should increase in height, but both panels can increase in width. PanelA以200的高度开始, PanelB以400的高度开始。当用户扩展窗口或调整窗口大小时,只有PanelB高度应增加,但是两个面板的宽度都可以增加。

How can I set this up? 我该如何设置? I've tried to use BorderLayout but then "North" is always too small in height. 我尝试使用BorderLayout,但是“ North”的高度始终太小。 I've tried to use BoxLayout but then both PanelA and PanelB are always the same height. 我尝试使用BoxLayout,但是PanelAPanelB总是相同的高度。

Consider having totalPanel use a BorderLayout. 考虑让totalPanel使用BorderLayout。 Add the PanelA to the BorderLayout.PAGE_START position and the PanelB to the BorderLayout.CENTER position. 将PanelA添加到BorderLayout.PAGE_START位置,将PanelB添加到BorderLayout.CENTER位置。

For more on the layout managers, please review the tutorial: Lesson: Laying Out Components Within a Container 有关布局管理器的更多信息,请查看教程: 课程:在容器中布置组件

Edit: I see that you've used BorderLayout, that it "doesn't work" but you don't show code. 编辑:我看到您使用了BorderLayout,它“不起作用”,但是您不显示代码。 For more fine tuning on why it doesn't work, consider showing us code. 要进一步了解为什么它不起作用,请考虑向我们展示代码。

To get it to work consider giving your PanelX classes getPreferredSize() overrides that would help set the initial sizes of the JPanels. 要使其正常工作,请考虑为您的PanelX类提供getPreferredSize()重写,以帮助设置JPanels的初始大小。

For example: 例如:

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.*;

public class SimpleGuiLayout {

   private static void createAndShowGui() {
      JPanel totalPanel = new JPanel(new BorderLayout());

      totalPanel.add(new PanelX(800, 200, "Panel A"), BorderLayout.PAGE_START);
      totalPanel.add(new PanelX(800, 400, "Panel B"), BorderLayout.CENTER);
      JFrame frame = new JFrame("Simple Gui Layout");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(totalPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class PanelX extends JPanel {
   private int prefW;
   private int prefH;

   public PanelX(int prefW, int prefH, String title) {
      this.prefW = prefW;
      this.prefH = prefH;
      setBorder(BorderFactory.createTitledBorder(title));
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(prefW, prefH);
   }
}

If run, it would look like so: 如果运行,它将如下所示:
在此处输入图片说明

And would resize appropriately. 并会适当调整大小。

One approach (probably not the simplest) would be a GridBagLayout. 一种方法(可能不是最简单的方法)是GridBagLayout。

Use the weightx and weighty constraints to control which members can resize. 使用weightx和weighty约束来控制可以调整大小的成员。

I recommend a GridBagLayout. 我建议使用GridBagLayout。 It is complex to use, but it can solve your problem. 使用起来很复杂,但是可以解决您的问题。 Assuming panelA is on top of panelB, you would have the following constraints: 假设panelA在panelB的顶部,则将具有以下约束:

  • panelA and panelB's gridx would be 0 panelA和panelB的gridx将为0
  • panelA would be ad gridy 0, and panelB at gridy 1 panelA将为广告网格0,panelB将为网格1
  • for both, you would set fill = BOTH 对于这两者,您都将设置fill = BOTH
  • but to have only panelB increase in height, you would set panelB's weighty to 1.0, and panelA's to 0 但要仅增加panelB的高度,可以将panelB的权重设置为1.0,而panelA的权重设置为0

You originally solution using BorderLayout will work already, you just need to set preferred height of panelA: 您最初使用BorderLayout的解决方案已经可以使用,您只需要设置panelA的首选高度即可:

    panelA.setPreferredSize(new Dimension(0, 200));

The width doesn't matter because BorderLayout will ignore preferred width of component added to NORTH or SOUTH. 宽度无关紧要,因为BorderLayout会忽略添加到NORTH或SOUTH的组件的首选宽度。

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

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