简体   繁体   English

如何执行在Button上执行的操作

[英]How to implement actionperformed on Button

I am implementing a JButton to perform an action but it's giving me an error. 我正在实现一个JButton来执行操作,但这给我一个错误。 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. 编译器要求我创建一个局部变量调用txtDisplay并将其声明为null ,但是当我这样做时,它没有运行。 thanks 谢谢

Declare txtDisplay as field, it seems you declared it as local variable. txtDisplay声明为字段,似乎您txtDisplay其声明为局部变量。 I suppose that's why you can't get an access to it. 我想这就是为什么您无法访问它的原因。

Your button7 variable is declared as a field, but txtDisplay not. 您的button7变量声明为字段,但txtDisplay不声明。

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

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

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