我創建一個「傳遞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名要添加的 學生),等等。
我不知道發生了什麼,但它只是無法正常工作。
我很難理解你打算做什麼。 –
廢棄該代碼並重新開始。首先,擺脫所有的靜態變量和方法。唯一應該是靜態的應該是主要的方法 - 就是這樣。接下來,詳細描述你想做什麼,因爲你的代碼真的沒有什麼意義。 –
OMG我在這個shi *上花了6個小時*幸運的是,現在它正在工作,但用戶一次只能添加一個學生:((( – b321234