2 Star 0 Fork 0

BESTI.IS.JAVA2018/20165337

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
InputArea1.java 2.53 KB
一键复制 编辑 原始数据 按行查看 历史
yueyuan 提交于 7年前 . week6
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class InputArea extends JPanel implements ActionListener {
File f=null; //存放链表的文件
Box baseBox ,boxV1,boxV2;
JTextField name,mount,price; //为Goods对象提供的视图
JButton button; //控制器
LinkedList<Goods> goodsList; //存放Goods对象的链表
InputArea(File f) {
this.f=f;
goodsList=new LinkedList<Goods>();
name=new JTextField(12);
mount=new JTextField(12);
price=new JTextField(12);
button=new JButton("录入");
button.addActionListener(this);
boxV1=Box.createVerticalBox();
boxV1.add(new JLabel("输入名称"));
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(new JLabel("输入库存"));
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(new JLabel("输入单价"));
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(new JLabel("单击录入"));
boxV2=Box.createVerticalBox();
boxV2.add(name);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(mount);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(price);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(button);
baseBox=Box.createHorizontalBox();
baseBox.add(boxV1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(boxV2);
add(baseBox);
}
public void actionPerformed(ActionEvent e) {
if(f.exists()) {
try{
FileInputStream fi=new FileInputStream(f);
ObjectInputStream oi=new ObjectInputStream(fi);
goodsList= (LinkedList<Goods>)oi.readObject();
fi.close();
oi.close();
Goods goods=new Goods();
goods.setName(name.getText());
goods.setMount(mount.getText());
goods.setPrice(price.getText());
goodsList.add(goods);
FileOutputStream fo=new FileOutputStream(f);
ObjectOutputStream out=new ObjectOutputStream(fo);
out.writeObject(goodsList);
out.close();
}
catch(Exception ee) {}
}
else{
try{
f.createNewFile();
Goods goods=new Goods();
goods.setName(name.getText());
goods.setMount(mount.getText());
goods.setPrice(price.getText());
goodsList.add(goods);
FileOutputStream fo=new FileOutputStream(f);
ObjectOutputStream out=new ObjectOutputStream(fo);
out.writeObject(goodsList);
out.close();
}
catch(Exception ee) {}
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/BESTI-IS-JAVA-2018/20165337.git
git@gitee.com:BESTI-IS-JAVA-2018/20165337.git
BESTI-IS-JAVA-2018
20165337
20165337
master

搜索帮助