这是我的文本文件的样子:将其复制到记事本中并在 SSCCE 中打开
名字:丹 姓氏: rmadakkk 电话号码:(348) 794-7329 电子邮件:leo@live.ca
更新:这就是我所看到的。
这就是我想要的。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.table.*;
public class Main extends JFrame {
private JTextField Searchtextfield;
private JTable table;
private JTextField Firstnametext;
private JTextField lastnametext;
private JTextField Phonenumbertext;
private JTextField Emailtext;
BufferedReader reader;
DefaultTableModel tableModel;
public Main() throws Exception {
getContentPane().setLayout(null);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setBounds(0, 32, 650, 365);
getContentPane().add(tabbedPane);
JPanel MainPanel = new JPanel();
MainPanel.setBackground(Color.LIGHT_GRAY);
tabbedPane.addTab("Main", null, MainPanel, null);
MainPanel.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 42, 604, 217);
MainPanel.add(scrollPane);
table = new JTable();
scrollPane.setViewportView(table);
String columns[] = {"First Name", "Last Name", "Phone Number", "Email"};
tableModel = new DefaultTableModel(0, 4);
tableModel.setColumnIdentifiers(columns);
table.setModel(tableModel);
JButton button = new JButton("Open Txt");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String line;
JFileChooser fileload = new JFileChooser();
if (fileload.showOpenDialog(Main.this) == JFileChooser.APPROVE_OPTION) {
try {
File file = fileload.getSelectedFile();
FileInputStream fis = new FileInputStream(file);
reader = new BufferedReader(new FileReader(file));
while ((line = reader.readLine()) != null) {
tableModel.addRow(line.split(": "));
}
reader.close();
} catch (IOException e10) {
JOptionPane.showMessageDialog(null, "Buffered Reader issue.");
}
}
}
});
button.setBounds(160, 11, 129, 23);
MainPanel.add(button);
}
public static void main(String[] args) throws Exception {
Main frame = new Main();
frame.setTitle("Phone Book App");
frame.setSize(640, 400);
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}