Java AWT:如何在带有gridlayout的面板内部的标签旁边,使带有流布局的复选框垂直居中?

时间:2019-06-10 01:06:39

标签: java checkbox awt grid-layout flowlayout

我正在用Java AWT编写一个小程序。

我有一个带有GridLayout的面板(2列)。面板的左侧是标签,右侧是CheckboxGroup。

CheckboxGroup位于FlowLayout中。它与标签的高度不同。如何调整CheckboxGroup的高度/垂直居中?

我尝试将CheckboxGroup(带有FlowLayout)包装在BorderLayout中,并将其设置为BorderLayout.CENTER/SOUTH。

Panel panelSelector = new Panel(new GridLayout(0,2,3,3));

HotTubLabel = new Label("Hot Tub option:");
    panelSelector.add(HotTubLabel);
    HotTubInput = new CheckboxGroup();
    Panel panelHotTubCheck = new Panel(new FlowLayout());
    panelHotTubCheck.add(new Checkbox("Yes", HotTubInput, false));
    panelHotTubCheck.add(new Checkbox("No", HotTubInput, false));
    panelHotTubCheck.add(new Checkbox("No preference", HotTubInput, true));
    panelSelector.add(panelHotTubCheck);

我希望CheckboxGroup行的高度与Label行的高度相同,就像选择其他所有对象一样,如TextField或Label。

这是一张图片:Link to the picture because I don't have enough reputation ^^

编辑:通过将GridLayout用于CheckboxGroup修复了对齐方式,但是现在GridLayout在程序的整个长度上拉伸了CheckboxGroup,并在默认程序大小中将“无首选项”削减为“无公关”。

查看此图片:Link

我希望它们像在FlowLayout之前一样显示,但要垂直对齐。

编辑2:这是初始问题的最小可重现示例:

import java.awt.*;
import java.awt.event.*;

public class ChaletSelectorExample1 extends Frame {

  private Label HotTubLabel;
  private CheckboxGroup HotTubInput;

  public ChaletSelectorExample1(){

    Panel panelSelector = new Panel(new GridLayout(0,2,3,3));

    HotTubLabel = new Label("Hot Tub option:");
    panelSelector.add(HotTubLabel);
    HotTubInput = new CheckboxGroup();
    Panel panelHotTubCheck = new Panel(new FlowLayout());
    panelHotTubCheck.add(new Checkbox("Yes", HotTubInput, false));
    panelHotTubCheck.add(new Checkbox("No", HotTubInput, false));
    panelHotTubCheck.add(new Checkbox("No preference", HotTubInput, true));
    panelSelector.add(panelHotTubCheck);

    setLayout(new BorderLayout(3, 3));
    add(panelSelector, BorderLayout.CENTER);

    addWindowListener(new WindowAdapter() {
      @Override public void windowClosing(WindowEvent evt) {
        System.exit(0);
      }
    });

    setTitle("ChaletSelectorExample1");
    setSize(500, 500);
    setVisible(true);  

  }

  public static void main(String[] args) {
      new ChaletSelectorExample1();
  }

}

以下是该问题的最小可重现示例,其中在解决了最初的问题后将文本截断:

import java.awt.*;
import java.awt.event.*;

public class ChaletSelectorExample2 extends Frame {

  private Label HotTubLabel;
  private CheckboxGroup HotTubInput;

  public ChaletSelectorExample2(){

    Panel panelSelector = new Panel(new GridLayout(0,2,3,3));

    HotTubLabel = new Label("Hot Tub option:");
    panelSelector.add(HotTubLabel);
    HotTubInput = new CheckboxGroup();
    Panel panelHotTubCheck = new Panel(new GridLayout(0,3,0,0));
    panelHotTubCheck.add(new Checkbox("Yes", HotTubInput, false));
    panelHotTubCheck.add(new Checkbox("No", HotTubInput, false));
    panelHotTubCheck.add(new Checkbox("No preference", HotTubInput, true));
    panelSelector.add(panelHotTubCheck);

    setLayout(new BorderLayout(3, 3));
    add(panelSelector, BorderLayout.CENTER);

    addWindowListener(new WindowAdapter() {
      @Override public void windowClosing(WindowEvent evt) {
        System.exit(0);
      }
    });

    setTitle("ChaletSelectorExample2");
    setSize(500, 500);
    setVisible(true);  

  }

  public static void main(String[] args) {
      new ChaletSelectorExample2();
  }

}

这就是我想要的样子(当然,还有所有其他我想放在顶部或下方的选项):Link

0 个答案:

没有答案