简体   繁体   中英

Java actionlistener doesn't work

I just started learning Java 1 week ago, and I'm a 100% totally beginner. In this code, I can't seem to be able to put an actionlistener/get one to work. I don't even know where/how/in what way to put it, despite reading dozens of tutorials. I've created a JFrame with a JPanel in it, and on the JPanel there's a button. So far so good (and working). But then, I want it to be so that if the button is clicked, another button appears. Thank you sooo much in advance!

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class Skeleton extends JFrame implements ActionListener {

    public static void main(String[] args) {
    //------------------------------------------------
    JFrame frame = new JFrame("Skeleton");
    JPanel panel = new JPanel();
frame.setContentPane(panel);
    frame.setSize(600,600);
    frame.setResizable(false);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    JButton button = new JButton("This is a button.");
    JButton button2 = new JButton("Hello");
    panel.setLayout(null);
    button.setBounds(20,20,200,25);
    button2.setBounds(20,70,200,25);
    panel.add(button);
   //-------------------------------------------

button.addMouseListener(this);


}

public void ActionPerformed(ActionEvent e) {
    System.out.println("Hello");

}
}

i will give you some advice

1) Don't implement ActionListener in top classes, use anonymous classes or private classes instead.

Example :

Anonymous class (also call Swing Actions)

myComponent.addActionListener(new ActionListener(){
      @Override
      public void actionPerformed(ActionEvent evt){
              //code here
      }

})

or

//inner class
public class Skeleton{

// in some part
private class MyActionListener implements ActionListener{
      public void actionPerformed(ActionEvent evt){
             //code here
       }
}


}

2) Your code won't compile cause you are not implementing ActionListener interface

public void actionPerformed(ActionEvent evt) is the signature.

You have to addActionListener to your component

button.addActionListener(this);

3) Don't use null layout, cause you'll have a lot of problem if you want to add more components or resize windows cause you have to setBounds manually and it will be frustrating instead use [Layout Manager][1] .

4) Try to not extends JFrame if is not necesary instead have a reference in your class, for example.

  public class Skeleton{

    private JFrame frame;

    }

You need to add the actionlistener.

Register an instance of the event handler class as a listener on one or more components. For example:

yourdesiredcomponent.addActionListener(this);

For more details check the doc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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