?? usergui.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) {// 單擊鼠標(biāo)左鍵
// if (e.getClickCount() == 2) {
// int colummCount = table.getModel().getColumnCount();// 列數(shù)
// 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);//用戶關(guān)閉窗口時退出程序
JMenuBar menubaer=new JMenuBar();
this.setJMenuBar(menubaer);
//設(shè)置菜單
// 文件菜單
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);//快捷鍵設(shè)置
KeyStroke ms1 = KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK);//crl+a快捷鍵
openFile.setAccelerator(ms1);
saveFile.setAccelerator(ms2);
// 統(tǒng)計(jì)菜單
JMenu statM=new JMenu("Stat");
menubaer.add(statM);
statInfo=new JMenuItem("Stat Info");
statM.add(statInfo);
statInfo.addActionListener(this);
// 網(wǎng)絡(luò)菜單
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);
}
// 事件監(jiān)聽
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) {
//事件模型的設(shè)置
//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);
//調(diào)用了文件的解析方法,然后將文件轉(zhuǎn)換為中文,
tables = tablev.tableView(chineseship);
//表格的表頭
columnNames=pare.title();
table= new JTable(tables,columnNames);
table.updateUI();//當(dāng)?shù)诙未蜷_文件時實(shí)現(xiàn)文件的更新
table.clearSelection();
// table.valueChanged(e);void valueChanged(ListSelectionEvent e)
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS );
table.setRowHeight(30);//設(shè)置表格的高度
table.getColumnModel().getColumn(0).setPreferredWidth(150);//設(shè)置表格寬度
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, "無法打開系統(tǒng)畫圖工具",
"提示", JOptionPane.ERROR_MESSAGE);
}
}
if(e.getSource().equals(computer)){
try {
Runtime.getRuntime().exec("calc");
} catch (Exception ev) {
JOptionPane.showMessageDialog(null, "無法打開系統(tǒng)畫圖工具",
"提示", JOptionPane.ERROR_MESSAGE);
}
}
if(e.getSource().equals(version)){
JOptionPane.showMessageDialog(null, "海運(yùn)系統(tǒng):V 1.1\n程序員Mrs zy");
}
if(e.getSource().equals(game)){
try {
Runtime.getRuntime().exec("D:\\生化危機(jī)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);
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -