1

我正在创建一个“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 个学生),依此类推。

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

4

1 回答 1

7

每次调用时addStudent(),都将 ActionListener 添加到 JButton,这将导致 JButton 最终多次添加侦听器。这意味着当按钮被按下时,监听器将被调用多次,这是你真的不希望发生的事情。解决方案是不这样做。相反,只需在构造函数或 init 方法中将侦听器添加到 JButton 一次,然后将其保留。

于 2012-06-10T22:07:13.527 回答