我是一名初学程序员,我正在从一本书中学习。而且我在java中使用getSource()
方法时遇到了麻烦。请看下面的代码,它有很多评论可以帮助您了解我的想法,并且写得很好谢谢,这是我第一次使用计算器。
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...
}
}
}
答案 0 :(得分:1)
Eclipse抱怨的原因"登录无法解决"是因为它需要一个尚未声明的登录变量。您的登录变量声明在EventDemo构造函数中,并且在actionPerformed()方法中不可用。如果在构造函数之前移动声明并在构造函数内部初始化,则不应再抱怨。
public class EventDemo extends JFrame implements ActionListener
{
JButton login;
public EventDemo() //constructor
{
//....
// do stuff
login = new JButton("Login");
// ...
}
答案 1 :(得分:0)
您需要在构造函数之前在类JButton login;
中声明EventDemo
(如全局)。然后你可以在构造函数中初始化它。这样就可以在代码中的任何位置访问login
。
此外,您恰好已声明source
两次(Object source = x.getSource();
和String source = x.getActionCommand();
),因此请注意并将String source = x.getActionCommand();
更改为source = x.getActionCommand();
。
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...
}
}
}