gpt4 book ai didi

java - 在JAVA中显示JTable中所有反序列化的对象

转载 作者:行者123 更新时间:2023-12-01 14:43:16 24 4
gpt4 key购买 nike

我想显示表格中的所有对象。

我有一个名为/menschen 的文件夹,其中包含如下文件:“firstname.lastname.ser”

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.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

/**
*
* @author
*/
public class Adressverwaltung {

JFrame mainWindow;
final File folder = new File("menschen");

/**
* @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();

}

public String deserialize(String m, int field) {

try {

FileInputStream fin = new FileInputStream("menschen\\" + m);
ObjectInputStream ois = new ObjectInputStream(fin);
Person n = (Person) ois.readObject();
ois.close();

switch (field) {
case 1:
return n.vorname;
case 2:
return n.nachname;
}



} catch (Exception ex) {
ex.printStackTrace();

}
return null;

}

public static void table(String[][] alle) {

String[] columnNames = {
"Vorname", "Nachname"
};
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTable table = new JTable(alle, columnNames);
f.add(new JScrollPane(table));
f.pack();
f.setVisible(true);
}

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) {
}
}

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) {
list(folder);
}
});
}

public void list(final File folder) {
String[][] rowData = new String[3][];

int r = 0;
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
} else {
String name = fileEntry.getName();

rowData[r][0] = deserialize(name, 1);
rowData[r][1] = deserialize(name, 2);

r++;
}
}

table(rowData);
}

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 类:

package adressverwaltung;

/**
*
* @author Mahshid
*/
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;
}
}

我认为 list() 函数有问题。这是我的第一个 Java 代码,所以如果您看到任何大错误,请不要感到惊讶:)

最佳答案

通过创建这样的二维数组:

String[][] rowData = new String[3][];

您正在创建一个数组,其中所有“列”数据均未初始化。您可以通过执行以下操作来亲自检查:

for (String[] s: rowData) {
System.out.println(s);
}

因此,尝试将任何外部数组元素分配给单个 String 是不可能的:

rowData[r][0] = deserialize(name, 1);

关于java - 在JAVA中显示JTable中所有反序列化的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15724243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com