JLabel 不会更新它的新文本

JLabel won't update it's new Text

所以我正在创建一个计算器应用程序。单击其中一个数字按钮时,它应该出现在 JLabel 上。我已将 actionListeners 添加到我的所有按钮,当单击数字按钮时,通过 .setText() 方法更改 JLabel 的文本。出于某种原因,当我 运行 程序时,JLabel 的文本没有更新。所以我认为 JLbale 的文本值没有改变,但是当我在控制台上打印 JLabel 的文本值时,它显示已经改变了。我现在卡住了。帮助将不胜感激

 package com.Patel.APSC;

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

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

import javax.swing.UIManager;

public class Calculator extends JFrame {
    double num1 = 0;
    double num2 = 0;
    String strNum1;
    String strNum2;
    String op;
    Double answer;
    String label = "";
    private JButton btnClear;
    JLabel lblLabel = new JLabel("");

    // constructors
    public Calculator() {

        // sets up the JFrame
        this.setVisible(true);
        this.setTitle("Calculator");
        this.setResizable(false);
        this.setSize(356, 512);
        this.getContentPane().setLayout(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.getContentPane().setBackground(new Color(212, 229, 238));

        // sets up all the buttons
        JButton btn1 = new JButton("1");
        JButton btn2 = new JButton("2");
        JButton btn3 = new JButton("3");
        JButton btn4 = new JButton("4");
        JButton btn5 = new JButton("5");
        JButton btn6 = new JButton("6");
        JButton btn7 = new JButton("7");
        JButton btn8 = new JButton("8");
        JButton btn9 = new JButton("9");
        JButton btn0 = new JButton("0");
        JButton btnAdd = new JButton("+");
        JButton btnSubtract = new JButton("-");
        JButton btnMultiply = new JButton("*");
        JButton btnDivide = new JButton("/");
        JButton btnEqual = new JButton("=");
        JButton btnClear = new JButton("AC");
        btnClear.setBorder(null);

        btn1.setFont(new Font("Times New Roman", Font.PLAIN, 32));
        btn2.setFont(new Font("Times New Roman", Font.PLAIN, 32));
        btn3.setFont(new Font("Times New Roman", Font.PLAIN, 32));
        btn4.setFont(new Font("Times New Roman", Font.PLAIN, 32));
        btn5.setFont(new Font("Times New Roman", Font.PLAIN, 32));
        btn6.setFont(new Font("Times New Roman", Font.PLAIN, 32));
        btn7.setFont(new Font("Times New Roman", Font.PLAIN, 32));
        btn8.setFont(new Font("Times New Roman", Font.PLAIN, 32));
        btn9.setFont(new Font("Times New Roman", Font.PLAIN, 32));
        btn0.setFont(new Font("Times New Roman", Font.PLAIN, 32));
        btnAdd.setFont(new Font("Mongolian Baiti", Font.PLAIN, 32));
        btnSubtract.setFont(new Font("Mongolian Baiti", Font.PLAIN, 32));
        btnMultiply.setFont(new Font("Mongolian Baiti", Font.PLAIN, 32));
        btnDivide.setFont(new Font("Mongolian Baiti", Font.PLAIN, 32));
        btnEqual.setFont(new Font("Mongolian Baiti", Font.PLAIN, 32));
        btnClear.setFont(new Font("Times New Roman", Font.PLAIN, 32));

        btn1.setLocation(28, 159);
        btn2.setLocation(98, 159);
        btn3.setLocation(168, 159);
        btn4.setLocation(28, 229);
        btn5.setLocation(98, 229);
        btn6.setLocation(168, 229);
        btn7.setLocation(28, 299);
        btn8.setLocation(98, 299);
        btn9.setLocation(168, 299);
        btn0.setLocation(98, 369);
        btnAdd.setLocation(265, 369);
        btnSubtract.setLocation(265, 299);
        btnMultiply.setLocation(265, 229);
        btnDivide.setLocation(265, 159);
        btnEqual.setLocation(265, 93);
        btnClear.setBounds(163, 369, 55, 55);
        btn1.setSize(55, 55);
        btn2.setSize(55, 55);
        btn3.setSize(55, 55);
        btn4.setSize(55, 55);
        btn5.setSize(55, 55);
        btn6.setSize(55, 55);
        btn7.setSize(55, 55);
        btn8.setSize(55, 55);
        btn9.setSize(55, 55);
        btn0.setSize(55, 55);
        btnAdd.setSize(55, 55);
        btnSubtract.setSize(55, 55);
        btnMultiply.setSize(55, 55);
        btnDivide.setSize(55, 55);
        btnEqual.setSize(55, 55);

        btn0.setActionCommand("0");
        btn1.setActionCommand("1");
        btn2.setActionCommand("2");
        btn3.setActionCommand("3");
        btn4.setActionCommand("4");
        btn5.setActionCommand("5");
        btn6.setActionCommand("6");
        btn7.setActionCommand("7");
        btn8.setActionCommand("8");
        btn9.setActionCommand("9");
        btnAdd.setActionCommand("+");
        btnSubtract.setActionCommand("-");
        btnMultiply.setActionCommand("*");

        ButtonListener listener = new ButtonListener();
        btn1.addActionListener(listener);
        btn2.addActionListener(listener);
        btn3.addActionListener(listener);
        btn4.addActionListener(listener);
        btn5.addActionListener(listener);
        btn6.addActionListener(listener);
        btn7.addActionListener(listener);
        btn8.addActionListener(listener);
        btn9.addActionListener(listener);
        btn0.addActionListener(listener);
        btnAdd.addActionListener(listener);
        btnSubtract.addActionListener(listener);
        btnMultiply.addActionListener(listener);
        btnDivide.addActionListener(listener);
        btnClear.addActionListener(listener);
        btnEqual.addActionListener(listener);

        this.getContentPane().add(btn0);
        this.getContentPane().add(btn1);
        this.getContentPane().add(btn2);
        this.getContentPane().add(btn3);
        this.getContentPane().add(btn4);
        this.getContentPane().add(btn5);
        this.getContentPane().add(btn6);
        this.getContentPane().add(btn7);
        this.getContentPane().add(btn8);
        this.getContentPane().add(btn9);
        this.getContentPane().add(btnAdd);
        this.getContentPane().add(btnSubtract);
        this.getContentPane().add(btnMultiply);
        this.getContentPane().add(btnDivide);
        this.getContentPane().add(btnEqual);
        this.getContentPane().add(btnClear);

        JLabel lblLabel = new JLabel("");

        lblLabel.setFont(new Font("Times New Roman", Font.PLAIN, 27));

        this.getContentPane().add(lblLabel);
        lblLabel.setLocation(43, 31);
        lblLabel.setSize(277, 51);
        lblLabel.setOpaque(true);

    }

    public static void main(String[] args) {
        Calculator gui = new Calculator();

    }

    public class ButtonListener implements ActionListener {


        public void actionPerformed(ActionEvent e) {

            if (e.getActionCommand().equals("1")
                    || e.getActionCommand().equals("2")
                    || e.getActionCommand().equals("3")
                    || e.getActionCommand().equals("4")
                    || e.getActionCommand().equals("5")
                    || e.getActionCommand().equals("6")
                    || e.getActionCommand().equals("7")
                    || e.getActionCommand().equals("8")
                    || e.getActionCommand().equals("9")
                    || e.getActionCommand().equals("0")) {
                // if a number is typed
                lblLabel.setText(e.getActionCommand());
                System.out.println(lblLabel.getText());
            }

        }



    }
}

您正在调用 text.setText(e.getActionCommand()) -- 但这个 text 字段是什么?调用的不是您的 JLabel lblLabel,事实上,我在您的程序中找不到 text 字段,因此令我惊讶的是 class 甚至可以编译。

我相信在正确的 JLabel 字段上调用 ​​setText 会更好:lblLabel.setText(e.getActionCommand())

侧面建议:

