VNC.jpg 

簡介:點選Gui的Item..執行程式..後面帶連線的IP..GUI及連線的IP都額外寫在兩個參數檔執行.

--------------------------以下為Code----------------------------------------------------

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;

public class VNC extends JFrame implements ActionListener
{
 private JPanel topPanel=new JPanel();;
 private JMenuBar menuBar;
 private int frameSizeX;
 private int frameSizeY;
 private String filePath;
 private JMenu tmpMenu;
 private JMenu tmpSubMenu;
 private JMenuItem tmpMenuItem;
 private JMenu otherMenu;
 private JMenuItem aboutMenuItem;
 private JLabel tmpJLabel;
 private JTextField tmpJText;
 private JTextField jt1;
 private JTextField jt2;
 private JTextField jt3;
 private JTextField jt4;
 private JTextField jt5;
 private JTextField jt6;
 private String version;
 private String loadingDate[];
 private String frameTitle;
 private byte[] tmpByte;
 private char[] tmpChar;
 private Font tmpFont=new Font("新細明體",Font.PLAIN,12);
 SortedSet<String> menuSet=new TreeSet<String>();
 SortedSet<String> subMenuSet=new TreeSet<String>();
 SortedSet<String> menuItemSet=new TreeSet<String>();
 FileReader fr=null;
 BufferedReader bf=null;
 Properties pro = new Properties();

