简体   繁体   中英

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. 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(). For some reason when i run the program, the JLabel's text doesn't update. 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. 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? 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.

I believe that you'll be much better off calling setText on the correct JLabel field: 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.
  • 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. 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.
  • Your last large if block can be compressed to 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. 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
        // ...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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