使用 GUI 简单对齐的快速建议?

Quick Advice with GUI simple alignment?

我是 Java 的新手,我们接到了任务。我必须创建一个数学框架来执行某些操作并以某种方式格式化 GUI。除了将三个文本字段堆叠在顶部之外,我可以做任何事情。像这样:

t1
t2
t3 

这三个在左边垂直向下,而其他 4 个盒子在底部。

其中t1、t2、t3为文本框,1、2、3、4为操作函数。

我无法让 x、y 和 z 位于左侧。这是我的代码,如果可以请帮忙!

ConvertFrame.java:

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class ConvertFrame extends JFrame {
    private final JLabel prompt; // label to prompt user to enter Fahrenheit
    private final JLabel display; // label to display temperature in Celsius
    private final JTextField temperatureF; // textfield to enter temperature
    private JButton b1;
    private JButton b2;
    private JButton b3;
    private JButton b4;
    private JTextField t1;
    private JTextField t2;
    private JTextField t3;

    private JLabel operation;
    private JLabel equL;

    // constructor sets up GUI
    public ConvertFrame() {
        super("Math Frame");

        prompt = new JLabel("Enter two numbers:");

        operation = new JLabel("        OPR ");
        equL = new JLabel(" = ");

        temperatureF = new JTextField(10); // textfield
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(1, 3));

        JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayout(1, 5));

        b1 = new JButton("ADD");
        b2 = new JButton("SUB");
        b3 = new JButton("MUL");
        b4 = new JButton("CLEAR");

        t1 = new JTextField(7);
        t2 = new JTextField(7);
        t3 = new JTextField(7);

        panel2.add(t1);
        panel2.add(operation);
        panel2.add(t2);
        panel2.add(equL);
        panel2.add(t3);

        panel.add(b1);
        panel.add(b2);
        panel.add(b3);
        panel.add(b4);
        // register anonymous action listener

        Handler1 h1 = new Handler1();
        Handler2 h2 = new Handler2();
        Handler3 h3 = new Handler3();
        Handler4 h4 = new Handler4();

        b1.addActionListener(h1);
        b2.addActionListener(h2);
        b3.addActionListener(h3);
        b4.addActionListener(h4);

        display = new JLabel("");

        add(prompt, BorderLayout.NORTH); // north region
        add(panel2, BorderLayout.CENTER); // center region
        add(panel, BorderLayout.SOUTH); // south region
    } // end ConvertFrame constructor

    private class Handler1 implements ActionListener // anonymous inner class
    {
        public void actionPerformed(ActionEvent e) {
            String st1 = t1.getText();
            String st2 = t2.getText();
            int x = Integer.parseInt(st1);
            int y = Integer.parseInt(st2);
            int z = x + y;
            String st3 = "";
            st3 = st3 + z;
            t3.setText(st3);

        }
    } //

    private class Handler2 implements ActionListener // anonymous inner class
    {
        public void actionPerformed(ActionEvent e) {
            String st1 = t1.getText();
            String st2 = t2.getText();
            int x = Integer.parseInt(st1);
            int y = Integer.parseInt(st2);
            int z = x - y;
            String st3 = "";
            st3 = st3 + z;
            t3.setText(st3);

        }
    } // end anonymous inner class

    private class Handler3 implements ActionListener // anonymous inner class
    {
        public void actionPerformed(ActionEvent e) {
            String st1 = t1.getText();
            String st2 = t2.getText();
            int x = Integer.parseInt(st1);
            int y = Integer.parseInt(st2);
            int z = x * y;
            String st3 = "";
            st3 = Integer.toString(z);

            t3.setText(st3);
        }
    } // end anonymous inner class

    private class Handler4 implements ActionListener // anonymous inner class
    {
        public void actionPerformed(ActionEvent e) {
            t1.setText("");
            t2.setText("");
            t3.setText("");
        }
    } // end anonymous inner class

    // end call to addActionListener
} // end class ConvertFrame

Convert.java:

import javax.swing.JFrame;

public class Convert {
    public static void main(String[] args) {
        ConvertFrame convertFrame = new ConvertFrame();
        convertFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        convertFrame.setSize(500, 400); // set frame size
        convertFrame.setVisible(true); // display frame
    }
} // end class

如果你想在顶部堆叠文本字段

你应该改变这个

panel2.setLayout(new GridLayout(1, 5));

至此

panel2.setLayout(new GridLayout(5, 1));

因为在 new GridLayout(row, column) 中,第一个参数是行数,下一个参数是列数。目前你有 1 行和 5 个 columns.so 文本字段定位为 coulmns。如果你想垂直定位,那么你应该使用 1 列和 5 行