 public VNC()
 {  
  if(checkFileExist("config.ini")==false)
  {
   showDiolog("找不到 config.ini 系統檔");
   System.exit(0);
  }
  else
  {
   filePath=loadFromINI("config.ini","filePath");
   frameSizeX=Integer.parseInt(loadFromINI("config.ini","frameSizeX"));
   frameSizeY=Integer.parseInt(loadFromINI("config.ini","frameSizeY"));
   version=loadFromINI("config.ini","version");
   frameTitle=loadFromINI("config.ini","frameTitle");
   if(checkFileExist(filePath)==false)
   {
    showDiolog("找不到 config.ini 內filePath 的執行檔");
   }
  }
  
  setSize(frameSizeX,frameSizeY);
  
  setTitle("VNC連線程式");
  
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  topPanel.setLayout(new BorderLayout());
  //topPanel.setLayout(new FlowLayout());
  topPanel.setOpaque(false);
  
  //set JLabel condition
  //(JFrame jp,String tmpName,int tmpSizeX,int tmpSizeY,int tmpLocationX,int tmpLocationY,Color tmpColor)
  createJLabel(this,"VCR連線密碼:",100,15,10,10,Color.blue);
  createJLabel(this,"TFT:",50,15,10,35,Color.red);
  createJLabel(this,"CF:",50,15,110,35,Color.red);
  createJLabel(this,"LCD:",50,15,210,35,Color.red);
  createJLabel(this,"DPC連線密碼:",100,15,10,60,Color.blue);
  createJLabel(this,"AGV連線密碼:",100,15,103,60,Color.blue);
  createJLabel(this,"PMS連線密碼:",100,15,214,60,Color.blue);
  
  //set JText condition
  //(JFrame jp,String tmpName,int tmpSizeX,int tmpSizeY,int tmpLocationX,int tmpLocationY,Color tmpColor)
  jt1=createJText(this,"xxxxxx",60,15,35,35,Color.black);
  jt2=createJText(this,"xxxxxx",60,15,130,35,Color.black);
  jt3=createJText(this,"xxxxxx",60,15,240,35,Color.black);
  jt4=createJText(this,"xxxxxx",60,15,35,85,Color.black);
  jt5=createJText(this,"xxxxxx",60,15,130,85,Color.black);
  jt6=createJText(this,"xxxxxx",60,15,240,85,Color.black);
  //join JFrame
  getContentPane().add(topPanel);
  
  //Create the menu bar
  menuBar = new JMenuBar();
  
  //Set this instance as the application's menu bar
  setJMenuBar(menuBar);
  
  try
  {
   readFiles("FrameConfig.ini");
  }
  catch(IOException e)
  {
  }  
  
  //Create other menu
  otherMenu=CreateMenu(menuBar,"OTHER",'O');
  aboutMenuItem=CreateMenuItem(otherMenu,"About","關於");
  
  //set the location on the windosw
  setLocation(300,300);
  
 }
 public static void main (String[] args)
 {
  long AverageTime =0; // 計算平均處理時間使用
  long StartTime = System.currentTimeMillis(); // 取出目前時間

  VNC ex=new VNC();
  ex.setVisible(true);
  
  long ProcessTime = System.currentTimeMillis() - StartTime; // 計算處理時間
  AverageTime += ProcessTime; // 累積計算時間
  System.out.println("運行時間:"+AverageTime);
 }
 public void actionPerformed(ActionEvent e)
 {
  //System.out.println(e.getActionCommand());
  
  if(e.getActionCommand()!="About")
  {
   Connections(e.getActionCommand());
  }
  else
  {
   try
   {
    pro.load(new FileInputStream("config.ini"));
   }
   catch(IOException io)
   {
   }
   showDiolog("Version:  "+version+"\n\nBy Danny");
  }
 }
 ////Start to create Menu
 public JMenu CreateMenu(JMenuBar jb,String menuText,int acceleratorKey)
 {
  JMenu menu;
  //Create Menu
  menu=new JMenu(menuText);
  //Create this Menu's name
  menu.setName(menuText);
  //Create this Menu's quickly button
  if(acceleratorKey>0)
  {
   menu.setMnemonic(acceleratorKey);
  }
  
  jb.add(menu);
  
  return menu;
 }
 //End Menu
 ////Start to create SubMenu
 public JMenu CreateSubMenu(JMenu jm,String menuText,int acceleratorKey)
 {
  JMenu menu=null;
  //Create SubMenu
  menu=new JMenu(menuText);
  //Create this SubMenu's name
  menu.setName(menuText);
  //Create this Menu's quickly button
  if(acceleratorKey>0)
  {
   menu.setMnemonic(acceleratorKey);
  }
  
  jm.add(menu);
  
  return menu;
 }
 //End SubMenu
 //Start to create MenuItem
 public JMenuItem CreateMenuItem(JMenu jm,String sText,String sToolTip)
 {
  JMenuItem menuItem=null;
  //Create MenuItem
  menuItem=new JMenuItem(sText);
  //Create this MenuItem's name
  menuItem.setName(sText);
  if(sToolTip!=null)
   {
    menuItem.setToolTipText(sToolTip);
   }
  //Join MenuItem into the ActionListen
  menuItem.addActionListener(this);
  
  jm.add(menuItem);
  
  return menuItem;
 }
 //End MenuItem
 //Start Diolog
 public void showDiolog(String message)
 {
  if(message!=null)
  {
   JOptionPane.showMessageDialog(null,message);
  }
 }
 //End Diolog
 public void Connections(String addressName)
 {
  try
  {
   pro.load(new FileInputStream("ip.ini"));
  }
  catch(IOException io)
  {
  }
  String getIP=pro.getProperty(addressName);
  if(getIP!=null)
  {
   String CheckgetIP[]=getIP.split("[.]+");
   //System.out.println(CheckgetIP.length);
   if(CheckgetIP.length==4)
   {
    try
    {
     Runtime.getRuntime().exec(filePath+" "+getIP);
     //System.out.println(filePath+" "+getIP);
    }
    catch(IOException e)
    {
    }
   }
   else
   {
   showDiolog("ip.ini載入失敗\n1.請檢查"+addressName+"有沒有設定IP");
   }
  }
  else
  {
   showDiolog("ip.ini載入失敗\n1.請檢查IP設定名稱是否為"+addressName);
  }
 }
 public boolean checkFileExist(String path)
 {
  File f=new File(path);
  return f.exists();
 }
 public String loadFromINI(String iniFileName,String settingName)
 {
  String loadFile;
  try
  {
   pro.load(new FileInputStream(iniFileName));
  }
  catch(Exception e)
  {
  }
  loadFile=pro.getProperty(settingName);
  return loadFile;
 }
 public void createJLabel(JFrame jp,String tmpName,int tmpSizeX,int tmpSizeY,int tmpLocationX,int tmpLocationY,Color tmpColor)
 {
  
  tmpJLabel=new JLabel(tmpName);
  tmpJLabel.setSize(tmpSizeX,tmpSizeY);
  tmpJLabel.setLocation(tmpLocationX,tmpLocationY);
  tmpJLabel.setForeground(tmpColor);
  tmpJLabel.setFont(tmpFont);
  jp.add(tmpJLabel);
 }
 public JTextField createJText(JFrame jp,String tmpName,int tmpSizeX,int tmpSizeY,int tmpLocationX,int tmpLocationY,Color tmpColor)
 {
  tmpJText=new JTextField(tmpName);
  tmpJText.setSize(tmpSizeX,tmpSizeY);
  tmpJText.setLocation(tmpLocationX,tmpLocationY);
  tmpJText.setForeground(tmpColor);
  tmpJText.setEditable(false);
  tmpJText.addActionListener(this);
  jp.add(tmpJText);
  return tmpJText;
 }
 public void readFiles(String fileName) throws IOException
 {
  String tmp=null;
  String menuTmp1=null;
  String menuTmp2=null;
  loadingDate=null;
  int tmp1=0; //快速鍵暫存
  char acceleratorTmp[];
  
  try
  {
   fr=new FileReader(fileName);
   bf=new BufferedReader(fr);
   //建立迴圈讀取FrameConfig.ini檔案
   while((tmp=bf.readLine())!=null)
   {
    //將讀到的那行依照:拆掉丟到陣列內
    loadingDate=tmp.split("[:]");
    //開頭如果不是#開頭的就不繼續
    if(loadingDate[0].equals("#")==false)
    {
     continue;
    }
    //第一層Menu的快捷鍵不是空白的就轉成數字
    if(loadingDate[2].equals("null")==false)
    {
     acceleratorTmp=loadingDate[2].toCharArray();
     tmp1=acceleratorTmp[0];
    }
    //比對是不是有重覆新增過第一層Menu
    if((loadingDate[1].equals("null")==false)&&(menuSet.contains(loadingDate[1])==false))
    {
     menuSet.add(loadingDate[1]);
     tmpMenu=CreateMenu(menuBar,loadingDate[1],tmp1);
    }
    //第二層Menu的快捷鍵不是空白的就轉成數字
    if(loadingDate[4].equals("null")==false)
    {
     acceleratorTmp=loadingDate[4].toCharArray();
     tmp1=acceleratorTmp[0];
    }
    //比對是否要新增第二層Menu和有重覆新增過第二層Menu
    if((loadingDate[3].equals("null")==false)&&(subMenuSet.contains(loadingDate[3])==false))
    {
     subMenuSet.add(loadingDate[3]);
     tmpSubMenu=CreateSubMenu(tmpMenu,loadingDate[3],tmp1);
    }
    //第二層Menu的快捷鍵不是空白的就轉成數字
    if(loadingDate[6].equals("null")==false)
    {
     acceleratorTmp=loadingDate[6].toCharArray();
     tmp1=acceleratorTmp[0];
    }
    //寫入條件:1.第二層有設定 2.Menuitem有設定 3.Menuitem 沒重覆
    if((loadingDate[3].equals("null")==false)&&(menuItemSet.contains(loadingDate[5])==false)&&(loadingDate[5].equals("null")==false))
    {
     menuItemSet.add(loadingDate[5]);
     tmpMenuItem=CreateMenuItem(tmpSubMenu,loadingDate[5],loadingDate[6]);
    }
    else if(menuItemSet.contains(loadingDate[5])==false)
    {
     menuItemSet.add(loadingDate[5]);
     tmpMenuItem=CreateMenuItem(tmpMenu,loadingDate[5],loadingDate[6]);
    }
   }
   /**
    *%:#(要有這個符號才會讀取) -Array0
    *:(第一層Menu的名稱) -Array1
    *:(第一層Menu快捷鍵,沒有的話輸入小寫null) -Array2
    *:(有第二層Menu才要加入,沒有的話輸入小寫null) -Array3
    *:(第二層快捷鍵,沒有的話輸入小寫null) -Array4
    *:(MenuItem的名稱,有第二層會加在第二層下) -Array5
    *:(MenuItem的提示,沒有的話輸入Null) -Array6
   **/
  }
  catch(IOException e)
  {
   showDiolog("FrameConfig.ini 載入錯誤");
   System.exit(0);
  }
 }
}

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 蛋酒 的頭像
    蛋酒

    藍色代表憂鬱 那我代表什麼

    蛋酒 發表在 痞客邦 留言(0) 人氣()