나는 "때문에 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 나는이시 *에서 6 시간을 보냈다. 운좋게 이제는 작동하지만 사용자는 한 번에 한 학생 만 추가 할 수있다. (((( – b321234