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

📄 usergui.java

📁 一个java开发的海运系统
💻 JAVA
字号:
package sun.com.gui;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.print.PrinterException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Vector;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;

import com.sun.corba.se.impl.util.Version;

import sun.com.domain.Ship;
import sun.com.service.impl.ConnecServer;
import sun.com.service.impl.Myserver;
import sun.com.stat.Stat;
import sun.com.tool.NewFile;
import sun.com.tool.ParesFiles;
import sun.com.tool.TableVcter;
import sun.com.tool.ToChinese;
/***
 * @since 09-02-16
 * @category用户界面的桌面程序
 * @author zhao
 *
 */
public class UserGui extends JFrame implements ActionListener{
	JMenuItem statInfo;
	
	JMenuItem staServer;
	JMenuItem Connecting;
	JMenuItem newFile;
	JMenuItem openFile;
	JMenuItem saveFile;
	JMenuItem next;
	JMenuItem game;
	JMenuItem computer;
	JMenuItem drow;
	JMenuItem version;
	String []columnNames=null;//菜单表头
	String tables[][]=null;
	JTable table=null;
	Vector<Ship> parefile=null;//解析出来的船
	File file1=null;//选择的文件
	JMenuItem jprint;
	//********************************
//	public void aa(){
//	    table.addMouseListener(new MouseAdapter() { 
//			public void mouseClicked(MouseEvent e) { 
//			if (e.getButton() == MouseEvent.BUTTON1) {// 单击鼠标左键 
//			if (e.getClickCount() == 2) { 
//			int colummCount = table.getModel().getColumnCount();// 列数 
//			for (int i = 0; i < colummCount; i++) 
//			System.out.print(table.getModel().getValueAt(table.getSelectedRow(), i).toString()+ " "); 
//			System.out.println(); 
//			} 
//			} 
//			}
//		});
//	}
	//********************************
	public UserGui(){
		Container con=this.getContentPane();
		this.setBounds(400, 400, 500, 500);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);//用户关闭窗口时退出程序
		JMenuBar menubaer=new JMenuBar();
		this.setJMenuBar(menubaer);
		//设置菜单
//		文件菜单
		JMenu fileM=new JMenu("File");
		
		
		
		menubaer.add(fileM);
	    newFile=new JMenuItem("new file");
		openFile=new JMenuItem("open file");
		saveFile=new JMenuItem("save file");
		next=new JMenuItem("Next");
        //菜单事件的添加
		newFile.addActionListener(this );
		openFile.addActionListener(this);
		saveFile.addActionListener(this);
		fileM.add(newFile);
		fileM.add(openFile);
		fileM.add(saveFile);
		fileM.add(next);
		KeyStroke ms2 = KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK);//快捷键设置
		KeyStroke ms1 = KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK);//crl+a快捷键
		openFile.setAccelerator(ms1);
		saveFile.setAccelerator(ms2);
		
//		统计菜单
		JMenu statM=new JMenu("Stat");
		menubaer.add(statM);
		statInfo=new JMenuItem("Stat Info");
		statM.add(statInfo);
		statInfo.addActionListener(this);
		
//		网络菜单
		JMenu netM=new JMenu("NetWork");
		menubaer.add(netM);
		staServer=new JMenuItem("Start SERVER");
		Connecting=new JMenuItem("Connecting SERVER ");
		netM.add(staServer);
		netM.add(Connecting);
		staServer.addActionListener(this);
		Connecting.addActionListener(this);
		
//		娱乐菜单
		JMenu recreationM=new JMenu("Recreation");
		menubaer.add(recreationM);
		game=new JMenuItem("GAME");
		recreationM.add(game);
		game.addActionListener(this);
		
//      工具菜单
		JMenu toolM=new JMenu("Tool");
		menubaer.add(toolM);
		computer=new JMenuItem("Computer");
		drow=new JMenuItem("Drow");
		jprint=new JMenuItem("print");
		
		toolM.add(computer);
		toolM.add(drow);
		toolM.add(jprint);
		jprint.addActionListener(this);
		computer.addActionListener(this);
		drow.addActionListener(this);
		
		
//帮助菜单
		JMenu v=new JMenu("Others");
	    version=new JMenuItem("Version");
		menubaer.add(v);
		v.add(version);
		version.addActionListener(this);
	}
