按钮的ActionListener

时间:2014-08-18 02:28:27

标签: java swing jframe actionlistener

所以我为我的一个类开发了一个程序,当单击一个按钮时,可以使用ActionListener来处理事件。我有程序工作但是当你点击任何按钮时,你必须多次点击输入按钮才能输入注册的答案并移动到程序的下一级。

示例1:警报按钮打开2有时3"嘿,你有一个错误!"消息对话框。

示例2:是/否提示您3-4次返回对txt的是/否答案。

example3:在将输入返回到txt之前,Color需要3/4次点击。

(我想你得到的照片......)

对于我的生活,我无法找出为什么它不会仅仅采取一个输入并继续前进...

我的计划代码供您审核......先谢谢您。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class MessageBoxes{

  private JButton alert = new JButton("Alert");
  private JButton yesNo = new JButton("Yes/No");
  private JButton color = new JButton("Color");
  private JButton vals = new JButton("3 Vals");
  private JButton input = new JButton("Input");

  private JTextField txt = new JTextField(15);

  private JFrame frame;

  private ActionListener al = new ActionListener(){
    public void actionPerformed(ActionEvent e){

    alert.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
        {
            //Execute when button is pressed
            JOptionPane.showMessageDialog(null,
                                  "There's a bug on you!", "Hey!",
                                  JOptionPane.ERROR_MESSAGE);
        }
    });  

    yesNo.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
        {
            //Execute when button is pressed
            int val = JOptionPane.showConfirmDialog(null,
                                  "Choose yes or no", "Your Call...",
                                  JOptionPane.YES_NO_OPTION);

            if(val != JOptionPane.CLOSED_OPTION){
              if(val == 0){
                txt.setText("Yes");
            }
            else{
                txt.setText("No");
            }
            }
        }
    });  

    color.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
        {
            //Execute when button is pressed
            Object[] options = {"Red", "Green"};

            int sel = JOptionPane.showOptionDialog(null,
                            "Choose a Color!", "Warning",
                            JOptionPane.DEFAULT_OPTION,
                            JOptionPane.WARNING_MESSAGE, null,
                                        options, options[0]);

            if(sel != JOptionPane.CLOSED_OPTION){
                txt.setText("Color Selected: " + options[sel]);
            }
        }
    });  

    vals.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
        {
            //Execute when button is pressed
            Object[] selections = {"First", "Second", "Third"};

            Object val = JOptionPane.showInputDialog(null,
                                             "Choose one", "Input",
                                             JOptionPane.INFORMATION_MESSAGE,
                                             null, selections, selections[0]);

            if(val != null){
                txt.setText(val.toString());
            }
        }
    });

    input.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
        {
            //Execute when button is pressed
            String val = JOptionPane.showInputDialog("How mant fingers do you see?");

                txt.setText(val);
        }
    });  
  }
  };



  public MessageBoxes(){

    frame = new JFrame("Title");

    frame.setSize(250, 250);

    frame.getContentPane().setLayout(new FlowLayout());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.getContentPane().add(alert);
    frame.getContentPane().add(yesNo);
    frame.getContentPane().add(color);
    frame.getContentPane().add(vals);
    frame.getContentPane().add(input);
    frame.getContentPane().add(txt);

    frame.setVisible(true);

  }

  public void launchJFrame(){

    alert.addActionListener(al);
    yesNo.addActionListener(al);
    color.addActionListener(al);
    vals.addActionListener(al);
    input.addActionListener(al);

}

 public static void main(String [] args){

    MessageBoxes messageBoxes = new MessageBoxes();
    messageBoxes.launchJFrame();
 }
}

3 个答案:

答案 0 :(得分:3)

