我必须为我的学校编写一个java应用程序..这是我的第一个java程序,所以如果你看到任何不好的代码,不要被高估!
我的想法是: 1.只有一个窗口 2.在右侧显示一个创建字段 3.每个新人都将保存在名为/ menschen的文件夹中作为“firstname.lastname.ser” 功能序列化已经完成了。 4.显示左侧所有已存在的人员的列表以及搜索的可能性...... 5.删除和更新人
我现在处于第4步,无法完成它...我也认为我的代码中存在一些错误和错误...... :(
我想我必须扫描文件夹/ menschen以获取所有.ser文件并从所有文件中创建一个数组并在窗口中的列表中显示...
所以这是班级:名为Adressverwaltung(德语)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package adressverwaltung;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
// FILE
import java.io.*;
import java.util.*;
import java.util.Formatter;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.awt.HeadlessException;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
/**
*
* @author john.smith
*/
public class Adressverwaltung {
JFrame mainWindow;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Adressverwaltung verweiss = new Adressverwaltung();
verweiss.main();
}
public void main() {
mainWindow = new JFrame();
mainWindow.setBounds(0, 0, 800, 400);
mainWindow.setLocationRelativeTo(null);
mainWindow.setLayout(null);
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.setResizable(false);
mainWindow.setVisible(true);
mainWindow.setLayout(new FlowLayout());
menu();
}
private static void deserialize(Person m) {
Person e = null;
try {
FileInputStream fileIn =
new FileInputStream("menschen\\" + m.vorname + "." + m.nachname + ".ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
e = (Person) in.readObject();
in.close();
fileIn.close();
} catch (IOException i) {
// i.printStackTrace();
return;
} catch (ClassNotFoundException c) {
System.out.println("Mensch class not found");
// c.printStackTrace();
return;
}
System.out.println("Deserialized Employee...");
System.out.println("Vorname: " + e.vorname);
}
private static void serialize(Person m) {
try {
FileOutputStream fileOut =
new FileOutputStream("menschen\\" + m.vorname + "." + m.nachname + ".ser");
ObjectOutputStream out =
new ObjectOutputStream(fileOut);
out.writeObject(m);
out.close();
fileOut.close();
} catch (IOException i) {
i.printStackTrace();
}
}
private static void check(String url) {
File f = new File(url);
if (!f.exists()) {
try {
Formatter format = new Formatter(f);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void menu() {
JPanel list = new JPanel();
list.setBorder( BorderFactory.createLineBorder( Color.black ) );
list.setPreferredSize( new java.awt.Dimension( 400, 360) );
list.setBackground(Color.white);
mainWindow.add(list);
// Wir lassen unseren Dialog anzeigen
mainWindow.setVisible(true);
int ButtonWidth = 100;
int ButtonHeight = 30;
int ButtonTop = 10;
JButton Button1 = new JButton("List all");
JButton Button3 = new JButton("Search");
Button1.setBounds(10, ButtonTop, ButtonWidth, ButtonHeight);
Button3.setBounds(230, ButtonTop, ButtonWidth, ButtonHeight);
list.add(Button1);
list.add(Button3);
mainWindow.add(list);
createForm();
Button1.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
JOptionPane.showMessageDialog(null, "Hello World", "Tutorial 2", JOptionPane.INFORMATION_MESSAGE);
}
});
}
private void createForm() {
JPanel p = new JPanel();
p.setLayout(new GridLayout(3,2));
JButton b = new JButton("Neue Person!");
JLabel vornameLabel = new JLabel("Vorname:");
final JTextField vorname = new JTextField();
JLabel nachnameLabel = new JLabel("Nachname:");
final JTextField nachname = new JTextField();
p.add( vornameLabel );
p.add( vorname );
p.add( nachnameLabel );
p.add( nachname );
p.add( b );
p.setBorder( BorderFactory.createLineBorder( Color.black ) );
p.setPreferredSize( new java.awt.Dimension( 300, 100) );
p.setBackground(Color.white);
mainWindow.add(p);
// Wir lassen unseren Dialog anzeigen
mainWindow.setVisible(true);
b.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
Person m = new Person(vorname.getText(), nachname.getText());
serialize(m);
JOptionPane.showMessageDialog(null, vorname.getText()+"."+nachname.getText()+".ser abgespeichert.", "Tutorial 2", JOptionPane.INFORMATION_MESSAGE);
}
});
}
}
这是Person类..
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package adressverwaltung;
/**
*
* @author Milad
*/
class Person implements java.io.Serializable {
// Allgemeine Kontaktinformationen:
public String vorname;
public String nachname;
// Adresse
public String strasse;
public int hausnummer;
public int postleitzahl;
public String ort;
public String land;
// Telefon
public int mobil;
public int festnetz;
// Email & Kommentar
public String mail;
public String kommentar;
Person(String vorname, String nachname) {
this.vorname = vorname;
this.nachname = nachname;
}
}
您建议的哪种方式是列出,更新和删除工作的最佳方式?我的代码中有任何重大错误吗?
谢谢!