示例代码:

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class ConvertFrame extends JFrame {

    // Label to prompt user to enter Fahrenheit
    private final JLabel prompt;

    // Label to display temperature in Celsius
    private final JLabel display;

/*  // Textfield to enter temperature
    private final JTextField temperatureF;*/

    // JButtons
    private JButton b1;
    private JButton b2;
    private JButton b3;
    private JButton b4;

    // JTextFields
    private JTextField t1;
    private JTextField t2;
    private JTextField t3;

    private JLabel operation;
    private JLabel equL;

    // constructor sets up GUI
    public ConvertFrame() {
        super("Math Frame");

        prompt = new JLabel("Enter two numbers:");

        operation = new JLabel("OPR");
        equL = new JLabel("=");

        b1 = new JButton("ADD");
        b2 = new JButton("SUB");
        b3 = new JButton("MUL");
        b4 = new JButton("CLEAR");

        t1 = new JTextField(7);
        t2 = new JTextField(7);
        t3 = new JTextField(7);

        // Using GridBagLayout
        JPanel layoutPanel = new JPanel(new GridBagLayout());

        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 4;
        c.anchor = GridBagConstraints.CENTER;
        c.insets = new Insets(10, 0, 20, 0);
        layoutPanel.add(prompt, c);

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 4;
        c.ipadx = 20;
        c.anchor = GridBagConstraints.CENTER;
        c.insets = new Insets(10, 0, 10, 0);
        layoutPanel.add(t1, c);

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 4;
        c.anchor = GridBagConstraints.CENTER;
        layoutPanel.add(operation, c);

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth = 4;
        c.ipadx = 20;
        c.anchor = GridBagConstraints.CENTER;
        c.insets = new Insets(10, 0, 10, 0);
        layoutPanel.add(t2, c);

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 4;
        c.gridwidth = 4;
        c.anchor = GridBagConstraints.CENTER;
        layoutPanel.add(equL, c);

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 5;
        c.gridwidth = 4;
        c.ipadx = 20;
        c.insets = new Insets(10, 0, 10, 0);
        c.anchor = GridBagConstraints.CENTER;
        layoutPanel.add(t3, c);

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 6;
        c.insets = new Insets(30, 5, 10, 5);
        c.anchor = GridBagConstraints.CENTER;
        layoutPanel.add(b1, c);

        c = new GridBagConstraints();
        c.gridx = 1;
        c.gridy = 6;
        c.insets = new Insets(30, 5, 10, 5);
        c.anchor = GridBagConstraints.CENTER;
        layoutPanel.add(b2, c);

        c = new GridBagConstraints();
        c.gridx = 2;
        c.gridy = 6;
        c.insets = new Insets(30, 5, 10, 5);
        c.anchor = GridBagConstraints.CENTER;
        layoutPanel.add(b3, c);

        c = new GridBagConstraints();
        c.gridx = 3;
        c.gridy = 6;
        c.insets = new Insets(30, 5, 10, 5);
        c.anchor = GridBagConstraints.CENTER;
        layoutPanel.add(b4, c);

        Handler1 h1 = new Handler1();
        Handler2 h2 = new Handler2();
        Handler3 h3 = new Handler3();
        Handler4 h4 = new Handler4();

        b1.addActionListener(h1);
        b2.addActionListener(h2);
        b3.addActionListener(h3);
        b4.addActionListener(h4);

        display = new JLabel("");

        add(layoutPanel, BorderLayout.CENTER); // center region

    } // end ConvertFrame constructor

    private class Handler1 implements ActionListener // anonymous inner class
    {
        public void actionPerformed(ActionEvent e) {
            String st1 = t1.getText();
            String st2 = t2.getText();
            int x = Integer.parseInt(st1);
            int y = Integer.parseInt(st2);
            int z = x + y;
            String st3 = "";
            st3 = st3 + z;
            t3.setText(st3);

            operation.setText("+");
        }
    } //

    private class Handler2 implements ActionListener // anonymous inner class
    {
        public void actionPerformed(ActionEvent e) {
            String st1 = t1.getText();
            String st2 = t2.getText();
            int x = Integer.parseInt(st1);
            int y = Integer.parseInt(st2);
            int z = x - y;
            String st3 = "";
            st3 = st3 + z;
            t3.setText(st3);

            operation.setText("-");
        }
    } // end anonymous inner class

    private class Handler3 implements ActionListener // anonymous inner class
    {
        public void actionPerformed(ActionEvent e) {
            String st1 = t1.getText();
            String st2 = t2.getText();
            int x = Integer.parseInt(st1);
            int y = Integer.parseInt(st2);
            int z = x * y;
            String st3 = "";
            st3 = Integer.toString(z);

            t3.setText(st3);

            operation.setText("*");
        }
    } // end anonymous inner class

    private class Handler4 implements ActionListener // anonymous inner class
    {
        public void actionPerformed(ActionEvent e) {
            t1.setText("");
            t2.setText("");
            t3.setText("");

            operation.setText("OPR");
        }
    } // end anonymous inner class

    public static void main(String[] args) {
        ConvertFrame convertFrame = new ConvertFrame();
        convertFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        convertFrame.setSize(500, 400); // set frame size
        convertFrame.setVisible(true); // display frame
    }

} // end class ConvertFrame

输出:

注:

  • 使用 GridBagLayout,这是一个更加灵活的布局管理器。
  • 使用 GridBagConstraints
  • 的不同属性可以轻松完成组件的定位和调整大小
  • c.gridwidth = 4;前6个分量可以用c.gridwidth = GridBagConstraints.REMAINDER;
  • 代替