Bmc243
Bmc243

Reputation: 33

How to implement actionperformed on Button

I am implementing a JButton to perform an action but it's giving me an error. on my code i wrote

String EnterNumber = txtDisplay.getText() + Jbutton7.getText(); 
txtDisplay.setText(EnterNumbet) ; 

the compiler is asking me to create a local variable call txtDisplay and declare it to null, but when i do it, it's not running. thanks

Upvotes: 0

Views: 63

Answers (1)

J Clean
J Clean

Reputation: 56

Declare txtDisplay as field, it seems you declared it as local variable. I suppose that's why you can't get an access to it.

Your button7 variable is declared as a field, but txtDisplay not.

Here is an example how should it be:

import javax.swing.*;

public class SomeClass {

    private JButton jButton;
    private JTextField txtDisplay;

    public SomeClass() {
        jButton = new JButton("7");
        txtDisplay = new JTextField();
    }
}

Upvotes: 1

Related Questions