- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
j1.add(button1[i]);
}
Hale Waihona Puke j2=newJPanel();j2.setLayout(newGridLayout(5,1));
for(intj=0;j<button2.length;j++){
button2[j]=newJButton(opretor[j]);
j2.add(button2[j]);
ObjectOutputStreamout=newObjectOutputStream(fo);
out.writeObject(goodsList);
out.close();
}
catch (Exception e1) {
}
}
else{
try{
f.createNewFile();
Goods goods=new Goods();
}
public voidactionPerformed(ActionEvente) {
if(e.getSource()==录入){
card.show(pCenter, "录入");
}
else if(e.getSource()==显示){
try {
FileInputStreamfi = newFileInputStream(file);
voidinit(){
j1=newJPanel();
j1.setLayout(newGridLayout(3, 3));
for(inti=0;i<button1.length;i++){
button1[i]=newJButton(""+(i+1));
button1[i].addActionListener(this);
name=newJTextField(12);
mount=newJTextField(12);
price=newJTextField(12);
button=newJButton("录入");
button.addActionListener(this);
boxv1=Box.createVerticalBox();
i++;
}
table.repaint();
} catch (Exception e1) {
card.show(pCenter, "显示");
}
}
}
}
package货物;
publicclassTest {
publicstaticvoidmain(Stringargs[]){
WindowGoodswin=newWindowGoods();
Iterator<Goods>iter=goodsList.iterator();
inti=0;
while(iter.hasNext()){
Goods 商品=iter.next();
表格单元[i][0]=商品.getName();
表格单元[i][1]=商品.getMount();
表格单元[i][2]=商品.getPrice();
boxv1.add(newJLabel("输入名称"));
boxv1.add(Box.createVerticalStrut(8));
boxv1.add(newJLabel("输入库存"));
boxv1.add(Box.createVerticalStrut(8));
boxv1.add(newJLabel("输入单价"));
实验报告附页
实验内容与步骤
1.ObjectInputStream,ObjectOutputStream,LinkedList,Swing组件类的综合应用
实验要求:
编写一个Java应用程序,实现录入商品信息,将录入信息保存在名字为”库存.txt”文件中,当选择菜单中的显示选项卡时,显示出所录入的商品信息。
this.price= price;
}
}
package 货物;
importjava.awt.*;
importjava.io.*;
importjava.awt.event.*;
importjavax.swing.*;
importjava.util.*;
public classInputAreaextendsJPanelimplementsActionListener{
JPanelj1,j2,j3;
JButtonbutton1[]=newJButton[9],del,
button2[]=newJButton[5];
ButtonGroupgroup=newButtonGroup();
inta,b;
JRadioButtonE,B,S;
Stringopretor[]={"+","-","*","/","="};
表格单元=new Object[length][3];
table=newJTable(表格单元,列名);
pCenter.removeAll();
pCenter.add("录入",inputMessage);
pCenter.add("显示",newJScrollPane(table));
pCenter.validate();
代码如下:
package货物;
publicclassGoodsimplementsjava.io.Serializable{
Stringname,mount,price;
publicStringgetName() {
returnname;
}
publicvoidsetName(String name) {
pCenter.add("录入",inputMessage);
add(pCenter,BorderLayout.CENTER);
setVisible(true);
setBounds(100,50,420,380);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ObjectInputStreamoi=newObjectInputStream(fi);
LinkedList<Goods>goodsList=(LinkedList<Goods>)oi.readObject();
fi.close();
oi.close();
intlength=goodsList.size();
try {
FileInputStreamfi=newFileInputStream(f);
ObjectInputStreamoi=newObjectInputStream(fi);
goodsList=(LinkedList<Goods>)oi.readObject();
fi.close();
oi.close();
ObjectOutputStreamout=newObjectOutputStream(fo);
out.writeObject(goodsList);
out.close();
}
catch (Exception e1) {
}
}
}
}
package 货物;
importjava.io.*;
importjavax.swing.*;
Goods goods=new Goods();
goods.setName(name.getText());
goods.setMount(mount.getText());
goods.setPrice(price.getText());
goodsList.add(goods);
FileOutputStreamfo=newFileOutputStream(f);
File f=null;
Box basebox,boxv1,boxv2;
JTextFieldname,mount,price;
JButtonbutton;
LinkedList<Goods>goodsList;
InputArea(File f){
this.f=f;
goodsList=newLinkedList<Goods>();
button2[j].addActionListener(this);
add(text,BorderLayout.NORTH);
add(j1,BorderLayout.CENTER);
add(j2,BorderLayout.EAST);
}
j3=newJPanel();
E=newJRadioButton("二进制");
JMenuItem录入,显示;
JTextAreashow;
InputAreainputMessage;
JPanelpCenter;
JTabletable;
Object 表格单元[][],列名[]={"名称","库存","单价"};
CardLayoutcard;
WindowGoods(){
file=new File("库存.txt");
= name;
}
publicStringgetMount() {
returnmount;
}
publicvoidsetMount(String mount) {
this.mount= mount;
}
publicStringgetPrice() {
returnprice;
}
publicvoidsetPrice(String price) {
importjava.util.Stack;
importjavax.swing.*;
publicclassJiSuanextendsJFrameimplementsActionListener{
doubleresult=0;
Stringfild="";
JTextFieldtext=newJTextField();
boxv2.add(mount);
boxv2.add(Box.createVerticalStrut(8));
boxv2.add(price);
boxv2.add(Box.createVerticalStrut(8));
boxv2.add(button);
basebox=Box.createHorizontalBox();
win.setTitle("商品管理系统");
}
}
程序运行效果如图所示:
2.JRadioButton,Stack的用法
实验要求:
编写一个Java应用程序,实现如下图布局中计算器的各种进位计数制的相互转换。
代码如下:
package进制转换;
importjava.awt.*;
importjava.awt.event.*;
goods.setName(name.getText());
goods.setMount(mount.getText());
goods.setPrice(price.getText());
goodsList.add(goods);
FileOutputStreamfo=newFileOutputStream(f);
录入.addActionListener(this);
显示.addActionListener(this);
inputMessage=newInputArea(file);
card=newCardLayout();
pCenter=newJPanel();
pCenter.setLayout(card);
importjava.awt.*;
importjava.awt.event.*;
importjava.util.*;
public classWindowGoodsextendsJFrameimplementsActionListener{
File file=null;
JMenuBarbar;
JMenufileMenu;
basebox.add(boxv1);
basebox.add(Box.createHorizontalStrut(10));
basebox.add(boxv2);
add(basebox);
}
public voidactionPerformed(ActionEvente) {
if(f.exists()){
boxv1.add(Box.createVerticalStrut(8));
boxv1.add(newJLabel("单击录入"));
boxv2=Box.createVerticalBox();
boxv2.add(name);
boxv2.add(Box.createVerticalStrut(8));
录入=newJMenuItem("录入");
显示=newJMenuItem("显示");
bar=newJMenuBar();
fileMenu=newJMenu("菜单选项");
fileMenu.add(录入);
fileMenu.add(显示);
bar.add(fileMenu);
setJMenuBar(bar);