我創建了一個名爲EmployeeGUI的類,它可以創建任意數量的用戶Employee對象並將它們添加到一個數組列表中。我遇到的問題是我的GUI底部有一個JTable,我想要顯示我創建的所有對象,但在我的情況下,GUI創建一個新的Table對象,每次它通過for循環一個對象的細節,但作爲for循環繼續新的細節覆蓋舊的細節,而不是被添加到Jtable的底部。任何人都可以幫助我調試我的錯誤?用JTable顯示Arraylist的值
這裏是我的代碼
import java.awt.event.*;
public class EmployeeGUI extends JFrame implements ActionListener{
/**
* @param args
*/
JFrame frame;
JButton button1, button2, button3, button4;
JTextField box1, box2, box3;
JLabel label1, label2, label3;
JTable table1;
int length = 0;
ArrayList<Employee> empArray = new ArrayList<Employee>();
public static void main(String[] args) {
// TODO Auto-generated method stub
EmployeeGUI empG = new EmployeeGUI();
empG.frame.setVisible(true);
}
public EmployeeGUI()
{
initialize();
}
public void initialize() {
// TODO Auto-generated method stub
frame = new JFrame("A Sample Window");
frame.setBounds(50,50,680,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
label1 = new JLabel("F-Name:");
label1.setBounds(30,33,54,25);
frame.getContentPane().add(label1);
box1 = new JTextField();
box1.setBounds(94, 35, 128,20);
frame.getContentPane().add(box1);
label2 = new JLabel("S-Name:");
label2.setBounds(250,33,54,25);
frame.getContentPane().add(label2);
box2 = new JTextField();
box2.setBounds(305, 35, 128,20);
frame.getContentPane().add(box2);
label3 = new JLabel("Phone:");
label3.setBounds(461,33,54,25);
frame.getContentPane().add(label3);
box3 = new JTextField();
box3.setBounds(500, 35, 128,20);
frame.getContentPane().add(box3);
button1 = new JButton("Add Employee");
button1.addActionListener(this);
button1.setBounds(71,131,113,39);
frame.getContentPane().add(button1);
button2 = new JButton("Remove Employee");
button2.addActionListener(this);
button2.setBounds(194,131,128,39);
frame.getContentPane().add(button2);
button3 = new JButton("Display Employee");
button3.addActionListener(this);
button3.setBounds(332,131,128,39);
frame.getContentPane().add(button3);
button4 = new JButton("Quit Program");
button4.addActionListener(this);
button4.setBounds(475,131,113,39);
frame.getContentPane().add(button4);
table1 = new JTable();
table1.setBounds(0,184,664,178);
frame.getContentPane().add(table1);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String action = ((JButton) e.getSource()).getActionCommand();
if(action.equals("Add Employee"))
{
String fName = box1.getText();
String lName = box2.getText();
String pNo = box3.getText();
int mobile = Integer.parseInt(pNo);
Employee ee = new Employee(fName,lName,mobile);
empArray.add(ee);
length++;
JOptionPane.showMessageDialog(null, "Employee Added");
}
if(action.equals("Remove Employee"))
{
String fName = box1.getText(), lName = box2.getText();
int mobile = Integer.parseInt(box3.getText());
Employee ee = new Employee(fName,lName,mobile);
if(length>0)
{
for(int i=0; i<empArray.size(); i++)
{
if(empArray.get(i).getLName() == ee.getLName())
{
empArray.remove(i);
JOptionPane.showMessageDialog(null, "Employee Removed");
}
}
}
else{
throw new ListEmptyException("List is Empty");
}
}
if(action.equals("Display Employee"))
{
for(int i = 0; i <empArray.size(); i++)
{
table1.setModel(new DefaultTableModel(
new Object[][] {
{empArray.get(i).getFName(),empArray.get(i).getLName(),empArray.get(i).getMobile()}
},
new String[] {
"First Name", "Surname", "Phone Number"
}
));
}
}
if(action.equals("Quit Program"))
{
System.exit(0);
}
}
}
和Employee類
public class Employee {
private String fName,lName;
private int mobile;
public Employee(String fName, String lName, int mobile)
{
setFName(fName);
setLName(lName);
setMobile(mobile);
}
private void setMobile(int mobile) {
// TODO Auto-generated method stub
this.mobile = mobile;
}
public void setLName(String lName) {
// TODO Auto-generated method stub
this.lName = lName;
}
public void setFName(String fName) {
// TODO Auto-generated method stub
this.fName = fName;
}
public String getFName()
{
return fName;
}
public String getLName()
{
return lName;
}
public int getMobile()
{
return mobile;
}
public String toString()
{
return getFName()+" "+getLName()+" "+getMobile();
}
public void print()
{
System.out.println(toString());
}
}
對那個'System.exit(0)'進行操作......每次都重置Table模型。改爲使用自定義的'MutableTableModel'和'model.fireTableDataChanged()'。 – AlexR
您正在爲for循環的每次迭代創建一個** new ** TableModel - 因此您不應該對代碼正在執行的操作感到驚訝。解決方案:不要這樣做。在for循環之前創建一個模型**,並用for循環中的項目填充它。這不是Java,這是常識。此外,您需要擺脫setBounds並使用佈局管理器。 –