- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
现在我需要一些帮助来序列化我的数组列表。现在我已经设法使序列化方面正常工作(至少我认为),现在我的问题在于反序列化对象。我正在制作一个小型地址簿程序。我有一个组合框,用于存储地址,上面有三个文本框,用户在其中输入:姓名、地址和电话号码。出于测试目的,我有一个保存和加载按钮。保存按钮保存联系人,加载按钮加载以前的 session 联系人。现在除了反序列化之外的所有内容都可以工作,我想知道如何继续进行。
我的代码如下:
import java.awt.EventQueue;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class Address_Book {
private JFrame frame;
private JTextField newName;
private JTextField newAddress;
private JTextField newPhoneAddress;
ArrayList<Book> test = new ArrayList<Book>();
ArrayList<Book> array = new ArrayList<Book>();
File addBook = new File("addBook.txt");
final JComboBox<String> comboBox = new JComboBox<String>();
final DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Address_Book window = new Address_Book();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Address_Book() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 250);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
newName = new JTextField();
newName.setBounds(10, 29, 107, 20);
frame.getContentPane().add(newName);
newName.setColumns(10);
JLabel lbl1 = new JLabel("Enter New Name:");
lbl1.setBounds(10, 11, 107, 14);
frame.getContentPane().add(lbl1);
JLabel lbl2 = new JLabel("Enter New Address:");
lbl2.setBounds(136, 11, 130, 14);
frame.getContentPane().add(lbl2);
newAddress = new JTextField();
newAddress.setColumns(10);
newAddress.setBounds(136, 29, 107, 20);
frame.getContentPane().add(newAddress);
newPhoneAddress = new JTextField();
newPhoneAddress.setColumns(10);
newPhoneAddress.setBounds(262, 29, 162, 20);
frame.getContentPane().add(newPhoneAddress);
JLabel lbl3 = new JLabel("Enter New Phone number:");
lbl3.setBounds(262, 11, 162, 14);
frame.getContentPane().add(lbl3);
JButton btnAddNewContact = new JButton("Add new contact");
btnAddNewContact.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent arg0) {
test.add((new Book(newName.getText(), newAddress.getText(), newPhoneAddress.getText())));
//mergesort.mergesort(test, 0, test.size() - 1);
model.removeAllElements();
for(int i=0; i < test.size();i++){
model.addElement(test.get(i).getContact());
}
comboBox.setModel(model);
newName.setText("");
newAddress.setText("");
newPhoneAddress.setText("");
}
});
btnAddNewContact.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnAddNewContact.setBounds(10, 53, 414, 23);
frame.getContentPane().add(btnAddNewContact);
JLabel lbl4 = new JLabel("Current Contacts:");
lbl4.setBounds(10, 87, 107, 14);
frame.getContentPane().add(lbl4);
frame.getContentPane().add(comboBox);
comboBox.setModel(model);
comboBox.setBounds(10, 101, 414, 20);
comboBox.setSelectedIndex(test.size()-1);
JButton btnLoad = new JButton("Load");
btnLoad.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
try {
/* Read objects */
FileInputStream in = new FileInputStream(addBook);
ObjectInputStream readIn = new ObjectInputStream(in);
array = (ArrayList<Book>) readIn.readObject();
readIn.close();
for(int i=0; i < array.size();i++){
model.addElement(array.get(i).getContact());
}
comboBox.setModel(model);
}catch(Exception e1){
e1.printStackTrace();
}
}
});
btnLoad.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnLoad.setBounds(10, 132, 89, 23);
frame.getContentPane().add(btnLoad);
JButton btnSave = new JButton("Save");
btnSave.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent arg0) {
/* write objects */
try{
FileOutputStream out = new FileOutputStream(addBook);
ObjectOutputStream writeAdd = new ObjectOutputStream(out);
writeAdd.writeObject(test);
writeAdd.close();
}catch(Exception e){
}
}
});
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnSave.setBounds(109, 132, 89, 23);
frame.getContentPane().add(btnSave);
}
}
这是我的对象:
public class Book implements Comparable {
private String flName, Address, pNumber;
public Book(String Name, String address, String phoneNumber ){
setFlName(Name);
setAddress(address);
setpNumber(phoneNumber);
}
public String getpNumber() {
return pNumber;
}
public void setpNumber(String pNumber) {
this.pNumber = pNumber;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
Address = address;
}
public String getFlName() {
return flName;
}
public void setFlName(String flName) {
this.flName = flName;
}
public String getContact() {
return flName + ", " + Address + ", " + pNumber;
}
public int compareTo(Object c) {
Book testBook = (Book)c;
if (testBook.getFlName().compareTo(this.getFlName()) < 0){
return(-1);
}else if(testBook.getFlName().compareTo(this.getFlName()) == 0){
return(0);
}else{
return(1);
}
}
}
下一个代码片段位于我的 Address_Book 类中,这是我给您的第一个代码,这只是为了更容易找到我从哪里加载它。
JButton btnLoad = new JButton("Load");
btnLoad.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
try {
/* Read objects */
FileInputStream in = new FileInputStream(addBook);
ObjectInputStream readIn = new ObjectInputStream(in);
array = (ArrayList<Book>) readIn.readObject();
readIn.close();
for(int i=0; i < array.size();i++){
model.addElement(array.get(i).getContact());
}
comboBox.setModel(model);
}catch(Exception e1){
e1.printStackTrace();
}
}
});
btnLoad.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnLoad.setBounds(10, 132, 89, 23);
frame.getContentPane().add(btnLoad);
感谢您抽出宝贵的时间,如果您有任何疑问,请随时询问。 :)
最佳答案
ArrayList<Book>
的主要原因。失败了。关于java - Java GUI 中的数组反序列化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11041453/
我想做的是让 JTextPane 在 JPanel 中占用尽可能多的空间。对于我使用的 UpdateInfoPanel: public class UpdateInfoPanel extends JP
我在 JPanel 中有一个 JTextArea,我想将其与 JScrollPane 一起使用。我正在使用 GridBagLayout。当我运行它时,框架似乎为 JScrollPane 腾出了空间,但
我想在 xcode 中实现以下功能。 我有一个 View Controller 。在这个 UIViewController 中,我有一个 UITabBar。它们下面是一个 UIView。将 UITab
有谁知道Firebird 2.5有没有类似于SQL中“STUFF”函数的功能? 我有一个包含父用户记录的表,另一个表包含与父相关的子用户记录。我希望能够提取用户拥有的“ROLES”的逗号分隔字符串,而
我想使用 JSON 作为 mirth channel 的输入和输出,例如详细信息保存在数据库中或创建 HL7 消息。 简而言之,输入为 JSON 解析它并输出为任何格式。 最佳答案 var objec
通常我会使用 R 并执行 merge.by,但这个文件似乎太大了,部门中的任何一台计算机都无法处理它! (任何从事遗传学工作的人的附加信息)本质上,插补似乎删除了 snp ID 的 rs 数字,我只剩
我有一个以前可能被问过的问题,但我很难找到正确的描述。我希望有人能帮助我。 在下面的代码中,我设置了varprice,我想添加javascript变量accu_id以通过rails在我的数据库中查找记
我有一个简单的 SVG 文件,在 Firefox 中可以正常查看 - 它的一些包装文本使用 foreignObject 包含一些 HTML - 文本包装在 div 中:
所以我正在为学校编写一个 Ruby 程序,如果某个值是 1 或 3,则将 bool 值更改为 true,如果是 0 或 2,则更改为 false。由于我有 Java 背景,所以我认为这段代码应该有效:
我做了什么: 我在这些账户之间创建了 VPC 对等连接 互联网网关也连接到每个 VPC 还配置了路由表(以允许来自双方的流量) 情况1: 当这两个 VPC 在同一个账户中时,我成功测试了从另一个 La
我有一个名为 contacts 的表: user_id contact_id 10294 10295 10294 10293 10293 10294 102
我正在使用 Magento 中的新模板。为避免重复代码,我想为每个产品预览使用相同的子模板。 特别是我做了这样一个展示: $products = Mage::getModel('catalog/pro
“for”是否总是检查协议(protocol)中定义的每个函数中第一个参数的类型? 编辑(改写): 当协议(protocol)方法只有一个参数时,根据该单个参数的类型(直接或任意)找到实现。当协议(p
我想从我的 PHP 代码中调用 JavaScript 函数。我通过使用以下方法实现了这一点: echo ' drawChart($id); '; 这工作正常,但我想从我的 PHP 代码中获取数据,我使
这个问题已经有答案了: Event binding on dynamically created elements? (23 个回答) 已关闭 5 年前。 我有一个动态表单,我想在其中附加一些其他 h
我正在尝试找到一种解决方案,以在 componentDidMount 中的映射项上使用 setState。 我正在使用 GraphQL连同 Gatsby返回许多 data 项目,但要求在特定的 pat
我在 ScrollView 中有一个 View 。只要用户按住该 View ,我想每 80 毫秒调用一次方法。这是我已经实现的: final Runnable vibrate = new Runnab
我用 jni 开发了一个 android 应用程序。我在 GetStringUTFChars 的 dvmDecodeIndirectRef 中得到了一个 dvmabort。我只中止了一次。 为什么会这
当我到达我的 Activity 时,我调用 FragmentPagerAdapter 来处理我的不同选项卡。在我的一个选项卡中,我想显示一个 RecyclerView,但他从未出现过,有了断点,我看到
当我按下 Activity 中的按钮时,会弹出一个 DialogFragment。在对话框 fragment 中,有一个看起来像普通 ListView 的 RecyclerView。 我想要的行为是当
我是一名优秀的程序员,十分优秀!