gpt4 book ai didi

java - 我的算法有问题吗?

转载 作者:行者123 更新时间:2023-11-29 06:41:45 24 4
gpt4 key购买 nike

我正在创建一个“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 次学生补充)等。

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

最佳答案

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

关于java - 我的算法有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10972711/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com