这是因为你的actionlistener构造/添加是嵌套在另一个actionlistener中。因此,您关注的动作者(弹出消息的动作)实际上并没有被添加到按钮,直到您单击它们一次。在launchJFrame()中将actionlisteners添加到按钮可以解决问题。这是您的代码的固定版本:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class MessageBoxes{

  private JButton alert = new JButton("Alert");
  private JButton yesNo = new JButton("Yes/No");
  private JButton color = new JButton("Color");
  private JButton vals = new JButton("3 Vals");
  private JButton input = new JButton("Input");

  private JTextField txt = new JTextField(15);

  private JFrame frame;

  public MessageBoxes(){

    frame = new JFrame("Title");

    frame.setSize(250, 250);

    frame.getContentPane().setLayout(new FlowLayout());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.getContentPane().add(alert);
    frame.getContentPane().add(yesNo);
    frame.getContentPane().add(color);
    frame.getContentPane().add(vals);
    frame.getContentPane().add(input);
    frame.getContentPane().add(txt);

    frame.setVisible(true);

  }

  public void launchJFrame(){

      alert.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                JOptionPane.showMessageDialog(null,
                                      "There's a bug on you!", "Hey!",
                                      JOptionPane.ERROR_MESSAGE);
            }
        });  

        yesNo.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                int val = JOptionPane.showConfirmDialog(null,
                                      "Choose yes or no", "Your Call...",
                                      JOptionPane.YES_NO_OPTION);

                if(val != JOptionPane.CLOSED_OPTION){
                  if(val == 0){
                    txt.setText("Yes");
                }
                else{
                    txt.setText("No");
                }
                }
            }
        });  

        color.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                Object[] options = {"Red", "Green"};

                int sel = JOptionPane.showOptionDialog(null,
                                "Choose a Color!", "Warning",
                                JOptionPane.DEFAULT_OPTION,
                                JOptionPane.WARNING_MESSAGE, null,
                                            options, options[0]);

                if(sel != JOptionPane.CLOSED_OPTION){
                    txt.setText("Color Selected: " + options[sel]);
                }
            }
        });  

        vals.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                Object[] selections = {"First", "Second", "Third"};

                Object val = JOptionPane.showInputDialog(null,
                                                 "Choose one", "Input",
                                                 JOptionPane.INFORMATION_MESSAGE,
                                                 null, selections, selections[0]);

                if(val != null){
                    txt.setText(val.toString());
                }
            }
        });

        input.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                String val = JOptionPane.showInputDialog("How mant fingers do you see?");

                    txt.setText(val);
            }
        });  

}

 public static void main(String [] args){

    MessageBoxes messageBoxes = new MessageBoxes();
    messageBoxes.launchJFrame();
 }
}

答案 1 :(得分:3)

问题是,每次在actionPerformed ActionListener内调用al时,它都会使用您的按钮注册新的ActionListener

private ActionListener al = new ActionListener() {
    public void actionPerformed(ActionEvent e) {

        alert.addActionListener(new ActionListener() {
            //...
        });

        yesNo.addActionListener(new ActionListener() {
            //...
        });

        color.addActionListener(new ActionListener() {
            //...
        });

        vals.addActionListener(new ActionListener() {
            //...
        });

        input.addActionListener(new ActionListener() {
            //...
        });
    }
};

因此,每次调用actionPerformed方法时,每个按钮都会注册一个新的actionListener

相反,您可以使用if-else语句来确定事件的来源,例如......

