2012-06-10 118 views
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名要添加的 學生),等等。

我不知道發生了什麼,但它只是無法正常工作。

+0

我很難理解你打算做什麼。 –

+0

廢棄該代碼並重新開始。首先,擺脫所有的靜態變量和方法。唯一應該是靜態的應該是主要的方法 - 就是這樣。接下來,詳細描述你想做什麼,因爲你的代碼真的沒有什麼意義。 –

+0

OMG我在這個shi *上花了6個小時*幸運的是,現在它正在工作,但用戶一次只能添加一個學生:((( – b321234

回答

7

每次您撥打addStudent()時,都會將ActionListener添加到JButton,這將導致JButton最終多次添加偵聽器。這意味着當按鈕被按下時,監聽器會被多次調用,這是你真正不想發生的事情。解決方案不是這樣做的。相反,只需在構造函數或init方法中將偵聽器添加到JButton中一次,然後將其留在那裏。

+0

我其實試過這樣做,但它不會工作,因爲現在if用戶點擊按鈕,它會爲[addHowManyStudents]次添加該學生的權利..? – b321234

+0

@ user1447864:這是沒有意義的。當你建立你的GUI時,你建立了你的聽衆,你做了一次,就是這樣。如果您希望將學生添加到列表或其他集合中,則不要在此方法中添加偵聽器。如果您需要更多幫助,請考慮告訴我們更多關於您的程序的內容,因爲我有一種感覺,即您的設計已關閉。 –

+0

............恐怕你是對的.....我給每一個按鈕一個新的ActionListener ... OMGOMG ...我會發布整個事情在這裏可以ü幫助我,這是我的12年級最終項目的一部分,它是我最後的標誌的30%....謝謝!! – b321234