问题描述
这段代码我想实现学生记录的修改和删除,可是里面实现部分实在是乱得写不出来,能不能请会的朋友帮忙指点一下?下面是我目前写好的代码!if部分不会写了!importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.border.*;importjava.io.*;importjava.util.*;publicclassStudentManagementimplementsActionListener{JFrameframe;ButtonGroupbg_sex;JRadioButtonrb_man,rb_woman;JLabellab_name,lab_sex,lab_number,lab_subject;JTextFieldtf_name,tf_number,tf_subject;JButtonb_save,b_edit,b_delete,b_select;StringBufferStudentInformation;Stringsex;Stringrecord="";Stringspecial;publicstaticvoidmain(String[]args){StudentManagementsm=newStudentManagement();sm.go();}publicvoidgo(){frame=newJFrame("学生信息管理系统");lab_name=newJLabel("姓名:");//设置姓名的面板;tf_name=newJTextField(10);JPanelp1=newJPanel();p1.setLayout(newFlowLayout(FlowLayout.LEFT,10,5));p1.add(lab_name);p1.add(tf_name);lab_sex=newJLabel("性别:");//设置性别的面板;bg_sex=newButtonGroup();rb_man=newJRadioButton("男",true);rb_woman=newJRadioButton("女");bg_sex.add(rb_man);bg_sex.add(rb_woman);JPanelp2=newJPanel();p2.setLayout(newFlowLayout(FlowLayout.LEFT,10,5));p2.add(lab_sex);p2.add(rb_man);p2.add(rb_woman);lab_number=newJLabel("学号:");//设置学号的面板;tf_number=newJTextField(15);JPanelp3=newJPanel();p3.setLayout(newFlowLayout(FlowLayout.LEFT,10,5));p3.add(lab_number);p3.add(tf_number);lab_subject=newJLabel("专业班级:");//设置专业班级的面板;tf_subject=newJTextField(15);JPanelp4=newJPanel();p4.setLayout(newFlowLayout(FlowLayout.LEFT,10,5));p4.add(lab_subject);p4.add(tf_subject);JPanelp5=newJPanel();//将各面板加入P5面板中设置在frame的center;p5.setLayout(newGridLayout(0,1));p5.add(p1);p5.add(p2);p5.add(p3);p5.add(p4);Borderetched=BorderFactory.createEtchedBorder();Borderborder=BorderFactory.createTitledBorder(etched,"学生信息管理系统");p5.setBorder(border);frame.getContentPane().add(p5,BorderLayout.CENTER);b_save=newJButton("保存");//设置按钮;b_save.setActionCommand("保存");b_save.addActionListener(this);b_edit=newJButton("修改");b_edit.setActionCommand("修改");b_edit.addActionListener(this);b_delete=newJButton("删除");b_delete.setActionCommand("删除");b_delete.addActionListener(this);b_select=newJButton("查询");b_select.setActionCommand("查询");b_select.addActionListener(this);JPanelp6=newJPanel();//按钮加入P6面板设置在frame的south;p6.setLayout(newFlowLayout(FlowLayout.LEFT,10,5));p6.add(b_save);p6.add(b_edit);p6.add(b_delete);p6.add(b_select);frame.getContentPane().add(p6,BorderLayout.SOUTH);frame.setSize(400,300);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setLocation(200,200);frame.setVisible(true);}publicvoidactionPerformed(ActionEvente){if(e.getActionCommand()=="保存"){//保存信息入myFile.txt;if(rb_woman.isSelected()){sex=newString("女");}else{sex=newString("男");}try{record="姓名:"+tf_name.getText()+"t"+"性别:"+sex+"t"+"学号:"+tf_number.getText()+"t"+"专业班级:"+tf_subject.getText()+"t"+"n";FileWriterout=newFileWriter(newFile("myFile.txt"),true);out.write(record);out.close();JOptionPane.showMessageDialog(null,"保存成功","保存",JOptionPane.PLAIN_MESSAGE);}catch(IOExceptionee){ee.printStackTrace();//错误}}elseif(e.getActionCommand()=="修改"){//修改信息;JOptionPane.showMessageDialog(frame,"请输入您要修改的姓名:","修改",JOptionPane.PLAIN_MESSAGE);try{FileReaderin1=newFileReader("myFile.txt");BufferedReaderreader1=newBufferedReader(in1);Strings1;s1=reader1.readLine();//从myFile文件读出内容赋值给s;Strings2;s2=tf_name.getText();booleanb1=s1.contains(s2);if(b1==true){//判断s中是否包含tf-name.getText(),即用户输入的名字;}}catch(IOExceptione2){e2.printStackTrace();}}elseif(e.getActionCommand()=="删除"){JOptionPane.showMessageDialog(frame,"请输入您要删除的姓名:","删除",JOptionPane.PLAIN_MESSAGE);try{FileReaderin2=newFileReader("myFile.txt");BufferedReaderreader2=newBufferedReader(in2);Strings3;s3=reader2.readLine();//从myFile文件读出内容赋值给s;Strings4;s4=tf_name.getText();booleanb2=s3.contains(s4);if(b2==true){//判断s中是否包含tf-name.getText(),即用户输入的名字;}}catch(IOExceptione2){e2.printStackTrace();}}elseif(e.getActionCommand()=="查询"){JOptionPane.showMessageDialog(frame,"请输入您要查询的姓名:","查询",JOptionPane.PLAIN_MESSAGE);try{FileReaderin3=newFileReader("myFile.txt");BufferedReaderreader3=newBufferedReader(in3);Strings5;s5=reader3.readLine();//从myFile文件读出内容赋值给s;Strings6;s6=tf_name.getText();booleanb3=s5.contains(s6);if(b3==true){//判断s中是否包含tf-name.getText(),即用户输入的名字;}}catch(IOExceptione2){e2.printStackTrace();}}else{}}}
解决方案
本帖最后由 u013179122 于 2013-12-16 20:39:30 编辑