Object source = e.getSource();
if (source == alert) {
    JOptionPane.showMessageDialog(null,
                    "There's a bug on you!", "Hey!",
                    JOptionPane.ERROR_MESSAGE);
} else if (...

或者你可以完全摆脱ActionListener al并简单地将单个ActionListener注册到launchJFrame方法中的按钮......

public void launchJFrame() {

    alert.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //Execute when button is pressed
        }
    });

    yesNo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //Execute when button is pressed
            int val = JOptionPane.showConfirmDialog(null,
                            "Choose yes or no", "Your Call...",
                            JOptionPane.YES_NO_OPTION);

            if (val != JOptionPane.CLOSED_OPTION) {
                if (val == 0) {
                    txt.setText("Yes");
                } else {
                    txt.setText("No");
                }
            }
        }
    });

    color.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //Execute when button is pressed
            Object[] options = {"Red", "Green"};

            int sel = JOptionPane.showOptionDialog(null,
                            "Choose a Color!", "Warning",
                            JOptionPane.DEFAULT_OPTION,
                            JOptionPane.WARNING_MESSAGE, null,
                            options, options[0]);

            if (sel != JOptionPane.CLOSED_OPTION) {
                txt.setText("Color Selected: " + options[sel]);
            }
        }
    });

    vals.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //Execute when button is pressed
            Object[] selections = {"First", "Second", "Third"};

            Object val = JOptionPane.showInputDialog(null,
                            "Choose one", "Input",
                            JOptionPane.INFORMATION_MESSAGE,
                            null, selections, selections[0]);

            if (val != null) {
                txt.setText(val.toString());
            }
        }
    });

    input.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //Execute when button is pressed
            String val = JOptionPane.showInputDialog("How mant fingers do you see?");

            txt.setText(val);
        }
    });

}

答案 2 :(得分:0)

以下是我发现最适合我的解决方案:

private ActionListener alertAction = new ActionListener(){
  public void actionPerformed(ActionEvent e){
    //Execute when button is pressed
            JOptionPane.showMessageDialog(null,
                                  "There's a bug on you!", "Hey!",
                                  JOptionPane.ERROR_MESSAGE);
}
};

private ActionListener yesNoAction = new ActionListener(){
  public void actionPerformed(ActionEvent e){
    //Execute when button is pressed
            //Execute when button is pressed
            int val = JOptionPane.showConfirmDialog(null,
                                  "Choose yes or no", "Your Call...",
                                  JOptionPane.YES_NO_OPTION);

            if(val != JOptionPane.CLOSED_OPTION){
              if(val == 0){
                txt.setText("Yes");
            }
            else{
                txt.setText("No");
            }
            }
}
};

private ActionListener colorAction = new ActionListener(){
  public void actionPerformed(ActionEvent e){
    //Execute when button is pressed
            Object[] options = {"Red", "Green"};

            int sel = JOptionPane.showOptionDialog(null,
                            "Choose a Color!", "Warning",
                            JOptionPane.DEFAULT_OPTION,
                            JOptionPane.WARNING_MESSAGE, null,
                                        options, options[0]);

            if(sel != JOptionPane.CLOSED_OPTION){
                txt.setText("Color Selected: " + options[sel]);
            }
}
};

private ActionListener valsAction = new ActionListener(){
  public void actionPerformed(ActionEvent e){
    //Execute when button is pressed
            Object[] selections = {"First", "Second", "Third"};

            Object val = JOptionPane.showInputDialog(null,
                                             "Choose one", "Input",
                                             JOptionPane.INFORMATION_MESSAGE,
                                             null, selections, selections[0]);

            if(val != null){
                txt.setText(val.toString());
            }
}
};

private ActionListener inputAction = new ActionListener(){
  public void actionPerformed(ActionEvent e){
            //Execute when button is pressed
            String val = JOptionPane.showInputDialog("How mant fingers do you see?");

                txt.setText(val);
            }
};





public MessageBoxes(){

  frame = new JFrame("Title");

  frame.setSize(250, 250);

  frame.getContentPane().setLayout(new FlowLayout());

  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  frame.getContentPane().add(alert);
  frame.getContentPane().add(yesNo);
  frame.getContentPane().add(color);
  frame.getContentPane().add(vals);
  frame.getContentPane().add(input);
  frame.getContentPane().add(txt);

  frame.setVisible(true);

}

public void launchJFrame(){

  alert.addActionListener(alertAction);
  yesNo.addActionListener(yesNoAction);
  color.addActionListener(colorAction);
  vals.addActionListener(valsAction);
  input.addActionListener(inputAction);

}