简体   繁体   English

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

[英]Java Swing aligning buttons, labels, and text fields

I am trying to align my JButton and JTextArea to the bottom middle of my code, side by side. 我试图将我的JButton和JTextArea并排对齐到代码的底部中间。 I followed a few tutorials and came to this point. 我遵循了一些教程,然后到了这一点。 When I execute my program, the text area and the button are still both aligned at the top. 当我执行程序时,文本区域和按钮仍然在顶部对齐。 Help! 救命!

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());
}

}

See the notes in the code comments. 请参阅代码注释中的注释。

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();
    }
}

There is no need of setting any flow layout to button or text for placing in center because panel has by default "FlowLayout center" which will place the components in center And frame has by default "BorderLayout" 不需要将任何流布局设置为按钮或文本以放置在中心,因为面板默认情况下具有“ FlowLayout中心”,它将组件放置在中心,而框架默认具有“ BorderLayout”
To place the button and textarea at the bottom you only need to add both of them in panel(which you already done) and then add panel to frame with parameter as BorderLayout.SOUTH Seee the modified code below it will work as per your need 要将按钮和文本区域放置在底部,您只需要将两个按钮和文本区域都添加到面板中(您已经完成),然后将面板添加到框架中,参数为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());
      }
 }

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

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