0

I keep getting an error when I implement the ActionListener. I dont really understand how to fix it. I did the actionPerformed (ActionEvent ev) {} and put my login button to call it with lg.addActionListener(this);

import java.awt.*;
import javax.swing.*;
import java.awt.Event.*;
import java.applet.Applet;

public class LoginScreen extends JApplet implements ActionListener   {
    JTextField un;
    JPasswordField pw;
    JButton lg;
    JLabel user,pass;

    public LoginScreen () {
        un = new JTextField ();
        pw = new JPasswordField ();
        lg = new JButton ("login");
        user = new JLabel ("username");
        pass = new JLabel ("password");

        lg.addActionListener(this);

        this.setLayout(null);

        user.setBounds(10, 10, 120, 20);
        pass.setBounds(10, 30, 120, 20);
        un.setBounds(140, 10, 200, 20);
        pw.setBounds(140, 30, 200, 20);

        lg.setBounds(140, 55, 100, 20);

        this.add(user);
        this.add(pass);
        this.add(un);
        this.add(pw);
        this.add(lg);

        this.setSize(500, 300);
        this.setVisible(true);
        }       
    public void actionPerformed(ActionEvent ev) {

    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
mike-gallego
  • 706
  • 2
  • 12
  • 27
  • 3
    So what is the error? – Sibbo Jan 02 '14 at 03:02
  • First bad practice `setLayout(null)` 2nd) implements ActionListener in a top level class, 3rd) Not add @Override annotation in `actionPerformed()` 4th) Your method implementation is empty.. – nachokk Jan 02 '14 at 03:05
  • 1) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). 2) `this.setSize(500, 300);` An applet's size is set in HTML. It should not be set in code. .. – Andrew Thompson Jan 02 '14 at 06:24
  • .. 3) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 4) Always copy/paste error & exception output. – Andrew Thompson Jan 02 '14 at 06:28

4 Answers4

5

Change import java.awt.Event.*; to import java.awt.event.*;

Java is case-sensitive.

Also change import java.applet.Applet; -> import javax.swing.JApplet;

Sibbo
  • 3,796
  • 2
  • 23
  • 41
4

You have the wrong imports. You need. import java.awt.event.*; or

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

The latter is better practice


If you're using one of the following IDEs (with default configuration), this is easily fixed with a couple buttons.

Eclipse Ctrl + Shift + O

Netbeans Ctrl + Shift + I

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
2
import java.awt.Event.*;

needs to be

import java.awt.event.*;

This causes a compile error. Once this has been fixed, you need to write some code in the actionPerformed or nothing will happen when you button is pressed.

Josh Engelsma
  • 2,636
  • 14
  • 17
  • +! I think the OP knows the second part. Besides there isn't even a `main` to run the program, so I think the question was just an example duplicating the error :) – Paul Samsotha Jan 02 '14 at 03:34
  • 1
    Thanks, I also upvoted yours! I admit you beat me to the answer first, second time that has happened to me today..so frustrating haha. I agree that he most likely knows about the second part, however, you never know based on some of the questions I have seen on SO :) – Josh Engelsma Jan 02 '14 at 03:40
  • thanks this helped me a lot. case sensitivity always gets me. – mike-gallego Jan 02 '14 at 03:44
  • Not a problem, consider accepting an answer so that this question is resolved on stackoverflow – Josh Engelsma Jan 02 '14 at 03:49
0

In your actionPerformed method, if you do not specify any action, your button will do nothing.