// 事件监听
	public void actionPerformed(ActionEvent e) {
		    Vector<Vector<String>> rowData = null;
	        ParesFiles pare=null;
	        ToChinese chinese=null;
	        TableVcter tablev=null;
	        JFileChooser jfc=new JFileChooser("d:/");
	        
		
		if(e.getSource().equals(newFile)){
			JOptionPane.showMessageDialog(new NewFile(), "Join");
		}
		       
		
		if(e.getSource().equals(openFile)){
			   
			    int value = jfc.showOpenDialog(this);
			     file1=jfc.getSelectedFile();//ѡ���ļ�
			if (value == JFileChooser.APPROVE_OPTION) {
				//事件模型的设置
                //DefaultTableModel defaultModel = new DefaultTableModel(rowData,columnNames);//
				 pare=new ParesFiles();
				 chinese=new ToChinese();
				 tablev=new TableVcter();
				 
				try{
					 parefile=pare.pareFile(file1);
				 
				}catch(Exception i){
					JOptionPane.showMessageDialog(null, "文件格式不正确!","提示", JOptionPane.ERROR_MESSAGE);
					return;	
				}
				List<Ship> chineseship=chinese.transitionFile(parefile);
				
			    //调用了文件的解析方法,然后将文件转换为中文,
			    tables  = tablev.tableView(chineseship);
			    //表格的表头
				columnNames=pare.title();
				table= new JTable(tables,columnNames);
				
				table.updateUI();//当第二次打开文件时实现文件的更新
				table.clearSelection();
//				table.valueChanged(e);void valueChanged(ListSelectionEvent e) 
				table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS );
				table.setRowHeight(30);//设置表格的高度
				table.getColumnModel().getColumn(0).setPreferredWidth(150);//设置表格宽度
				JScrollPane scrollpane = new JScrollPane(table);
			    this.add(scrollpane);
			    this.setVisible(true);
			    
			    
		   }
	    }
		if(e.getSource().equals(saveFile)){
			    //没有文件保存时 弹出错误对话框
			    if(tables==null){
			    	JOptionPane.showMessageDialog(null,"NO FILE SAVE","Error",JOptionPane.ERROR_MESSAGE);
				   return;
			    }
			       int value=jfc.showSaveDialog(this);//显示窗口
			    if(value==JFileChooser.APPROVE_OPTION){
				   try {
					         
				            FileWriter ps=new FileWriter(jfc.getSelectedFile());       
					for(int i= 0;i<table.getRowCount();i++){
						for(int n=0;n<table.getColumnCount();n++){
							ps.write((String)table.getValueAt(i, n)+"\t\n");
							ps.flush();
						}
				   }
					ps.close();
				} catch (IOException e1) {		
					e1.printStackTrace();
				}
			
			  }
			} 
		if(e.getSource().equals(statInfo)){
			if(table==null){
			 JOptionPane.showMessageDialog(this, "No File to Stat", "Error", JOptionPane.ERROR_MESSAGE);
			 return;
			}else{
				Stat s=new Stat(tables,file1);
				
				
				
			}
		}
		
		
		
			
		if(e.getSource().equals(drow)){
			try {
				Runtime.getRuntime().exec("mspaint");
			} catch (Exception ev) {
				JOptionPane.showMessageDialog(null, "无法打开系统画图工具",
						"提示", JOptionPane.ERROR_MESSAGE);
			}
		}
		if(e.getSource().equals(computer)){
	     try {
				Runtime.getRuntime().exec("calc");
			} catch (Exception ev) {
				JOptionPane.showMessageDialog(null, "无法打开系统画图工具",
						"提示", JOptionPane.ERROR_MESSAGE);
			}
		}
		if(e.getSource().equals(version)){
			
	        JOptionPane.showMessageDialog(null, "海运系统:V 1.1\n程序员Mrs zy");
	       
		}
		if(e.getSource().equals(game)){
			
			try {				
				
				Runtime.getRuntime().exec("D:\\生化危机3\\bio3_pc.exe");
				
			} catch (IOException e1) {
				JOptionPane.showMessageDialog(null, "GAME NO FIND");
			}
			
		}
		if(e.getSource().equals(staServer)){
			System.out.println("start server now");
			Serv s=new Serv();
			s.start();
			
		}
		
		if(e.getSource().equals(Connecting)){
			Connet con=new Connet();
			con.start();
		}
		if(e.getSource().equals(jprint)){
			Graphics c=this.getGraphics();
			table.getGraphics().draw3DRect(300, 300, 30, 40, true);
//			=jprint.getComponent().getGraphics();
//			JTable v=new JTable();
//			print(c);
			
			try {
				table.print();
			} catch (PrinterException e1) {
				
				e1.printStackTrace();
			}
			
			
		}
	
		
	}
	
class Serv extends Thread{
	public void run(){
		Myserver v=new Myserver();
		v.setVisible(true);
		
	}
}
class Connet extends Thread{
	public void run(){
		
		ConnecServer s=new ConnecServer();
		s.setVisible(true);
		
	}
}

	
	
}

⌨️ 快捷键说明

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