SlideShare a Scribd company logo
1 of 4
Download to read offline
由一个简单的程序谈起――之一

                                                                 江苏 无锡 缪小东
这是一个网友的程序!该程序主要完成――从持续层(本地文件系统)删除某个记录。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;

public class Delete extends JPanel implements ActionListener {
     Hashtable 基本信息表=null;
     JTextField 学号,姓名,专业,年级,出生;
     JRadioButton 男,女;
     JButton 删除;
     ButtonGroup group=null;
     FileInputStream inOne=null;
     ObjectInputStream inTwo=null;
     FileOutputStream outOne=null;
     ObjectOutputStream outTwo=null;
     File file=null;

    public Delete(File file) {
         this.file=file;
         学号=new JTextField(10);
         删除=new JButton("删除");
         学号.addActionListener(this);
         删除.addActionListener(this);
         姓名=new JTextField(10);
         姓名.setEditable(false);
         专业=new JTextField(10);
         专业.setEditable(false);
         年级=new JTextField(10);
         年级.setEditable(false);
         出生=new JTextField(10);
         出生.setEditable(false);
         男=new JRadioButton("男",false);
         女=new JRadioButton("女",false);
         group=new ButtonGroup();
         group.add(男);
         group.add(女);
         Box box1=Box.createHorizontalBox();
         box1.add(new JLabel("输入要删除的学号:",JLabel.CENTER));
         box1.add(学号);         box1.add(删除);
                                               1
Box box2=Box.createHorizontalBox();
    box2.add(new JLabel("姓名:",JLabel.CENTER));
    box2.add(姓名);
    Box box3=Box.createHorizontalBox();
    box3.add(new JLabel("性别:",JLabel.CENTER));
    box3.add(男);
    box3.add(女);
    Box box4=Box.createHorizontalBox();
    box4.add(new JLabel("专业:",JLabel.CENTER));
    box4.add(专业);
    Box box5=Box.createHorizontalBox();
    box5.add(new JLabel("年级:",JLabel.CENTER));
    box5.add(年级);
    Box box6=Box.createHorizontalBox();
    box6.add(new JLabel("出生:",JLabel.CENTER));
    box6.add(出生);
    Box boxH=Box.createVerticalBox();
    boxH.add(box1);
    boxH.add(box2);
    boxH.add(box3);
    boxH.add(box4);
    boxH.add(box5);
    boxH.add(box6);
    boxH.add(Box.createVerticalGlue());
    JPanel pCenter=new JPanel();
    pCenter.add(boxH);
    setLayout(new BorderLayout());
    add(pCenter,BorderLayout.CENTER);
    validate();
}

public void actionPerformed(ActionEvent e) {
     if(e.getSource()==删除||e.getSource()==学号) {
           String number="";
           number=学号.getText();
           if(number.length()>0) {
                try {
                      inOne=new FileInputStream(file);
                      inTwo=new ObjectInputStream(inOne);
                      基本信息表=(Hashtable)inTwo.readObject();
                      inOne.close();
                      inTwo.close();
                }catch(Exception ee){ }
                if(基本信息表.containsKey(number)) {
                      Student stu=(Student)基本信息表.get(number);
                      姓名.setText(stu.getName());
                      专业.setText(stu.getDisciping());
                                      2
年级.setText(stu.getGrade());
                       出生.setText(stu.getBorth());
                       if(stu.getSex().equals("男")) {
                             男.setSelected(true);
                       }else{
                             女.setSelected(true);
                       }
                       String m="确定要删除该学号及全部信息吗?";
                       int           ok=JOptionPane.showConfirmDialog(this,m,"    确   认
",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
                       if(ok==JOptionPane.YES_OPTION) {
                             基本信息表.remove(number);
                             try {
                                   outOne=new FileOutputStream(file);
                                   outTwo=new ObjectOutputStream(outOne);
                                   outTwo.writeObject(基本信息表);
                                   outTwo.close();
                                   outOne.close();
                                   学号.setText(null);
                                   姓名.setText(null);
                                   专业.setText(null);
                                   年级.setText(null);
                                   出生.setText(null);
                             }catch(Exception ee){
                                   System.out.println(ee);
                             }
                       }else if(ok==JOptionPane.NO_OPTION){
                             学号.setText(null);
                             姓名.setText(null);
                             专业.setText(null);
                             年级.setText(null);
                             出生.setText(null);
                       }
                  }else{
                       String warning="该学号不存在!";
                       JOptionPane.showMessageDialog(this,warning,"             警     告
",JOptionPane.WARNING_MESSAGE);
                  }
             } else{
                  String warning="必须要输入学号!";
                  JOptionPane.showMessageDialog(this,warning,"                 警      告
",JOptionPane.WARNING_MESSAGE);
             }
         }
    }
  }

                                           3
与本程序相关的还有增加、修改……几个模块。这几个模块组合完成一个完整的数据
库操作。程序中有很多设计方面的问题。下面的文章我将对该程序谈谈自己的看法!请大
家笑纳!不对之处请指正!



更多精彩请关注:
  http://blog.163.com/miaoxiaodong78/




                   4

More Related Content

What's hot

LazyRecord: The Fast ORM for PHP
LazyRecord: The Fast ORM for PHPLazyRecord: The Fast ORM for PHP
LazyRecord: The Fast ORM for PHPLin Yo-An
 
Python learn guide
Python learn guidePython learn guide
Python learn guiderobin yang
 
Keep your code clean
Keep your code cleanKeep your code clean
Keep your code cleanmacrochen
 
由一个简单的程序谈起--之四
由一个简单的程序谈起--之四由一个简单的程序谈起--之四
由一个简单的程序谈起--之四yiditushe
 
Yui3入门
Yui3入门Yui3入门
Yui3入门cly84920
 

What's hot (6)

Python元組,字典,集合
Python元組,字典,集合Python元組,字典,集合
Python元組,字典,集合
 
LazyRecord: The Fast ORM for PHP
LazyRecord: The Fast ORM for PHPLazyRecord: The Fast ORM for PHP
LazyRecord: The Fast ORM for PHP
 
Python learn guide
Python learn guidePython learn guide
Python learn guide
 
Keep your code clean
Keep your code cleanKeep your code clean
Keep your code clean
 
由一个简单的程序谈起--之四
由一个简单的程序谈起--之四由一个简单的程序谈起--之四
由一个简单的程序谈起--之四
 
Yui3入门
Yui3入门Yui3入门
Yui3入门
 

More from yiditushe

Spring入门纲要
Spring入门纲要Spring入门纲要
Spring入门纲要yiditushe
 
J Bpm4 1中文用户手册
J Bpm4 1中文用户手册J Bpm4 1中文用户手册
J Bpm4 1中文用户手册yiditushe
 
性能测试实践2
性能测试实践2性能测试实践2
性能测试实践2yiditushe
 
性能测试实践1
性能测试实践1性能测试实践1
性能测试实践1yiditushe
 
性能测试技术
性能测试技术性能测试技术
性能测试技术yiditushe
 
Load runner测试技术
Load runner测试技术Load runner测试技术
Load runner测试技术yiditushe
 
J2 ee性能测试
J2 ee性能测试J2 ee性能测试
J2 ee性能测试yiditushe
 
面向对象的Js培训
面向对象的Js培训面向对象的Js培训
面向对象的Js培训yiditushe
 
Flex3中文教程
Flex3中文教程Flex3中文教程
Flex3中文教程yiditushe
 
开放源代码的全文检索Lucene
开放源代码的全文检索Lucene开放源代码的全文检索Lucene
开放源代码的全文检索Luceneyiditushe
 
基于分词索引的全文检索技术介绍
基于分词索引的全文检索技术介绍基于分词索引的全文检索技术介绍
基于分词索引的全文检索技术介绍yiditushe
 
Lucene In Action
Lucene In ActionLucene In Action
Lucene In Actionyiditushe
 
Lucene2 4学习笔记1
Lucene2 4学习笔记1Lucene2 4学习笔记1
Lucene2 4学习笔记1yiditushe
 
Lucene2 4 Demo
Lucene2 4 DemoLucene2 4 Demo
Lucene2 4 Demoyiditushe
 
Lucene 全文检索实践
Lucene 全文检索实践Lucene 全文检索实践
Lucene 全文检索实践yiditushe
 
Lucene 3[1] 0 原理与代码分析
Lucene 3[1] 0 原理与代码分析Lucene 3[1] 0 原理与代码分析
Lucene 3[1] 0 原理与代码分析yiditushe
 
7 面向对象设计原则
7 面向对象设计原则7 面向对象设计原则
7 面向对象设计原则yiditushe
 
10 团队开发
10  团队开发10  团队开发
10 团队开发yiditushe
 
9 对象持久化与数据建模
9  对象持久化与数据建模9  对象持久化与数据建模
9 对象持久化与数据建模yiditushe
 
8 Uml构架建模
8  Uml构架建模8  Uml构架建模
8 Uml构架建模yiditushe
 

More from yiditushe (20)

Spring入门纲要
Spring入门纲要Spring入门纲要
Spring入门纲要
 
J Bpm4 1中文用户手册
J Bpm4 1中文用户手册J Bpm4 1中文用户手册
J Bpm4 1中文用户手册
 
性能测试实践2
性能测试实践2性能测试实践2
性能测试实践2
 
性能测试实践1
性能测试实践1性能测试实践1
性能测试实践1
 
性能测试技术
性能测试技术性能测试技术
性能测试技术
 
Load runner测试技术
Load runner测试技术Load runner测试技术
Load runner测试技术
 
J2 ee性能测试
J2 ee性能测试J2 ee性能测试
J2 ee性能测试
 
面向对象的Js培训
面向对象的Js培训面向对象的Js培训
面向对象的Js培训
 
Flex3中文教程
Flex3中文教程Flex3中文教程
Flex3中文教程
 
开放源代码的全文检索Lucene
开放源代码的全文检索Lucene开放源代码的全文检索Lucene
开放源代码的全文检索Lucene
 
基于分词索引的全文检索技术介绍
基于分词索引的全文检索技术介绍基于分词索引的全文检索技术介绍
基于分词索引的全文检索技术介绍
 
Lucene In Action
Lucene In ActionLucene In Action
Lucene In Action
 
Lucene2 4学习笔记1
Lucene2 4学习笔记1Lucene2 4学习笔记1
Lucene2 4学习笔记1
 
Lucene2 4 Demo
Lucene2 4 DemoLucene2 4 Demo
Lucene2 4 Demo
 
Lucene 全文检索实践
Lucene 全文检索实践Lucene 全文检索实践
Lucene 全文检索实践
 
Lucene 3[1] 0 原理与代码分析
Lucene 3[1] 0 原理与代码分析Lucene 3[1] 0 原理与代码分析
Lucene 3[1] 0 原理与代码分析
 
7 面向对象设计原则
7 面向对象设计原则7 面向对象设计原则
7 面向对象设计原则
 
10 团队开发
10  团队开发10  团队开发
10 团队开发
 
9 对象持久化与数据建模
9  对象持久化与数据建模9  对象持久化与数据建模
9 对象持久化与数据建模
 
8 Uml构架建模
8  Uml构架建模8  Uml构架建模
8 Uml构架建模
 

由一个简单的程序谈起--之一

  • 1. 由一个简单的程序谈起――之一 江苏 无锡 缪小东 这是一个网友的程序!该程序主要完成――从持续层(本地文件系统)删除某个记录。 import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.util.*; public class Delete extends JPanel implements ActionListener { Hashtable 基本信息表=null; JTextField 学号,姓名,专业,年级,出生; JRadioButton 男,女; JButton 删除; ButtonGroup group=null; FileInputStream inOne=null; ObjectInputStream inTwo=null; FileOutputStream outOne=null; ObjectOutputStream outTwo=null; File file=null; public Delete(File file) { this.file=file; 学号=new JTextField(10); 删除=new JButton("删除"); 学号.addActionListener(this); 删除.addActionListener(this); 姓名=new JTextField(10); 姓名.setEditable(false); 专业=new JTextField(10); 专业.setEditable(false); 年级=new JTextField(10); 年级.setEditable(false); 出生=new JTextField(10); 出生.setEditable(false); 男=new JRadioButton("男",false); 女=new JRadioButton("女",false); group=new ButtonGroup(); group.add(男); group.add(女); Box box1=Box.createHorizontalBox(); box1.add(new JLabel("输入要删除的学号:",JLabel.CENTER)); box1.add(学号); box1.add(删除); 1
  • 2. Box box2=Box.createHorizontalBox(); box2.add(new JLabel("姓名:",JLabel.CENTER)); box2.add(姓名); Box box3=Box.createHorizontalBox(); box3.add(new JLabel("性别:",JLabel.CENTER)); box3.add(男); box3.add(女); Box box4=Box.createHorizontalBox(); box4.add(new JLabel("专业:",JLabel.CENTER)); box4.add(专业); Box box5=Box.createHorizontalBox(); box5.add(new JLabel("年级:",JLabel.CENTER)); box5.add(年级); Box box6=Box.createHorizontalBox(); box6.add(new JLabel("出生:",JLabel.CENTER)); box6.add(出生); Box boxH=Box.createVerticalBox(); boxH.add(box1); boxH.add(box2); boxH.add(box3); boxH.add(box4); boxH.add(box5); boxH.add(box6); boxH.add(Box.createVerticalGlue()); JPanel pCenter=new JPanel(); pCenter.add(boxH); setLayout(new BorderLayout()); add(pCenter,BorderLayout.CENTER); validate(); } public void actionPerformed(ActionEvent e) { if(e.getSource()==删除||e.getSource()==学号) { String number=""; number=学号.getText(); if(number.length()>0) { try { inOne=new FileInputStream(file); inTwo=new ObjectInputStream(inOne); 基本信息表=(Hashtable)inTwo.readObject(); inOne.close(); inTwo.close(); }catch(Exception ee){ } if(基本信息表.containsKey(number)) { Student stu=(Student)基本信息表.get(number); 姓名.setText(stu.getName()); 专业.setText(stu.getDisciping()); 2
  • 3. 年级.setText(stu.getGrade()); 出生.setText(stu.getBorth()); if(stu.getSex().equals("男")) { 男.setSelected(true); }else{ 女.setSelected(true); } String m="确定要删除该学号及全部信息吗?"; int ok=JOptionPane.showConfirmDialog(this,m," 确 认 ",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE); if(ok==JOptionPane.YES_OPTION) { 基本信息表.remove(number); try { outOne=new FileOutputStream(file); outTwo=new ObjectOutputStream(outOne); outTwo.writeObject(基本信息表); outTwo.close(); outOne.close(); 学号.setText(null); 姓名.setText(null); 专业.setText(null); 年级.setText(null); 出生.setText(null); }catch(Exception ee){ System.out.println(ee); } }else if(ok==JOptionPane.NO_OPTION){ 学号.setText(null); 姓名.setText(null); 专业.setText(null); 年级.setText(null); 出生.setText(null); } }else{ String warning="该学号不存在!"; JOptionPane.showMessageDialog(this,warning," 警 告 ",JOptionPane.WARNING_MESSAGE); } } else{ String warning="必须要输入学号!"; JOptionPane.showMessageDialog(this,warning," 警 告 ",JOptionPane.WARNING_MESSAGE); } } } } 3