java测试

import java.awt.Toolkit;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.table.AbstractTableModel;
import java.util.*;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import javax.swing.JTextArea;
import java.io.*;
import javax.swing.JFileChooser;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.File.*;
import javax.swing.JComboBox;
import javax.swing.table.TableColumnModel;
import javax.swing.DefaultCellEditor;
import javax.swing.filechooser.FileFilter.*;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Dimension;
import java.io.FileWriter;
import java.io.BufferedWriter;
/**
* 学生信息管理表格
* Student.java
* auther **
* updataTime 2006-11-21
*/

class Studentframe extends JFrame
{
 
    JFrame frame = new JFrame("学生管理");
    JButton jbtOpen;
    JButton jbtInsert;
    JButton jbtDel;
    JButton jbtClearAll;
    JButton jbtSave;
    File ursefile;
    //定义滚动面版
    JScrollPane   s_pan;
    JScrollPane   t_pan;
    JPanel pane;
    //定义文本区
    JTextArea jta;
    //定义表格
    mytable model;
    JTable  table;
    //输入框
    JLabel jlid;
    JLabel jlname;
    JLabel jlage;
    JLabel jlsex;
    JPanel jl_pan;  
    JTextField jtid;
    JTextField jtname;
    JTextField jtage;
    JPanel jt_pan;
    JButton jbtc;
    JPanel jb_pan;
    JPanel input_pan;
    JComboBox cmbsex;       
    //输入框 
    public void inputinint()
    {
        jlid = new JLabel ("学号");
        jlname = new JLabel ("姓名");
        jlage = new  JLabel ("年龄");
        jlsex = new JLabel("性别");
        jl_pan = new JPanel();
        jl_pan.add(jlid);
        jl_pan.add(jlname);
        jl_pan.add(jlage);
        jl_pan.add(jlsex);
       
        jtid = new JTextField();
        jtid.setColumns(5);
        jtname = new JTextField();
        jtname.setColumns(8);
        jtage = new JTextField();
        jtage.setColumns(3);
        String[] sexy = {"female","male"};
        cmbsex= new JComboBox(sexy);
        jt_pan = new JPanel();
        jt_pan.add(jtid);
        jt_pan.add(jtname);
        jt_pan.add(jtage);
        jt_pan.add(cmbsex);
        jbtc = new JButton("确定");
        jb_pan = new JPanel();
        jb_pan.add(jbtc);
        input_pan = new JPanel();
        input_pan.add(jl_pan);
        input_pan.add(jt_pan);
        input_pan.add(jb_pan);
        frame.remove(t_pan);
        frame.add(input_pan,BorderLayout.CENTER);
        frame.repaint();
        frame.setVisible(true);
        jbtc.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e)
        {  
           int len = jtid.getText().length();
           boolean stID = true;
           int len1 = jtage.getText().length();
           boolean stage= true;
           for(int i = 0; i < len; i++)
           {
              if(!Character.isDigit(jtid.getText().charAt(i)))
              stID = false;
           }
           for(int i = 0; i < len1; i++)
           {
              if(!Character.isDigit(jtage.getText().charAt(i)))
              stage =  false;
           }    
           
            if((stID) && (stage) && (jtname.getText().length()>0)&&(cmbsex.getSelectedItem().toString().length()>0) )
             {
               Student stu = new Student(Integer.parseInt(jtid.getText()),jtname.getText(),Integer.parseInt(jtage.getText()),cmbsex.getSelectedItem().toString());
               if(insertIsvisible(jtid.getText()))
               {  model.addRow(stu);
               }else{
                JOptionPane.showMessageDialog(frame,"学号有重复,录入失败");
               }
             }
             else{
               JOptionPane.showMessageDialog(frame,"输入错误,录入失败");
             }       
            frame.remove(input_pan);
            frame.add(t_pan,BorderLayout.CENTER);
            table.updateUI();
            getjtaText();    
            frame.repaint();
            frame.setVisible(true);
                      
        }
        });
     
    }
    public void inint()
    {
       //定义按钮并加载
       jbtOpen = new  JButton("打开");
       jbtInsert = new  JButton("插入");
       jbtDel = new  JButton("删除");
       jbtClearAll = new  JButton("清除");
       jbtSave = new  JButton("保存");
       pane = new JPanel();
       pane.add(jbtOpen);
       pane.add(jbtInsert);
       pane.add(jbtDel);
       pane.add(jbtClearAll);
       pane.add(jbtSave);
       frame.getContentPane().add(pane,BorderLayout.NORTH);
       //定义table相关的并加载
       model = new mytable();
       table = new JTable(model);
       //定义下拉框
       String[] sexy = {"female","male"};
       JComboBox cmb= new JComboBox(sexy);
       TableColumnModel tcm = table.getColumnModel();
       tcm.getColumn(3).setCellEditor(new DefaultCellEditor(cmb));
       t_pan = new JScrollPane(table);
       frame.getContentPane().add(t_pan,BorderLayout.CENTER);
       //定义文本显示区域并加载
       jta = new JTextArea(6,10);
       jta.setLineWrap(true);
       s_pan = new JScrollPane(jta);
       frame.getContentPane().add(s_pan,BorderLayout.SOUTH);
       //jbtOpen添加事件监听器
       jbtOpen.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent e)
        {  
            getData();
            if(OpenIsvisibleData())
            {
                getjtaText();
            }else{
                clearAll();
                JOptionPane.showMessageDialog(frame,"文件打开失败/n可能是文件内学号有重复");                          
            }
         }
        });
        //jbtDel添加事件监听器
        jbtDel.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e)
        {
            if(table.getRowCount() > 0){
               delData();
            }else{
               return;
            }
        }
        });
        //jbtClearAll添加事件监听器
       jbtClearAll.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e)
        {
            clearAll();
        }
        });
        //jbtInsert添加事件监听器
       jbtInsert.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e)
        {
            insert();
        }
        }); 
        //jbtSave添加事件监听器
        jbtSave.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e)
        {
            savedata();
            getjtaText();
            JOptionPane.showMessageDialog(frame,"数据已经成功保存");
        }
        }); 

       frame.setSize(400,400);
       frame.setVisible(true);
    }
     //保存文件方法
     public void savedata()
     {
         try{
              FileWriter fw = new FileWriter ("student.stu");
              BufferedWriter bw = new BufferedWriter (fw);
              int rows = table.getRowCount();
              int cols = table.getColumnCount();
              for(int i = 0; i < rows; i++)
              {
                  String str = "";
                  for(int j = 0; j < cols;j++)
                  {
                     if(str.length()==0)
                     {
                       str = model.getValueAt(i,j).toString(); 
                     }else{
                       str = str+"_"+model.getValueAt(i,j).toString();
                     }
                  }
                     bw.write(str);
                     bw.newLine();
              }
              bw.flush();
              fw.close();
            }catch(Exception se)
            {
                 se.printStackTrace();
            }
     }
    //insert方法 
    public void insert()
    {
      inputinint();
    }
    //clearAll方法
    public void clearAll()
    {
      model.clear(); 
      table.updateUI();
      getjtaText();   
    }
    //delData方法
    public void delData()
    {
       model.removeRow(table.getSelectedRow(),table.getSelectedRowCount()-1);
       table.updateUI();
        getjtaText();   
    }
    //getData方法
    public  void getData()
    {
        getFile();
    }
    //getFile方法
    public void getFile()
    {
       JFileChooser fc = new JFileChooser();
       fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
       int c = fc.showOpenDialog(frame);
       if(c == JFileChooser.CANCEL_OPTION)
       {
          return;
       }else
       {
         File f= fc.getSelectedFile();
         ursefile = f;
         if(f.getName().toString().equals("student.stu")){
             clearAll();
             openFiletext(f);
         }else{
             JOptionPane.showMessageDialog(frame,"文件打开失败/n请确认文件是student.stu");                          
             return;
        }       
       }     
    }
   //openFiletext方法
     public void openFiletext(File f)
     {
       try{
           FileReader fr = new FileReader (f);
           BufferedReader br = new BufferedReader (fr);
           String line = br.readLine();
           while(line != null)
           {
              String[] fileds = line.split("_");
              Student stu = new Student(Integer.parseInt(fileds[0]),fileds[1],Integer.parseInt(fileds[2]),fileds[3]);
              model.addRow(stu);
              table.updateUI();
              line = br.readLine();                   
           } 
             br.close();
             fr.close();                       
           }catch(Exception fe){
              fe.printStackTrace();
           }
       }
 //获得文本显示方法
    public  void getjtaText()
      {
          jta.setText("");
          if(table.getRowCount() >0){
             for(int i = 0; i < table.getRowCount();i++)
             {
                String str = "";
                for(int j = 0; j < table.getColumnCount(); j++)
                {
                   if(str.length() == 0)
                   {
                      str = table.getValueAt(i,j).toString();
                   }else{
                       str = str+"   "+table.getValueAt(i,j).toString();
                   }
                }
                jta.setText(jta.getText()+str+"/n/r");
             }
           jta.setText(jta.getText()+"共有"+table.getRowCount()+"条记录");
          }else
          {
             jta.setText("");
          }
      }
 //判断打开的数据是否有效(学号)
     public boolean OpenIsvisibleData()
     {
       for(int i= 0; i < table.getRowCount();i++)
       {
           for(int j = i+1; j < table.getRowCount();j++)
           {
               if(table.getValueAt(i,0).toString().equals(table.getValueAt(j,0).toString()))
               {
                 return false;
               }
           }
       }
       return true;
     }
      //判断输入的数据是否有效(学号)
     public boolean insertIsvisible(String stuid)
    {
       for(int i = 0; i < table.getRowCount();i++)
       {
            if(table.getValueAt(i,0).toString().equals(stuid))
            {
               return false;
            }
       }
      return true;
    }
 }
 
 class studentIn
 {
      public static void main(String[] args)
    {
        Studentframe maintable = new Studentframe();
        try{
             maintable.inint();
           }catch(Exception e)
           {
                e.printStackTrace();
            }finally{
                maintable.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         
           }
    }
 }


