2016-04-21 163 views
-1

我想创建一个地址簿,我目前需要的是一张表来存储数据,但是我不能让它显示在面板上,任何想法为什么?我有一个方法应该是在执行时创建表,但不是。为什么我的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(); 

} 

}

+0

1)请参阅[检测/修复代码块的悬挂紧密支架](http://meta.stackexchange.com/q/251795/155831),以解决问题,我不再担心修复问题。 2)以最小尺寸提供ASCII艺术或简单的GUI图形*图形,并且如果可调整大小,则具有更大的宽度和高度。 –

回答

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中的大小和位置。

我建议学习使用布局管理器。它会更好地工作,它将处理调整窗口大小的用户,并使代码更易于维护。

+0

我将阅读布局经理,谢谢 – breadkun

+0

@breadkun,从[布局管理器](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html)上的Swing教程一节开始,获取信息和工作示例。您还需要查看“如何使用表格”部分的目录。 – camickr