JLabel不会更新它的新文本

时间:2015-09-11 23:15:17

标签: java swing actionlistener calculator

所以我正在创建一个计算器应用程序。单击其中一个数字按钮时,它应该出现在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());
            }

        }



    }
}

1 个答案:

答案 0 :(得分:3)

您正在呼叫text.setText(e.getActionCommand()) - 但这个text字段是什么?这不是你的JLabel被称为lblLabel,事实上,我在你的程序中找不到text字段,所以我很惊讶该课程甚至编译。

我相信你在正确的JLabel字段上调用setText要好得多:lblLabel.setText(e.getActionCommand())

侧面建议:

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

修改
您的问题编辑会更改所有内容。请避免这些类型的更改,因为它们对我们的志愿者来说非常令人沮丧。

现在我看到你通过声明两次来隐藏lblLbl变量,一次在类中,一次在构造函数中,这意味着类的字段不是正在显示的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
        // ...
    }
}