我正在尝试创建一个地址簿,而我目前需要的只是一个用于存储数据的表,但是我无法将其显示在面板上,任何想法为什么?我有一个方法应该在执行时创建表但不是。
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();
}
}
答案 0 :(得分:3)
panel.setLayout(null);
您选择不使用布局管理器并自行处理所有布局。
JTable table = new JTable(data, columns); table.setModel(model); table.setBackground(Color.LIGHT_GRAY); table.setForeground(Color.black); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setFillsViewportHeight(true);
但是,您没有指定JTable的大小和位置。
我建议学习使用布局管理器。它会更好地工作,它将处理用户调整窗口大小,并使代码更易于维护。