  • 不必要的重复往往会导致错误难以发现,因此您需要使用数组或集合以及 for 循环来整合您的代码。
  • 您的直觉可能是使用 null 布局并在您的组件上调用 setBounds(...) 以绝对定位放置它们,但我建议您不要这样做,因为这会使对于非常不灵活的 GUI,虽然它们在一个平台上看起来不错,但在大多数其他平台或屏幕分辨率上看起来很糟糕,而且很难更新和维护。相反,您需要研究和学习布局管理器,然后嵌套 JPanel,每个 JPanel 都使用自己的布局管理器来创建在所有 OS 上看起来都不错的令人愉悦的复杂 GUI。
  • 你最后一个大的if块可以压缩到if ("1234567890".contains(e.getActionCommand()) {

编辑
你的问题的编辑改变了一切。请避免这些类型的更改,因为它们会让我们的志愿者感到非常沮丧。

现在我看到您通过在 两次 中声明 lblLbl 变量来隐藏它,一次在 class 中一次在构造函数中,这意味着 class 的字段不是正在显示的 JLabel。解决方法:只声明变量一次.

所以改变

public class Calculator {
    JLabel lblLabel = new JLabel("");

    public Calculator() {
        JLabel lblLabel = new JLabel("");
        // ...
    }
}

至:

public class Calculator {
    JLabel lblLabel = new JLabel("");

    public Calculator() {
        //  JLabel lblLabel = new JLabel("");  // don't re-declare
        // ...
    }
}