class mytable extends AbstractTableModel
{
   Vector content = null;
   String[] title = {"学号","姓名","年龄","性别"};
   mytable()
   {
       content = new Vector();
   }
  
   public String getColumnName(int col)
   {
     return title[col];
   }
   public int getRowCount()
   {
     return content.size();
   }  
   public int getColumnCount()
   {
     return title.length;
   }
   public boolean isCellEditable(int row,int col)
   {
      if(col == 0)
      return false;
      else
      return true;
   }
   public void setValueAt(Object value, int row,int col)
   {
      ((Vector)content.get(row)).remove(col);
      ((Vector)content.get(row)).add(col,value);
      this.fireTableCellUpdated(row,col);
   }
   public void addRow(Student stu)
   {
     Vector v = new Vector();
     v.add(0,new Integer(stu.stuId));
     v.add(1,stu.name);
     v.add(2,new Integer(stu.age));
     v.add(3,stu.sex);
     content.add(v);     
   }
   public void clear()
   {
      for (int i = content.size()-1; i >=0 ; i--)
      {
         content.remove(i);  
      }
   }
   public void removeRow(int frist,int count)
   {
      for(int i =frist+count; i >= frist; i--)
      {
          content.remove(i);
      }
   }
   public Object getValueAt(int row,int col)
   {
      return ((Vector)content.get(row)).get(col);
   } 
}

class Student
{
   int  stuId ;
   String name;
   int age;
   String sex;

   Student(int stuId,String name,int age,String sex)
   {
       this.name = name;
       this.stuId =stuId;
       this.age = age;
       this.sex = sex;
    }
}

 

//需要一个student.stu的文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值