如何使用 Java Swing 中的按钮从文本字段获取文本,然后将其添加到面板?

How to get text from a textfield using a button in Java Swing, then add it to the panel?

我开发一个带有用户界面的计算器总共用了大约 2 天,正要完成它,直到我的进度被一个错误突然打断了(不完全是错误)。

这是我的代码

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Objects;

public class GUI implements ActionListener {

    double solution = 0;
    JButton button;
    JLabel answerLabel;
    JPanel panel;
    JTextField firstNumber;
    JTextField operator;
    JTextField secondNumber;

    public GUI(){
        JFrame frame = new JFrame();

        button = new JButton("Solve");
        button.addActionListener(this);

        JLabel label = new JLabel("Welcome to Calculator");
        label.setFont(new Font("Consolas", Font.BOLD, 30));

        JLabel enterFirstNumber = new JLabel("Enter first number:");
        enterFirstNumber.setFont(new Font("Consolas", Font.BOLD, 20));

        JLabel enterOperator = new JLabel("Enter your operator(+, -, *, /):");
        enterOperator.setFont(new Font("Consolas", Font.BOLD, 20));

        JLabel enterSecondNumber = new JLabel("Enter second number");
        enterSecondNumber.setFont(new Font("Consolas", Font.BOLD, 20));

        firstNumber = new JTextField();
        firstNumber.setFont(new Font("Consolas", Font.BOLD, 35));

        operator = new JTextField();
        operator.setFont(new Font("Consolas", Font.BOLD, 35));

        secondNumber = new JTextField();
        secondNumber.setFont(new Font("Consolas", Font.BOLD, 35));

        panel = new JPanel();
        panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
        panel.setLayout(new GridLayout(0, 1));
        panel.add(label);
        panel.add(enterFirstNumber);
        panel.add(firstNumber);
        panel.add(enterOperator);
        panel.add(operator);
        panel.add(enterSecondNumber);
        panel.add(secondNumber);
        panel.add(button);

        frame.add(panel, BorderLayout.CENTER);
        frame.setSize(450, 450);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Calculator");
        frame.setVisible(true);
        frame.setResizable(false);

        double firstNumberValue = Double.parseDouble(firstNumber.getText());
        String operatorChoice = operator.getText();
        double secondNumberValue = Double.parseDouble(secondNumber.getText());
        System.out.println(firstNumberValue + secondNumberValue + operatorChoice);
    }

    public static void main(String[] args) {
        new GUI();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        double firstTextBox = Double.parseDouble(firstNumber.getText());
        String operatorValue = operator.getText();
        double secondTextBox = Double.parseDouble(secondNumber.getText());

        if (Objects.equals(operatorValue, "+")) {
            solution = firstTextBox+secondTextBox;
        }
        if (Objects.equals(operatorValue, "-")) {
            solution = firstTextBox+secondTextBox;
        }
        if (Objects.equals(operatorValue, "*")) {
            solution = firstTextBox*secondTextBox;
        }
        if (Objects.equals(operatorValue, "/")) {
            solution = firstTextBox/secondTextBox;
        }
        answerLabel = new JLabel(String.valueOf(solution));
        panel.add(answerLabel);
    }
}

最后,我添加了一个使用 panel.add(anwerLabel); 执行的“方法”操作。但是,这不起作用。

除此之外,如果有人能指出我代码中的其他冗余,那将会有所帮助,因为我最近开始学习 Java。

谢谢。

下面的代码实现了@AndrewThompson 在他的 中的建议,即

Since a label with no text or icon is invisible, I'd add the label to the GUI at start-up and simply change the state (in this case the text) when the action listener fires.

以下代码与您问题中的代码相同,但有以下更改:

  • 我在构造函数(class GUI)中初始化 answerLabel
  • 我在方法 actionPerformed 中设置了 answerLabel 的文本。
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Objects;

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

public class GUI implements ActionListener {

    double solution = 0;
    JButton button;
    JLabel answerLabel;
    JPanel panel;
    JTextField firstNumber;
    JTextField operator;
    JTextField secondNumber;

    public GUI() {
        JFrame frame = new JFrame();

        button = new JButton("Solve");
        button.addActionListener(this);

        JLabel label = new JLabel("Welcome to Calculator");
        label.setFont(new Font("Consolas", Font.BOLD, 30));

        JLabel enterFirstNumber = new JLabel("Enter first number:");
        enterFirstNumber.setFont(new Font("Consolas", Font.BOLD, 20));

        JLabel enterOperator = new JLabel("Enter your operator(+, -, *, /):");
        enterOperator.setFont(new Font("Consolas", Font.BOLD, 20));

        JLabel enterSecondNumber = new JLabel("Enter second number");
        enterSecondNumber.setFont(new Font("Consolas", Font.BOLD, 20));

        firstNumber = new JTextField();
        firstNumber.setFont(new Font("Consolas", Font.BOLD, 35));

        operator = new JTextField();
        operator.setFont(new Font("Consolas", Font.BOLD, 35));

        secondNumber = new JTextField();
        secondNumber.setFont(new Font("Consolas", Font.BOLD, 35));

        panel = new JPanel();
        panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
        panel.setLayout(new GridLayout(0, 1));
        panel.add(label);
        panel.add(enterFirstNumber);
        panel.add(firstNumber);
        panel.add(enterOperator);
        panel.add(operator);
        panel.add(enterSecondNumber);
        panel.add(secondNumber);
        answerLabel = new JLabel("       "); // Added this line.
        panel.add(answerLabel); // Added this line.
        panel.add(button);

        frame.add(panel, BorderLayout.CENTER);
        frame.setSize(450, 450);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Calculator");
        frame.setVisible(true);
        frame.setResizable(false);
    }

    public static void main(String[] args) {
        new GUI();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        double firstTextBox = Double.parseDouble(firstNumber.getText());
        String operatorValue = operator.getText();
        double secondTextBox = Double.parseDouble(secondNumber.getText());

        if (Objects.equals(operatorValue, "+")) {
            solution = firstTextBox + secondTextBox;
        }
        if (Objects.equals(operatorValue, "-")) {
            solution = firstTextBox + secondTextBox;
        }
        if (Objects.equals(operatorValue, "*")) {
            solution = firstTextBox * secondTextBox;
        }
        if (Objects.equals(operatorValue, "/")) {
            solution = firstTextBox / secondTextBox;
        }
        answerLabel.setText(String.valueOf(solution));
    }
}

下面是我 运行 上述代码时的样子: