Java 如何实现ActionListener

Java 如何实现ActionListener,java,actionlistener,Java,Actionlistener,我试图在单击按钮时简单地更改JLabel的值 这是GUI代码: import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.Border; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class login extends JFrame implements

我试图在单击按钮时简单地更改JLabel的值

这是GUI代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class login extends JFrame implements ActionListener
{
       public login (){

         //Create (Frame, Label, Text input for doctors name & Password field):

       JFrame frame = new JFrame("Doctor Login");
       JLabel label = new JLabel("Login Below:");
       JTextField name = new JTextField(10);
       JPasswordField pass = new JPasswordField(10);
       JButton button = new JButton("Login");

        //Exit program on close:
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       //Label
       label.setPreferredSize(new Dimension(175, 100));
       label.setLocation(100,100);

       //Add Elements to frame:
       frame.setLayout(new FlowLayout());
       frame.getContentPane().add(label);
       frame.getContentPane().add(name);
       frame.getContentPane().add(pass);
       frame.getContentPane().add(button);

       //Create border for text field:
       Border nameBorder = BorderFactory.createLineBorder(Color.BLACK, 1);

       name.setBorder(nameBorder);
       pass.setBorder(nameBorder);

       //Set Size of frame:
       frame.setSize(500, 250);

       //Set Location of the frame on the screen:
       frame.setLocation(200,200);

       //Display
       frame.setVisible(true);


       //Compiler Gives an error here - Illegal Start of Expression
       public void actionEvent(ActionEvent event){
           label.setText("Logged in");
       }
    }

}
主要类别代码:

import java.util.*;
import java.util.Date;

public class main
{

public static void main(String[] args) {

   login login = new login();

}


}
login类中的actionEvent方法返回错误“表达式的非法开始”。

您将actionPerformed方法放错位置并命名错误。现在,它嵌套在构造函数中,Java不允许将方法嵌套在其他方法或构造函数中,这与不允许将构造函数嵌套在方法或构造函数中的构造函数相同。将它移出构造函数,调用actionPerformedActionEvent e,然后看看会发生什么。

您将actionPerformed方法放错了位置并命名错误。现在,它嵌套在构造函数中,Java不允许将方法嵌套在其他方法或构造函数中,这与不允许将构造函数嵌套在方法或构造函数中的构造函数相同。将它移出构造函数,调用actionPerformedActionEvent e,看看会发生什么。

如果您只想将ActionListener添加到按钮中,那么应该删除它

implements ActionListener
并且做:

button.addActionListener(new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        label.setText(...);
    }
});
注:

请注意,您得到该错误是因为您在构造函数中添加了该方法。 您应该遵循Java命名约定,为类使用SomeClass,为变量或方法使用someVariable。 如果您只想将ActionListener添加到按钮,那么应该删除

implements ActionListener
并且做:

button.addActionListener(new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        label.setText(...);
    }
});
注:

请注意,您得到该错误是因为您在构造函数中添加了该方法。 您应该遵循Java命名约定,为类使用SomeClass,为变量或方法使用someVariable。
你的语法不正确;您的意思是使用匿名类,或者您的意思是将actionPerformed声明为类方法,但意外地将该声明放在login中。“官方”中的示例与您在此处尝试执行的操作几乎相同—它使用按钮单击次数的计数更新标签。你可能想把它通读一遍。我投票认为这是一个印刷错误,加上已经说过的,使用与类名相同的变量名是不好的做法。登录名=新登录名;。按照惯例,类名应以大写字符开头。为避免混淆,更改登录名=新登录名;登录日志=新登录;或者将类重命名为“Login”。语法不正确;您的意思是使用匿名类,或者您的意思是将actionPerformed声明为类方法,但意外地将该声明放在login中。“官方”中的示例与您在此处尝试执行的操作几乎相同—它使用按钮单击次数的计数更新标签。你可能想把它通读一遍。我投票认为这是一个印刷错误,加上已经说过的,使用与类名相同的变量名是不好的做法。登录名=新登录名;。按照惯例,类名应以大写字符开头。为避免混淆,更改登录名=新登录名;登录日志=新登录;或者将该类重命名为“Login”。如果我将其移出构造函数,我将无法访问标签来更改它?@AnonOmus:简单的解决方案是:将JLabel以及需要从类中引用的任何其他内容声明为类本身的实例字段。谢谢,我刚刚完成了这项工作,它可以正常工作,请原谅我的糟糕ignorance@AnonOmus当前位置不要担心,因为我们中没有人天生就懂Java或OOPs,也许你是。顺便说一句,看起来你真正想用这段代码创建的是一个登录对话框,它显示在模态JDialog中,而不是JFrame中。哇-Jon Skeet可以让IE遵守他的CSS规则。那么他一定是个天才。如果我把它移出构造函数,我就无法访问标签来更改它?@AnonOmus:简单的解决方案是:声明JLabel,以及需要从类中引用到类本身作为实例字段的任何其他内容。谢谢,我刚刚做到了这一点,因为它确实有效,请原谅我的OOPignorance@AnonOmus当前位置不要担心,因为我们中没有人天生就懂Java或OOPs,也许你是。顺便说一句,看起来你真正想用这段代码创建的是一个登录对话框,它显示在模态JDialog中,而不是JFrame中。哇,Jon Skeet可以让IE遵守他的CSS规则,他一定是个天才。