简体   繁体   English

如何处理Java中的事件?

[英]How to Handle events in Java?

I just made my first event based GUI in java, but i don't understand where i am wrong. 我刚刚在java中创建了我的第一个基于事件的GUI,但我不明白我错在哪里。 the code worked fine when there was no event handling applied.. 当没有应用事件处理时,代码工作正常。

Here's the code. 这是代码。

package javaapplication1;

import javax.swing.*;
import java.awt.event.*;

 class Elem implements ActionListener{

    void perform(){
        JFrame frame = new JFrame();
        JButton button;
        button = new JButton("Button");
        frame.getContentPane().add(button) ;
        frame.setSize(300,300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        button.addActionListener(this);
     }

    public void actionPerformed(ActionEvent ev){
        button.setText("Clicked");
    }
}

 public class JavaApplication1 {

  public static void main(String[] args) {
   Elem obj = new Elem();
   obj.perform();
  }  
}

As you used button object inside actionPerformed method. 正如您在actionPerformed方法中使用了button对象。 So declare button globally. 所以全局声明button

class Elem implements ActionListener{
     JButton button;// Declare JButton here.
    void perform(){
        JFrame frame = new JFrame();

        button = new JButton("Button");
        frame.getContentPane().add(button) ;
        frame.setSize(300,300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        button.addActionListener(this);
     }

    public void actionPerformed(ActionEvent ev){
        button.setText("Clicked");
    }
}

 public class JavaApplication1 {

  public static void main(String[] args) {
   Elem obj = new Elem();
   obj.perform();
  }  
}

There is a problem with variable scope. 变量范围存在问题。 Move the JButton button; 移动JButton button; definition outside of the perform() method so that actionPerformed() can access it. perform()方法之外的定义,以便actionPerformed()可以访问它。

JButton button;

void perform(){
    JFrame frame = new JFrame();
    button = new JButton("Button");
    frame.getContentPane().add(button) ;
    frame.setSize(300,300);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    button.addActionListener(this);
 }

When you define an object inside of a method (eg perform() ), its scope is limited to that method (inside the curly braces). 当您在方法内部定义对象(例如perform() )时,其范围仅限于该方法(在花括号内)。 This means that other methods inside your class cannot access that variable. 这意味着类中的其他方法无法访问该变量。

By moving the object definition outside of the method, it now has class level scope. 通过将对象定义移到方法之外,它现在具有类级别范围。 This means that any method within that class can access the variable. 这意味着该类中的任何方法都可以访问该变量。 You're still defining its value inside of perform() , but now it can be accessed by other methods, including actionPerformed() . 您仍然在perform()定义其值,但现在可以通过其他方法访问它,包括actionPerformed()

See here for more explanation. 请参阅此处以获取更多解释

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

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