⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stuinfosystem.java

📁 简单的学生管理系统C/S结构的
💻 JAVA
字号:
package stuinfosystem;

import java.awt.event.*;
import java.awt.Image;
import javax.swing.table.*;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import java.util.Vector;
import java.awt.Dimension;
import java.net.Socket;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Enumeration;
import javax.swing.ScrollPaneLayout;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JToolBar;
import javax.swing.JButton;
import java.net.URL;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class StuInfoSystem extends JFrame {
	
	private Vector vector = null;
    private String data [][] = new String[0][0];      
    private MyTableModel tableModel = new MyTableModel(data,MyConstants.TITLENAMES);
	private JTable table = new JTable(tableModel);
    private JScrollPane jsp = new JScrollPane(table);
    private Socket s = null;
    
    
     public StuInfoSystem() {
        
        //菜单这块       
        JMenuBar menuBar = new JMenuBar();
        JMenu menuFile = new JMenu();
        JMenuItem menuFileExit = new JMenuItem();
        
        menuFile.setText("文件");
        menuFileExit.setText("关闭");
        
        // Add action listener.for the menu button
        menuFileExit.addActionListener
        (
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    shutDown();
                }
            }
        ); 
        menuFile.add(menuFileExit);
        menuBar.add(menuFile);
        
        setTitle("基于C/S模式的简单学生管理系统");
        setJMenuBar(menuBar);
        setSize(new Dimension(MyConstants.WIDTH,MyConstants.HEIGHT));
        //窗口居中
        this.setLocationRelativeTo(this);
        
        //设置窗口图标
        Image img = getToolkit().getImage("images/title.gif");
        this.setIconImage(img);
        
        // Add window listener.
        this.addWindowListener
        (
            new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                	shutDown();
                }
            }
        );  
         
         //要先创建表格       
        createTable();
        
        //布局
        JPanel jp = new JPanel(new BorderLayout());
        jp.setSize(new Dimension(MyConstants.WIDTH,MyConstants.HEIGHT));
        //工具栏
        JToolBar toolbar = new JToolBar("快捷工具栏");
        makeToolbar(toolbar);
        
        jp.add(toolbar);
      	jp.add(jsp,BorderLayout.SOUTH);
      	
      	this.add(jp);
      	this.pack();
		
    }
    
    //构造表格
    public void createTable()
    {
    	//链接服务器先
   		connectServer();   	  	
      
      	//如果vector 不为null,则遍历数据
      	if(!vector.isEmpty())
      	{
      		Enumeration e = vector.elements();
	 		while(e.hasMoreElements())
			{
				Vector v = (Vector)e.nextElement();
				tableModel.addRow(v);
			}
		}
		//设置表格宽高
        table.setPreferredScrollableViewportSize(new Dimension(MyConstants.WIDTH,MyConstants.HEIGHT/2));
              
        
    }
    
    //关闭窗口代码
    public void shutDown()
    {
    	setVisible(false);
        dispose();
        //关闭服务器连接
        unLinkServer();
        System.exit(0);
    }
    
    //连接服务器
    public void connectServer()
    {	
    	try
    	{
    		s = new Socket(MyConstants.SERVERIP,MyConstants.SERVERPORT);
    		
    		OutputStream os = s.getOutputStream();
    		ObjectOutputStream oos = new ObjectOutputStream(os);
    		
    		//第一次请求。不发送数据
    		oos.writeObject(null);    		
    		
    		InputStream is = s.getInputStream(); 
    		    		
    		try
			{	    		
				ObjectInputStream ois = new ObjectInputStream(is);
				vector = (Vector)ois.readObject();					
			}
			catch(Exception e)
			{
				JOptionPane.showMessageDialog(this,e.getMessage());
			}
			finally
			{
				byte buf[] = new byte[32];
				int len = is.read();
				if (len>0)
				{
					byte b[] = new byte[len];
					int length = is.read(b);
					JOptionPane.showMessageDialog(this,new String(b,0,length));
				}
				
				
				oos.close();
				s.close(); 				
			}
    		     		 			
    	}	
    	catch(Exception ex)
    	{
    		JOptionPane.showMessageDialog(this,ex.getMessage());    		
    	}    			
    }
    
    //关闭服务器连接
    public void unLinkServer()
    {
    	try
    	{
    		s.close();
    	}
    	catch(Exception e)
    	{
    		JOptionPane.showMessageDialog(this,e.getMessage()); 
    	}
    }
    
  	//创建一个按钮方法
	private JButton makeButton(String imageName,String altText)
     {
        
        String imgLocation = "images/" + imageName + ".gif";
        //定位图片.以下方式图片要放到stuinfosystem包中
        /*
        URL imageURL = StuInfoSystem.class.getResource(imgLocation);
        
        
       */
       //以下方式则放在包外
       Image imageURL = getToolkit().getImage(imgLocation);
        //创建和初始化按钮
        JButton button = new JButton();
        button.setToolTipText(altText);
        
        if(imageURL != null) 
        {   
        	//图片存在
            button.setIcon(new ImageIcon(imageURL, altText));
        } 
        else 
        {   
        	//图片没有找到
            button.setText(altText);
            System.err.println("图片找不到 "+ imgLocation);
        }

        return button;
    }
    
  	//创建工具栏方法  
  	private void makeToolbar(JToolBar toolbar)
  	{
  		//插入按钮
		JButton jbInsert = makeButton("insert","插入数据");
		jbInsert.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				new InsertFrame(tableModel,StuInfoSystem.this);
			}
		}
		);
		toolbar.add(jbInsert);
		
		toolbar.addSeparator(new Dimension(2,2));
		//更新按钮
		JButton jbUpdate = makeButton("update","更新数据");
		toolbar.add(jbUpdate);
		jbUpdate.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if(StuInfoSystem.this.table.getSelectedRow()==-1)
				{
					JOptionPane.showMessageDialog(StuInfoSystem.this,"请选择要编辑的数据行");					
				}
				else
				{
					new UpdateFrame(table,StuInfoSystem.this);
				}
			}
		}
		);
		
		toolbar.addSeparator(new Dimension(2,2));
		
		//查找按钮
		JButton jbSearch = makeButton("search","查找数据");
		jbSearch.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				new SearchFrame(table,StuInfoSystem.this);
			}
		}
		);

		toolbar.add(jbSearch);
		toolbar.addSeparator(new Dimension(2,2));
		toolbar.addSeparator(new Dimension(2,2));
		
		//查找全部数据按钮
		JButton jbAll = makeButton("all","全部数据");
		jbAll.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				int total = tableModel.getRowCount();
							
				for(int i=total-1;i>=0;i--)
				{
					tableModel.removeRow(i);
				}
				createTable();
			}
		}
		);

		toolbar.add(jbAll);		
		toolbar.addSeparator();
		//删除按钮
		JButton jbDel = makeButton("delete","删除数据");
		jbDel.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				int row = StuInfoSystem.this.table.getSelectedRow();
				if(row==-1)
				{

					JOptionPane.showMessageDialog(StuInfoSystem.this,"请选择要删除的数据行");
					return;					
				}
				else
				{
					int key = JOptionPane.showConfirmDialog(StuInfoSystem.this,"确定要删除学号为"+StuInfoSystem.this.tableModel.getValueAt(row,0)+"的记录吗?","警告",JOptionPane.YES_NO_OPTION);
					if(key==0)//要删除
					{
						Vector v = new Vector();
						//加入操作指示,表示插入数据的1
						v.addElement(Integer.toString(MyConstants.DELETE_KEY));
				
						v.addElement(StuInfoSystem.this.tableModel.getValueAt(row,0));				
			
						
						try
						{
							Socket s = new Socket(MyConstants.SERVERIP,MyConstants.SERVERPORT);
					
							try
							{
								OutputStream os = s.getOutputStream();
								ObjectOutputStream oos = new ObjectOutputStream(os);
								oos.writeObject(v);
						
							}	
							catch(Exception e1)
							{
								JOptionPane.showMessageDialog(StuInfoSystem.this,e1.getMessage());								
							}
							finally
							{
								InputStream is = s.getInputStream();
								byte buf[] = new byte[32];
								int len = is.read();
								if (len>0)
								{
									byte b[] = new byte[len];
									int length = is.read(b);
									String echo = new String(b,0,length);
									if(echo.equals(MyConstants.DELETE))
									{
										JOptionPane.showMessageDialog(StuInfoSystem.this,echo);
										StuInfoSystem.this.tableModel.removeRow(row);											
									}
									else
									{
										JOptionPane.showMessageDialog(StuInfoSystem.this,"删除失败");
									}
																					
								}
								 				
							}
							
							s.close();						
						}
						catch(Exception ex)
						{
							JOptionPane.showMessageDialog(StuInfoSystem.this,ex.getMessage());						
						}					
				
					}
					
					else
					{
						return;
					}		
				}
			}
		}
		);
		
		toolbar.add(jbDel);
		
		toolbar.addSeparator(new Dimension(2,2));
		//退出按钮
		JButton jbExit = makeButton("exit","退出系统");
		toolbar.add(jbExit);
		jbExit.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				shutDown();				
			}
		}
		);
		
		toolbar.addSeparator();
		
		//关于按钮
		JButton jbAbout = makeButton("about","关于本系统");
		jbAbout.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				JOptionPane.showMessageDialog(StuInfoSystem.this,"基于Java C/S结构的学生管理系统V1.0 Beta Demo版\n系统作者:http://www.mnkjxy.com    福建·泉州·康美");
			}
		}
		);
		
		toolbar.add(jbAbout);
		
		toolbar.setSize(new Dimension(MyConstants.WIDTH,MyConstants.HEIGHT));
	}
  	  
  
    public static void main(String []args)
    {
    	StuInfoSystem sis = new StuInfoSystem();
    	sis.setVisible(true);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -