2012-02-08 45 views
0

我試圖根據用戶輸入的內容添加一個新行到我的JTable,並重復多次用戶註冊。例如,假設用戶在registerTF中輸入了「Susie」,對於lengthTF輸入了「11:00」,對於IDTF輸入了「60」。我可以在examInfo.java中以行的形式添加到我的JTable中,但沒有問題,但我該如何重複執行,因此下一次運行Registration.java時,在這些字段中輸入的所有內容都將放入一個全新的行中我的JTable。在任何人問起之前,不,這不是我爲家庭成員做的家庭作業。向JTable添加一個新行

這裏是我的Registration.java代碼:

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 

public class Registration extends JFrame{ 

    private static final int WIDTH = 400; 
    private static final int HEIGHT = 300; 
    private JLabel registerL, lengthL, IDL; 
    private JTextField registerTF, lengthTF, IDTF; 
    private JButton registerB, exitB; 

    //Button handlers: 
    private CalculateButtonHandler cbHandler; 

    private ExitButtonHandler ebHandler; 



    public Registration(){ 

     registerL = new JLabel("Enter Name: ", SwingConstants.RIGHT); 
     lengthL = new JLabel("Enter Registration: ", SwingConstants.RIGHT); 
     IDL = new JLabel("Length of exam: ", SwingConstants.RIGHT); 

     registerTF = new JTextField(10); 
     lengthTF = new JTextField(10); 
     IDTF = new JTextField(10); 


     //Specify handlers for each button and add (register) ActionListeners to each button. 
     registerB = new JButton("Register"); 
     cbHandler = new CalculateButtonHandler(); 
     registerB.addActionListener(cbHandler); 
     exitB = new JButton("Exit"); 
     ebHandler = new ExitButtonHandler(); 
     exitB.addActionListener(ebHandler); 



     setTitle("Registration"); 
     Container pane = getContentPane(); 
     pane.setLayout(new GridLayout(4, 2)); 


     //Add things to the pane in the order you want them to appear (left to right, top to bottom 
     pane.add(registerL); 
     pane.add(registerTF); 
     pane.add(lengthL); 
     pane.add(lengthTF); 
     pane.add(IDL); 
     pane.add(IDTF); 
     pane.add(registerB); 
     pane.add(exitB); 

     setSize(WIDTH, HEIGHT); 
     setVisible(true); 

     setDefaultCloseOperation(EXIT_ON_CLOSE); 

    } 
    private class CalculateButtonHandler implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 

    } 
} 

public class ExitButtonHandler implements ActionListener{ 
    public void actionPerformed(ActionEvent e){ 
     System.exit(0); 
    } 
} 

public static void main(String[] args){ 
    Registration rectObj = new Registration(); 
} 
} 

這是我examInfo.java代碼在我的JTable的是:

import java.util. Arrays; 
import java.util.Vector; 
import java.awt.BorderLayout; 

import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 

public class examInfo { 
    public static void main(String agrs[]){ 

     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     Vector<String> rowOne = new Vector<String>(); 
     rowOne.addElement("Row1-Column1"); 
     rowOne.addElement("Row1-Column2"); 
     rowOne.addElement("Row1-Column3"); 

     Vector<String> rowTwo = new Vector<String>(); 
     rowTwo.addElement("Row2-Column1"); 
     rowTwo.addElement("Row2-Column2"); 
     rowTwo.addElement("Row2-Column3"); 

     Vector<Vector> rowData = new Vector<Vector>(); 
     rowData.addElement(rowOne); 
     rowData.addElement(rowTwo); 

     Vector<String> columnNames = new Vector<String>(); 
     columnNames.addElement("Time"); 
     columnNames.addElement("Length"); 
     columnNames.addElement("ID"); 
     JTable table = new JTable(rowData, columnNames); 

     table.setValueAt("11:00 Wed", 0, 0);  

     JScrollPane scrollPane = new JScrollPane(table); 
     frame.add(scrollPane, BorderLayout.CENTER); 
     frame.setSize(300, 150); 
     frame.setVisible(true); 

    } 
} 

回答