简体   繁体   English

JLabel不会更新它的新文本

[英]JLabel won't update it's new Text

So I am creating a calculator app. 所以我正在创建一个计算器应用程序。 When one of the number buttons is clicked, it is supposed to appear on the JLabel. 单击数字按钮之一时,它应该出现在JLabel上。 I have added actionListeners to all of my buttons, and when a number button is clicked, the JLabel's text is changed, via the method .setText(). 我已将actionListeners添加到所有按钮,单击数字按钮后,将通过.setText()方法更改JLabel的文本。 For some reason when i run the program, the JLabel's text doesn't update. 由于某些原因,当我运行该程序时,JLabel的文本不会更新。 So i thought that the text value of the JLbale wasn't being changed, yet when I print the text value of the JLabel on the console, its shows that is has changed. 因此,我认为JLbale的文本值没有更改,但是当我在控制台上打印JLabel的文本值时,它的显示已更改。 I'm an now stuck. 我现在陷入困境。 Help would be appreciated 帮助将不胜感激

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

        }



    }
}

You're calling text.setText(e.getActionCommand()) -- but what is this text field? 您正在调用text.setText(e.getActionCommand()) -但是此文text字段是什么? It's not your JLabel which is called, lblLabel , and in fact, I can't find a text field within your program, and so I'm surprised that the class even compiles. 不是您的JLabel被称为lblLabel ,实际上,我在您的程序中找不到text字段,因此让类甚至编译都感到惊讶。

I believe that you'll be much better off calling setText on the correct JLabel field: lblLabel.setText(e.getActionCommand()) 我相信您可以在正确的JLabel字段上调用setText更好: lblLabel.setText(e.getActionCommand())

Side recommendations: 侧面建议:

  • Needless repetition often leads to hard to find bugs, and so you'll want to use arrays or collections and for loops to consolidate your code. 不必要的重复通常会导致难以发现错误,因此您将需要使用数组或集合以及for循环来合并代码。
  • Your gut instinct may be to use a null layout and calling setBounds(...) on your components to place them with absolute positioning, but I'm going suggest that you not do this as this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain. 您的直觉可能是使用null布局并在组件上调用setBounds(...)将它们放置在绝对位置,但是我建议您不要这样做,因为这会导致非常不灵活的GUI,尽管它们可能在一个平台上看起来不错在大多数其他平台或屏幕分辨率上看起来很糟糕,并且很难更新和维护。 Instead you will want to study and learn the layout managers and then nest JPanels, each using its own layout manager to create pleasing and complex GUI's that look good on all OS's. 相反,您将需要学习和学习布局管理器,然后嵌套JPanels,每个JPanels都使用其自己的布局管理器来创建令人愉悦且复杂的GUI,在所有OS上都看起来不错。
  • Your last large if block can be compressed to if ("1234567890".contains(e.getActionCommand()) { 您可以将最后一个大的if块压缩为if ("1234567890".contains(e.getActionCommand()) {

Edit 编辑
Your question's edit changes everything. 您问题的编辑会改变一切。 Please avoid these types of changes as they can be very frustrating to us volunteers. 请避免这些类型的更改,因为它们会使我们的志愿者非常沮丧。

Now I see that you're shadowing the lblLbl variable by declaring it twice , once in the class and once in the constructor, meaning that the class's field is not the JLabel that is being displayed. 现在,我看到您通过两次声明lblLbl变量(一次在类中,一次在构造函数中)对其进行了阴影处理,这意味着该类的字段不是正在显示的JLabel。 Solution: declare the variable only once . 解决方案:仅声明一次变量。

so change 所以改变

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

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

to: 至:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM