简体   繁体   English

如何实现ActionListener

[英]How to implement ActionListener

I am trying to simply change the value of a JLabel when a button is clicked. 我试图在单击按钮时简单地更改JLabel的值。

This is the GUI code: 这是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");
       }
    }

}

main class code: 主类代码:

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

public class main
{

public static void main(String[] args) {

   login login = new login();

}


}

The actionEvent method in the login class returns the error Illegal Start of Expression. 登录类中的actionEvent方法返回错误“表达式的非法开始”。

If you just want to add the ActionListener to the button , then you should delete 如果只想将ActionListener添加到该按钮 ,则应该删除

implements ActionListener

and do: 并做:

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

Notes: 笔记:

  • Note that you are getting that error because you are adding that method inside the constructor. 请注意,您获得的错误,因为你要添加的构造内部的方法。
  • You should follow Java naming conventions, use names like SomeClass for classes and someVariable for variables or methods. 您应遵循Java命名约定,对类使用诸如SomeClass名称,对变量或方法使用诸如someVariable名称。

You've misplaced and misnamed the actionPerformed method. 您放错了位置并将其命名为actionPerformed方法。 Right now you have it nested inside of the constructor, and Java does not allow methods to be nested inside of other methods or constructors (same for constructors not being allowed to be nested in methods or constructors). 现在,您已将其嵌套在构造函数内部,并且Java不允许将方法嵌套在其他方法或构造函数内部(与不允许将嵌套的构造函数嵌套在方法或构造函数中相同)。 Move it out of the constructor, call it actionPerformed(ActionEvent e) , and see what happens. 将其移出构造函数,将其称为actionPerformed(ActionEvent e) ,然后看看会发生什么。

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

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