【问题标题】:Java Swing aligning buttons, labels, and text fieldsJava Swing 对齐按钮、标签和文本字段
【发布时间】:2018-05-22 04:50:25
【问题描述】:

我正在尝试将我的 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());
}

}

【问题讨论】:

  • BorderLayout 只能在 5 个可用位置中的任何位置管理单个组件。更好的解决方案可能是使用更灵活的布局管理器,例如 GridBagLayout
  • 另一个不错的选择是 JavaFX 库。不能解决您的问题,但我喜欢 JavaFX 库。
  • @JacobB。 “但我喜欢 JavaFX 库” 这不是推荐明显偏离主题的 API 的借口。
  • @AndrewThompson “明显偏离主题”是固执己见的。我认为提供另一个可能的 GUI API 并不是关于 GUI 的问题,特别是如果它只在 cmets 部分中。

标签: java swing alignment layout-manager


【解决方案1】:

参见代码 cmets 中的注释。

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

【讨论】:

【解决方案2】:

不需要为按钮或文本设置任何流布局以放置在中心,因为面板默认具有“FlowLayout center”,它将组件放置在中心 并且框架默认具有“BorderLayout”
要将按钮和文本区域放在底部,您只需将它们都添加到面板中(您已经完成),然后将面板添加到框架中,参数为 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());
      }
 }

【讨论】:

    猜你喜欢
    • 2014-06-09
    • 1970-01-01
    • 2013-09-03
    • 2020-03-12
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    • 2020-01-27
    • 2017-08-29
    相关资源
    最近更新 更多