简体   繁体   English

使用ActionListener查找事件时遇到问题

[英]Trouble with finding event using ActionListener

I'm just beginning to work with JSwing and trying to learn how to deal with buttons and such, so all critiques will be appreciated and I'll apologize in advance for any basic errors. 我刚刚开始与JSwing合作,并试图学习如何处理按钮等,所以所有的批评都将受到赞赏,我会提前为任何基本错误道歉。 I'm only trying to create a basic JFrame with two buttons with different outcomes. 我只是尝试用两个具有不同结果的按钮创建一个基本的JFrame。 For some reason the method actionPerformed can not find the button "pressme" even though I use the Action Listener. 由于某种原因,即使我使用动作侦听器,actionPerformed方法也找不到按钮“pressme”。 Can anyone help me find the solution to this? 任何人都可以帮我找到解决方案吗? Thank you in advance! 先感谢您! -Nick -缺口

    import java.util.Scanner;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class JPanel  extends JFrame implements ActionListener
    { 
      public JPanel createContentPane(){
        JPanel pane = new JPanel();
        Container con = this.getContentPane();
        JPanel titlePanel = new JPanel();
            titlePanel.setLayout(null);
            titlePanel.setLocation(10, 0);
            titlePanel.setSize(250, 30);
            pane.add(titlePanel);
        JLabel answer = new JLabel("");
            pane.add(answer);
        JLabel label1 = new JLabel("Is Ms. Stilman a savage?",            JLabel.CENTER);
            label1.setSize(150, 15);
            label1.setLocation(250, 10);
            titlePanel.add(label1);
        JButton pressme = new JButton("Nah, what are you talking         about?");
            pane.add(pressme);
            pressme.setMnemonic('P');
            pressme.addActionListener(this);
            pressme.setLocation(250, 10);
            pressme.requestFocus();
        JButton secondpress = new JButton("Yea she's a hardcore savage.");
            pane.add(secondpress);
            secondpress.addActionListener(this);
            secondpress.setLocation(250, 10);
            secondpress.requestFocus();
              }
              public void actionPerformed(ActionEvent event)  {
          Object source = event.getSource();
          if (source == pressme) {
              answer.setText("Button pressed!");
              JOptionPane.showMessageDialog(null, "You're wrong and probably a scrub.", "Message Dialog",
                      JOptionPane.PLAIN_MESSAGE); setVisible(true);
          }
          else if (source == secondpress) {
              answer.setText("Button pressed!");
              JOptionPane.showMessageDialog(null, "Oh ya for sure.",         "Message Dialog",
              JOptionPane.PLAIN_MESSAGE); setVisible(true);
          }
      }
      public static void createAndShowGui(){
       JFrame Frame1 = new JFrame("Button Press Test"); 
            setContentPane(demo.createContentPane());
            setSize(500, 300);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            setVisible(true);
      }
    }

your buttons are local variables which are declared and instantiated within a method change them to instance variables ex: 您的按钮是在方法中声明和实例化的局部变量,将它们更改为实例变量ex:

private final JButton pressme;
private final JButton secondpress;

createContentPane(){
pressme = new JButton("Nah, what are you talking         about?");
secondpress =new JButton("Yea she's a hardcore savage.");
}

First, change your class name. 首先,更改您的班级名称。 It can't be existing class/container's name(JPanel). 它不能是现有的类/容器名称(JPanel)。 Second, use class variables instead of local variables for buttons and other UI elements. 其次,对按钮和其他UI元素使用类变量而不是局部变量。

== will check if the variable point to the same memory. ==将检查变量是否指向相同的内存。 The object will be destroyed after the method completes it's execution. 方法完成后,对象将被销毁。

If you still are not able execute the listener, for checking which button is pressed, instead of using == operation try using the following type of code: 如果您仍然无法执行侦听器,要检查按下哪个按钮,而不是使用== operation,请尝试使用以下类型的代码:

    button.setActionCommand("-");
    button.addActionListener(this);
    public void actionPerformed(ActionEvent me){

      JButton button =(JButton)me.getSource();
      if(button.getActionCommand().equals("-"))
        //corresponding action to be taken
      }
    }

It has solved the same problem to me a long back ago. 它很久以前就解决了同样的问题。

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

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