简体   繁体   中英

Trouble with ActionListener

I am having trouble implementing ActionListener. In my class addbutton it will not let me ass ActionListener. What I am trying to do is display two JFrame's and have click able as a button. I have everything else working expect for the the button clicking action. The button appears, but clicking it does nothing so I add an ActionListener and it is not define for my method. What am I doing wrong and what can I do to fix it. Thank you.

import java.awt.event.ActionEvent;

import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;

@SuppressWarnings("serial")
public class PictureTest extends JFrame {


public static class addbutton extends JFrame implements ActionListener{

    public addbutton()  { 

        JFrame button = new JFrame();
        JButton take = new JButton("Take Please");
        button.add(take);
        add(take);
        button.addActionListener(this); //This line is the issue
    }
    public void actionPerformed(ActionEvent e) {
        e.getSource();
        System.out.println("Here");

    }
}



public PictureTest() {
    ImageIcon image = new ImageIcon("c:/Vending/pepsi.jpg");
    JLabel label = new JLabel(image);
    JPanel soda = new JPanel();
    soda.add(label);
    add(soda);
}

        public static void main(String[] args)  {
        PictureTest frame = new PictureTest();
        addbutton button = new addbutton();
        frame.setSize(250, 450);
        frame.setTitle("Pepsi");
        frame.setLocation(200, 100);
        frame.setUndecorated(true);
        button.setSize(105, 25);
        button.setLocation(275, 550);
        button.setUndecorated(true);
        button.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        button.setVisible(true);
        }
}

Don't add the same button to both JFrame s. Add the listener to the button.

Add two buttons but you can make the listener listen to a click on either button.

    JButton take = new JButton("Take Please");
    button.add(take);
    take.addActionListener(this); // listen to the button

    JButton take2 = new JButton("Take Please");
    add(take2);
    take2.addActionListener(this); // also listen to the other button

Also, by convention, all Java classes have names beginning with a capital letter. If you follow this rule and get used to it yourself, others will be able to read your code more easily. And you theirs.

It is possible that you could help avoid this error with a different way of naming components.

Usually a variable named 'button' is assigned a JButton object, not a JFrame , which would typically be named, in this case, something like "otherFrame" indicating it is a frame and there is another one that's also in play at this time.

Another way of doing this is to use an anonymouse inner class to do the listening, but you can't easily have it listen to two buttons that way. So, assuming there was only one button in one JFrame:

    JButton take = new JButton("Take Please");
    button.add(take);
    take.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        e.getSource();
        System.out.println("Here");
      }
    });

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