我的算法有问题吗?

时间:2012-06-10 22:04:55

标签: java actionlistener

我正在创建一个“addStudent”方法,它看起来像这样:

package gui;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.*;

import dataManager.DataManager;



public class test extends JFrame {
    private static boolean addHowManyStudentsSet=false;
    private static int addHowManyStudents=0;
    private static JFrame addStudentFrame = new JFrame("Add Student");
    private static JTextField newStudentName = new JTextField();
    private static JTextField newStudentID = new JTextField();
    private static JLabel label1 = new JLabel("");
    private static final JButton addButton = new JButton("ADD");
    private static JButton addStudent = new JButton("SET");
    private static JPanel addStudentPanel = new JPanel();
    /**
     * Constructor of the GUI, creating labels, buttons, and other stuff.  Then they are added onto the interface.
     */
    public test() {
        super("test");
        setSize(200, 200);
        setLocation(10, 10);
        final JPanel panel = new JPanel();

        addStudent.setBounds(10,60,80,25);
        panel.add(addStudent);
        add(panel);
        addStudent.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae){
                if(!addHowManyStudentsSet){
                    try{
                        addHowManyStudents=Integer.parseInt(JOptionPane.showInputDialog(panel, "Add how many students..."));
                        JOptionPane.showMessageDialog(panel,"Set, please click this button again");
                        addStudent.setText("ADD");
                        addHowManyStudentsSet=true;
                    }
                    catch(NumberFormatException ex){
                        JOptionPane.showMessageDialog(panel, "Please enter a number");
                    }
                }

                else{

                    addStudentPanel.setLayout(null);
                    label1.setText("    "+(addHowManyStudents-1)+" more students to add...");
                    label1.setFont(new Font("Segoe UI Light",Font.PLAIN,30));
                    label1.setBounds(5,20,400,25);
                    newStudentName.setBounds(270,100,140,30);
                    newStudentID.setBounds(270,150,140,30);
                    final JLabel label2 = new JLabel("New Student Name:");
                    final JLabel label3 = new JLabel("New Student Number:");
                    label2.setBounds(30,100,200,30);
                    label2.setFont(new Font("Segoe UI Light",Font.PLAIN,21));
                    label3.setBounds(30,150,200,30);
                    label3.setFont(new Font("Segoe UI Light",Font.PLAIN,21));
                    //      final JButton addButton = new JButton("ADD");
                    addButton.setBounds(330,220,80,25);
                    addStudentPanel.add(addButton);
                    addButton.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent ae){            
                            addStudent();
                            //      addStudentFrame.dispose();
                        }
                    });                 
                    addStudentPanel.add(label1);
                    addStudentPanel.add(label2);
                    addStudentPanel.add(label3);
                    addStudentPanel.add(newStudentName);
                    addStudentPanel.add(newStudentID);
                    addStudentFrame.add(addStudentPanel);
                    addStudentFrame.setVisible(true);
                    addStudentFrame.setLocation(40,40);
                    addStudentFrame.setSize(470,335);
                }

            }

        });
    }

    public static void main(String[] args) {
        JFrame f = new test(  );

        f.addWindowListener(new WindowAdapter(  ) {
            public void windowClosing(WindowEvent we) { 
                System.exit(0); }
        });
        f.setVisible(true);
    }

    private static void addStudent(){
        if(addHowManyStudents>0){
            addHowManyStudents--;           
            //          addButton.addActionListener(new ActionListener(){
            //              public void actionPerformed(ActionEvent ae){        
            System.out.println("add");
            //          //  JLabel label1 = new JLabel((StudentList.getHowManyStudentToAdd()-1)+"more students to add");
            try{
                String studentName = newStudentName.getText();
                long studentNum = Long.parseLong(newStudentID.getText());
                //          //  DataManager.addStudent(studentNum, studentName);
                System.out.println("Done: "+studentNum+", "+studentName);
            }
            catch(NumberFormatException ex){
                JOptionPane.showMessageDialog(addStudentFrame, "Student ID can only be numbers");
            }
            if(addHowManyStudents!=0){
                label1.setText("    "+(addHowManyStudents-1)+" more students to add...");

            }
            newStudentName.setText("");
            newStudentID.setText("");
            addStudent();
            //              }               
            //          });

        }
        else if(addHowManyStudents==0){
            JOptionPane.showMessageDialog(addStudentFrame,"Done!");
            addStudentFrame.dispose();
            addHowManyStudentsSet=false;
            addStudent.setText("SET");
        }
    }
}

这实际上非常有趣,因为用户第一次点击“添加”按钮 只添加一次学生(例如,如果你想添加14名学生,它可以工作 第一次正确地告诉你还有13个学生要添加。)

然而,当用户第二次点击“添加”按钮时,它会添加学生 两次(还有11名学生要加);它在第三次点击时增加了8次(另外3次 学生加),等等。

我不知道发生了什么,但它无法正常工作。

1 个答案:

答案 0 :(得分:7)

每次调用addStudent()时,都会将ActionListener添加到JButton中,这将导致JButton最终多次添加侦听器。这意味着当按下按钮时,将多次调用监听器,这是您真正不希望发生的事情。解决方案不是这样做的。而是在构造函数或init方法中仅将监听器添加到JButton一次,并将其留在那里。