简体   繁体   English

麻烦与ActionListener

[英]Trouble with ActionListener

I am having trouble implementing ActionListener. 我在实现ActionListener时遇到问题。 In my class addbutton it will not let me ass ActionListener. 在我的类addbutton中,它不会让我屁股ActionListener。 What I am trying to do is display two JFrame's and have click able as a button. 我想做的是显示两个JFrame并单击即可作为按钮。 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. 该按钮会出现,但是单击它不会执行任何操作,因此我添加了ActionListener,但未为我的方法定义该按钮。 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. 不要向两个JFrame添加相同的按钮。 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. 同样,按照惯例,所有Java类的名称都以大写字母开头。 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. 通常,给名为“ button”的变量分配一个JButton对象,而不是JFrame对象,在这种情况下,通常将其命名为“ otherFrame”之类的东西,表明它是一个框架,并且此时还存在另一个框架。

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: 因此,假设一个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");
      }
    });

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

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