Java JFrame/Swing 对齐

Java JFrame/Swing alignment

我是新手,目前正在学习 JFrame,我 运行 遇到了 ComboBox、复选框和按钮的对齐问题。听众还没有连接起来,这只是一个测试,看看它是否正确对齐。我的问题是为什么对齐功能似乎不起作用?我希望组合框位于顶部,复选框位于中间,按钮位于底部。

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.SwingConstants;

public class CheckBoxFrame extends JFrame 
{

private final JComboBox<String> colorComboBox; 
private final JCheckBox backgroundColorCheckBox; 
private final JCheckBox foregroundColorCheckBox; 
private final JButton okButton;
private final JButton cancelButton;

private static final String[] color =
{"Red", "Blue", "Green", "Purple"};

public CheckBoxFrame()
{
  super("ColorSelect");
  setLayout(new FlowLayout());


  colorComboBox = new JComboBox<String>(color);
  colorComboBox.setAlignmentX(SwingConstants.TOP);
  add(colorComboBox);


  backgroundColorCheckBox = new JCheckBox("Background"); 
  foregroundColorCheckBox = new JCheckBox("Foreground"); 
  foregroundColorCheckBox.setVerticalAlignment(SwingConstants.CENTER);
  backgroundColorCheckBox.setVerticalAlignment(SwingConstants.CENTER);
  add(backgroundColorCheckBox); // add bold checkbox to JFrame
  add(foregroundColorCheckBox); // add italic checkbox to JFrame

  okButton = new JButton("Ok");
  cancelButton = new JButton("Cancel");
  okButton.setVerticalAlignment(SwingConstants.BOTTOM);
  cancelButton.setVerticalAlignment(SwingConstants.BOTTOM);
  add(okButton);
  add(cancelButton);


  // register listeners for JCheckBoxes
  CheckBoxHandler handler = new CheckBoxHandler();
  backgroundColorCheckBox.addItemListener(handler);
  foregroundColorCheckBox.addItemListener(handler);
  } 


private class CheckBoxHandler implements ItemListener 
{
  // respond to checkbox events
  @Override
  public void itemStateChanged(ItemEvent event)
  {

  } 
 }
} // end class CheckBoxFrame

来自JavaDocs for setVerticalAlignment

Sets the vertical alignment of the icon and text.

这改变了 icontext 属性在组件上下文中的布局方式。

setVerticalAlignment 不会影响组件在容器中的布局方式。

要更改组件在容器中的布局方式,您需要更改布局管理器,请参阅 Lesson: Laying Out Components Within a Container

根据您的要求,您可以通过多种方式实现它。

您可以使用 GridLayout,将单个行的所有组件包裹在它们自己的 JPanel 中,并将此面板添加到主容器中。

您也可以使用 GridBagLayout,它虽然更复杂,但也更灵活

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.SwingConstants;

public class CheckBoxFrame extends JFrame 
{

private final JComboBox<String> colorComboBox; 
private final JCheckBox backgroundColorCheckBox; 
private final JCheckBox foregroundColorCheckBox; 
private final JButton okButton;
private final JButton cancelButton;

private static final String[] color = {"Red", "Blue", "Green", "Purple"};

public CheckBoxFrame()
{
  super("ColorSelect");
  setLayout(new GridBagLayout());

  GridBagConstraints c = new GridBagConstraints();
  colorComboBox = new JComboBox<String>(color);
  c.fill = GridBagConstraints.HORIZONTAL;
  c.gridx = 0;
  c.gridy = 0;
  c.gridwidth = 2;
  add(colorComboBox, c);


  backgroundColorCheckBox = new JCheckBox("Background"); 
  c.fill = GridBagConstraints.HORIZONTAL;
  c.gridx = 0;
  c.gridy = 1;
  c.gridwidth = 1;
  add(backgroundColorCheckBox, c); // add bold checkbox to JFrame

  foregroundColorCheckBox = new JCheckBox("Foreground");
  c.fill = GridBagConstraints.HORIZONTAL;
  c.gridx = 1;
  c.gridy = 1;
  c.gridwidth = 1;
  add(foregroundColorCheckBox, c); // add italic checkbox to JFrame

  okButton = new JButton("Ok");
  c.fill = GridBagConstraints.HORIZONTAL;
  c.gridx = 0;
  c.gridy = 2;
  c.gridwidth = 1;
  add(okButton, c);

  cancelButton = new JButton("Cancel");
  c.fill = GridBagConstraints.HORIZONTAL;
  c.gridx = 1;
  c.gridy = 2;
  c.gridwidth = 1;
  add(cancelButton, c);


  // register listeners for JCheckBoxes
  CheckBoxHandler handler = new CheckBoxHandler();
  backgroundColorCheckBox.addItemListener(handler);
  foregroundColorCheckBox.addItemListener(handler);
  } 


private class CheckBoxHandler implements ItemListener 
{
  // respond to checkbox events
  @Override
  public void itemStateChanged(ItemEvent event)
  {

  } 
 }
} // end class CheckBoxFrame