Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中使用getSource方法?_Java_Windows_Eclipse - Fatal编程技术网

如何在Java中使用getSource方法?

如何在Java中使用getSource方法?,java,windows,eclipse,Java,Windows,Eclipse,我是一个初学者程序员,我正在从一本书中学习。我在java中使用getSource()方法时遇到问题。请看一下下面的代码,它有大量的注释来帮助您了解我的想法,而且写得很好,谢谢,这是我第一次使用stackoverFlow import javax.swing.*; import java.awt.*; import java.awt.Event; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; pu

我是一个初学者程序员,我正在从一本书中学习。我在java中使用
getSource()
方法时遇到问题。请看一下下面的代码,它有大量的注释来帮助您了解我的想法,而且写得很好,谢谢,这是我第一次使用stackoverFlow

import javax.swing.*;
import java.awt.*;
import java.awt.Event;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class EventDemo extends JFrame implements ActionListener {

    public EventDemo() //constructor
    {

        super("Sign in");    //title for JFrame

        setSize(300, 250);  //size for JFrame

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    // this make sure the JFrame close when click on exit

        JLabel username = new JLabel("Enter Username");  //promt user for username

        username.setFont(new Font("Arial", Font.BOLD, 16));  //set the font for the username label

        JTextField usernameTextField = new JTextField(20);  // where user type in username

        JLabel password = new JLabel("Enter password"); // prompt user for password

        password.setFont(new Font("Arial", Font.BOLD, 16)); // set font for password label

        JTextField passwordTextField = new JTextField(20);  // where user enter password

        JButton login = new JButton("Login");    // login button created

        JButton back = new JButton("Back");   // back button

        setLayout(new FlowLayout());     // our layout      

        setVisible(true);   // making sure we see our frame when we run our program :) i know it's better to this in main   

        login.addActionListener(this);   // telling our login button to listen for an event
        back.addActionListener(this);  // telling our back button to listen for an event

        add(username); // adding components to frame, you guys know this :)
        add(usernameTextField);
        add(password);
        add(passwordTextField);
        add(login);
        add(back);

    }

    @Override
    public void actionPerformed(ActionEvent x) // THIS IS WHERE I'M GOING CRAZY, INSIDE THE METHOD. i'm trying to see which button is causing the event. 
    {

        // this is the method the book (Java Programming Seventh Edition Joyce Farrell) taught me, and i also see this online as well, but is not working 
        Object source = x.getSource(); //no error shows up here

        if (source == login) // or if(x.getSource == login) // Eclipse is saying "login cannot be resolved to a variable"
        {
            // do this..
        } else {
            // to this.... it's only two buttons :)
        }

        // this method below i got online, but it works. But it said i have to set the buttons name after i created them see the first 4 line of code below
        JButton login = new JButton();
        JButton back = new JButton();
        login.setText("Login");
        back.setText("Back");
        // the above 4 line of code would go inside the constructor EventDemo, and not inside the actionPerformed method

        String source = x.getActionCommand();

        if (source.equals("login")) {
            // do this..
        } else {
            // do this...
        }

    }

}

Eclipse抱怨“登录无法解析”的原因是它需要一个尚未声明的登录变量。您的登录变量声明位于EventDemo构造函数中,在actionPerformed()方法中不可用。如果将声明移到构造函数之前并在构造函数内部初始化,则它不应再抱怨了

public class EventDemo extends JFrame implements ActionListener
{
   JButton login;



public EventDemo()   //constructor
{
//....
// do stuff

login = new JButton("Login");
// ...

}

您需要声明
JButton登录EventDemo
(与全局类似)中的code>。然后可以在构造函数中初始化它。这样就可以在代码中的任何位置访问
登录

另外,您碰巧声明了两次
source
objectsource=x.getSource();
String source=x.getActionCommand();
),因此请注意并更改
String source=x.getActionCommand()
to
source=x.getActionCommand()


请努力改进你的问题。你会希望这样做,因为这将提高你快速得到好答案的机会。我的建议包括:1)不要只在代码注释中输入关键信息,而是花时间输入一个写得好、清楚的问题,一个有足够背景信息的问题。2) 请努力发布格式良好的代码。如果你的代码都是左对齐的,我们就不能很好地阅读它,如果我们不能阅读它,我们就不能理解它。因此,请了解Java代码格式化标准,包括缩进标准,包括何时包含,。。。。。。以及何时避免空行。上面的代码没有任何缩进,而且使用了大量的空行,因此很难理解。事实上,如果代码缩进正确,您会立即看到您遇到了一些严重的变量范围问题--actionPerformed方法试图获取一个无法看到的变量引用,因为该变量是在类的构造函数中声明的,并且仅在构造函数中可见。您遇到了什么麻烦,错误消息是什么。非常感谢,你们是英雄。这开始阻止我学习编码。我很年轻,我还在学习,我也是一个自学成才的人。谢谢非常感谢你们,你们是英雄。这开始阻止我学习编码。我很年轻,我还在学习,我也是一个自学成才的人。谢谢欢迎@Mykey,别忘了标记为最佳答案,谢谢
   public class EventDemo extends JFrame implements ActionListener {

        JButton login;

        public EventDemo() //constructor
        {
            //....
            // do stuff

            login = new JButton("Login");
            // ...

        }

        public void actionPerformed(ActionEvent x) // THIS IS WHERE I'M GOING CRAZY, INSIDE THE METHOD. i'm trying to see which button is causing the event. 
        {
            //your code
            source = x.getActionCommand();//take out the String

            if (source.equals("login")) {
                // do this..
            } else {
                // do this...
            }

        }
    }