Java Swing对齐按钮、标签和文本字段

Java Swing对齐按钮、标签和文本字段,java,swing,alignment,layout-manager,Java,Swing,Alignment,Layout Manager,我正试图将我的JButton和JTextArea与代码的中间底部并排对齐。我遵循了一些教程,得出了这一点。当我执行程序时,文本区域和按钮仍然在顶部对齐。救命啊 import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class

我正试图将我的JButton和JTextArea与代码的中间底部并排对齐。我遵循了一些教程,得出了这一点。当我执行程序时,文本区域和按钮仍然在顶部对齐。救命啊

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;


public class Main extends JFrame implements ActionListener{

JPanel panel = new JPanel();
JButton button = new JButton("Confirm!");
JTextArea text = new JTextArea(1, 20);
public Main() {
    super("Battleship!");
    setLayout(new FlowLayout());
    button.addActionListener(this);
    setSize(600, 500);
    setResizable(true);
    button.setLayout(new FlowLayout(FlowLayout.CENTER));
    text.setLayout(new FlowLayout(FlowLayout.CENTER));
    panel.add(text, BorderLayout.SOUTH);
    panel.add(button, BorderLayout.SOUTH);
    add(panel);

    setVisible(true);
}
public static void main(String[] args) {
    new Main();
}
@Override
public void actionPerformed(ActionEvent e) {
    button.setText(text.getText());
}

}

请参见代码注释中的注释

import java.awt.*;
import javax.swing.*;

public class MainLayout extends JFrame {

    // A panel defaults to flow layout. Use the default
    JPanel panel = new JPanel();
    JButton button = new JButton("Confirm!");
    JTextArea text = new JTextArea(1, 20);

    public MainLayout() {
        super("Battleship!");
        //setLayout(new FlowLayout()); // use the default border layout
        setSize(600, 500); // should be packed after components added.
        //setResizable(true); // this is the default
        /* Don't set layouts on things like buttons or text areas
        This is only useful for containers to which we add other
        components, and it is rarely, if ever, useful to add components
        to these types of GUI elements. */
        //button.setLayout(new FlowLayout(FlowLayout.CENTER));
        // text.setLayout(new FlowLayout(FlowLayout.CENTER));

        /* No need for layout constraints when adding these to 
        a flow layout */
        //panel.add(text, BorderLayout.SOUTH);
        panel.add(text);
        //panel.add(button, BorderLayout.SOUTH);
        panel.add(button);

        // NOW we need the constraint!
        add(panel, BorderLayout.PAGE_END);

        setVisible(true);
    }

    public static void main(String[] args) {
        /* Swing/AWT GUIs should be started on the EDT.
        Left as an exercise for the reader */
        new MainLayout();
    }
}

不需要将任何flow layout设置为按钮或文本以放置在中心,因为面板默认具有flow layout center,它将组件放置在中心 默认情况下,框架具有边框布局 要将按钮和文本区域放置在底部,只需将它们都添加到已完成的panel中,然后将panel添加到框架中,参数为BorderLayout.SOUTH 请参阅下面的修改代码,它将根据您的需要工作

 import java.awt.BorderLayout;
 import java.awt.FlowLayout;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import javax.swing.*;
 public class Main extends JFrame implements ActionListener
 {
      JPanel panel = new JPanel();
      JButton button = new JButton("Confirm!");
      JTextArea text = new JTextArea(1, 20);
      public Main()
      {
          super("Battleship!");
          button.addActionListener(this);
          setSize(600, 500);
          setResizable(true);
          panel.add(text);
          panel.add(button);
          add(panel, BorderLayout.SOUTH);
          setVisible(true);
      } 
      public static void main(String[] args)
      {
          new Main();
      }
      @Override public void actionPerformed(ActionEvent e)
      {
           button.setText(text.getText());
      }
 }

BorderLayout只能在5个可用位置中的任意位置管理单个零部件。更好的解决方案可能是使用更灵活的布局管理器,如GridBagLayoutan。另一个伟大的选择是JavaFX库。不是你问题的解决方案,但我喜欢JavaFX库。@JacobB。但是我喜欢JavaFX库,它不是推荐一个明显脱离主题的API的借口。@AndrewThompson明显脱离主题是自以为是的。我认为提供另一个可能的GUI API并不是关于GUI的问题的主题,特别是如果它只是在评论部分。如果它有助于解决问题,请。