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

时间:2016-04-20 16:06:30

标签: java swing

我刚刚开始与JSwing合作,并试图学习如何处理按钮等,所以所有的批评都会受到赞赏,我会提前为任何基本错误道歉。我只是尝试用两个具有不同结果的按钮创建一个基本的JFrame。由于某种原因,即使我使用动作侦听器,actionPerformed方法也找不到按钮“pressme”。任何人都可以帮我找到解决方案吗?先感谢您! - 尼克

    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);
      }
    }

2 个答案:

答案 0 :(得分:0)

您的按钮是在方法中声明和实例化的局部变量,将它们更改为实例变量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.");
}

答案 1 :(得分:0)

首先,更改您的班级名称。它不能是现有的类/容器名称(JPanel)。其次,对按钮和其他UI元素使用类变量而不是局部变量。

==将检查变量是否指向相同的内存。该方法在执行完毕后将被销毁。

如果你仍然无法执行监听器,要检查按下哪个按钮,而不是使用== 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
      }
    }

很久以前它解决了同样的问题。