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 人の生徒を追加する必要があることがわかります。 )

ただし、ユーザーが「追加」ボタンを 2 回目にクリックすると、生徒が 2 回追加されます (追加する生徒はあと 11 人です)。3 回目のクリックで 8 回追加されます (さらに 3 人の生徒を追加する必要があります)。

何が起こったのかわかりませんが、正しく動作しません。

4

1 に答える 1

7

呼び出すたびaddStudent()に JButton に ActionListener を追加すると、最終的に JButton にリスナーが何度も追加されることになります。これは、ボタンが押されたときにリスナーが数回呼び出されることを意味します。これは、実際には発生したくないことです。解決策は、それをしないことです。代わりに、リスナーを JButton にコンストラクターまたは init メソッドで 1 回だけ追加し、そのままにしておきます。

于 2012-06-10T22:07:13.527 に答える