我想創建一個地址簿,我目前需要的是一張表來存儲數據,但是我不能讓它顯示在面板上,任何想法爲什麼?我有一個方法應該是在執行時創建表,但不是。爲什麼我的JTable沒有顯示?
private String name;
private String surname;
private String phone;
private String address;
static String summary;
private JPanel panel;
private JFrame frame;
private JLabel lblName;
private JLabel lblAddress;
private JLabel lblSurname;
private JLabel lblPhone;
private JTextField txtName;
private JTextField txtSurname;
private JTextField txtPhone;
private JTextField txtAddress;
private JButton btnSave;
private JButton btnUpload;
private JButton btnDelete;
String columns[] = {"Name","Surname","Phone","Address"};
String[][] data =
{{"human", "bean","07443244","somewhere"},
{"Max", "Kenny","044353534","Somewhere"}};
public AddressBook() {
createForm();
createLabels();
createTextField();
createButton();
createTable();
frame.add(panel);
frame.setVisible(true);
}
public void createLabels(){
lblName = new JLabel ("Name");
lblName.setBounds(10, 30, 100, 20);
panel.add (lblName);
lblSurname = new JLabel ("Surname");
lblSurname.setBounds(10, 50, 100, 20);
panel.add (lblSurname);
lblAddress = new JLabel ("Address");
lblAddress.setBounds(10, 70, 100, 20);
panel.add (lblAddress);
lblPhone = new JLabel ("Phone");
lblPhone.setBounds(10, 90, 100, 20);
panel.add (lblPhone);
}
public void createTextField(){
txtName = new JTextField (null);
txtName.setBounds(110, 30, 150, 20);
panel.add (txtName);
txtSurname = new JTextField (null);
txtSurname.setBounds(110, 50, 150, 20);
panel.add (txtSurname);
txtAddress = new JTextField (null);
txtAddress.setBounds(110, 70, 150, 20);
panel.add (txtAddress);
txtPhone = new JTextField (null);
txtPhone.setBounds(110, 90, 150, 20);
panel.add (txtPhone);
}
public void createForm(){
panel = new JPanel();
panel.setBorder (BorderFactory.createLineBorder (Color.RED, 3));
panel.setLayout(null);
frame = new JFrame();
frame.setTitle("Address Book");
frame.setSize(800,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void createButton(){
btnSave = new JButton ("Save to a file");
btnSave.setBounds(50, 200, 200, 20);
btnSave.addActionListener(new SaveHandler());
panel.add (btnSave);
btnDelete = new JButton ("Delete from the table");
btnDelete.setBounds(50, 300, 200, 20);
panel.add (btnDelete);
btnUpload = new JButton ("Upload file to table");
btnUpload.setBounds(50, 400, 200, 20);
panel.add (btnUpload);
}
public void createTable(){
JTable table = new JTable(data, columns);
DefaultTableModel model = new DefaultTableModel();
model.setColumnIdentifiers(columns);
table.setModel(model);
table.setBackground(Color.LIGHT_GRAY);
table.setForeground(Color.black);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
panel.add(table);
}
class SaveHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnSave)
{
name = ("");
surname = ("");
phone = ("");
address = ("");
name = txtName.getText();
surname = txtSurname.getText();
phone = txtPhone.getText();
address = txtAddress.getText();
summary = ("Name:" +name)+(surname)+("Phone:" + phone)+("Address:" + address);
String saveFile = summary;
try {
BufferedWriter reader = new BufferedWriter(new FileWriter(new File("userinfo.txt"), true));
reader.write(saveFile);
reader.newLine();
reader.close();
JOptionPane.showMessageDialog(frame, "The details were sucessfuly saved");
} catch (IOException e1) {
e1.printStackTrace();
}
}
} }
public static void main(String[] args) {
new AddressBook();
}
}
1)請參閱[檢測/修復代碼塊的懸掛緊密支架](http://meta.stackexchange.com/q/251795/155831),以解決問題,我不再擔心修復問題。 2)以最小尺寸提供ASCII藝術或簡單的GUI圖形*圖形,並且如果可調整大小,則具有更大的寬度和高度。 –