(十三)Core Java GUI(圖形化界面小程序開發,事件監聽機制流程,以及可運行jar包的配置實現,) (109)


 目錄 :          
1 ) . GUI(概述)

2 ) .  GUI(布局)

3 ) .   GUI(Frame)

4 ) .  GUI(事件監聽機制)

5 ) .  GUI(窗體事件)

6 ) .  GUI(Action事件)

7 ) .   GUI(鼠標事件)

8 ) .  GUI(鍵盤事件)

9 ) .  GUI(練習--列出指定目錄內容)

10 ) .  GUI(對話框Dialog)

11 ) .   GUI(菜單)

12 ) .  GUI(練習--打開文件)

13 ) .  GUI(練習--保存文件)

14 ) .  GUI(jar包雙擊執行)
 


     一 .  GUI(概述)

1 ) . 簡述 : 對比記憶 --> GUI (圖形化界面) 與CLI (命令行窗口)

1.1 GUI :  Graphical User Interface(圖形用戶接口)  --> 用圖形的方式顯示計算機操作的界面,方便直觀

1.2 CLI :  Command line User Interface (命令行用戶接口) --> 指常見的Dos命令行操作, 需要記憶一些常用的命令,操作不直觀

1.3 舉例 : 創建文件,刪除文件等

 
2 ) . GUI工具包 : 

2.1 描述    :  java為GUI提供的對象都存在java.Awt和javax.Swing兩個包中

2.2 功能分類 :

[1]java.Awt  : Abstract Window ToolKit (抽象窗口工具包) ,需要調用本地系統方法實現功能,屬於重量級空間 --> 可移植性差

[2] javax.Swing  : 在AWT基礎之上,建立的一套圖形化界面系統,組件更多更完善,並完全由java實現,屬輕量級控件  --> 可移植性好
 

3 ) .  繼承關系圖 :


        Container : 為容器, 是一個特殊的組件,該組件中可通過add方法來添加其他組件進行融合

 

 
小結 :  

             1.  GUI是圖形化界面編程
        
           2.  計算機與用戶交互的兩種方式 : 1.DOS窗口命令行 2.圖形化界面

           3. Lable標簽對象的作用是封裝文字,將文字通過對象的方式與其它組件相結合

           4. java的圖形化界面開發包有三個 : java.Awt    ,  javax.Swing  , java.Swt  
 

       二.  GUI(布局)


1 ) . 布局簡述 : 

1.1 容器中組件的排列方式就是布局

1.2 常見的布局管理器(對象)

[1] FlowLayout(流式布局管理器)  : 從做到右順序排列 , 是Panel默認的布局管理器

[2] BorderLayout(邊界布局管理器) : 東南西北中 , 是Frame默認的布局管理器

[3] GridLayout(網格布局管理器) : 規則的矩陣 ,計算器常用的布局管理器

[4]GradLayout(卡片布局管理器) : 選項卡

[5] GridBagLayout(網格包布局管理器) : 非規則的矩陣

2 ) . 布局特點 : 

2.1 流式布局的特點是 : 默認居中,而后左邊,然后后邊,最后下邊

2.2 邊界布局的特點是 : 上北下南左西又東中 , 若不指定方向則填充全頁面

2.3 網格布局的特點是 : 水平和垂直形成的特定大小平均的格子

2.4 網格包布局的特點 : 水平和垂直形成的不是特定大小的格子

2.5 卡片布局的特點是 : 多個模塊融合成一個模塊,一個模塊點擊可選項進入多個模塊 

2.6 坐標布局的特點是 : 依據橫縱坐標形成的布局特點,意味着區塊想放哪放哪

 

小結 :  

             1.  何為布局?  布局就是組件的排列方式
        
           2.  圖形化界面開發工具 : Hbuilder, 微軟的vb 

           3.  面板用於將多個布局應用同一個模塊內

       

      三.  GUI(Frame)

1 ) . Frame 使用窗體搭建一個簡單的圖形化按鈕界面

2 ) . Demo:
/*
 
 
制作圖形化界面思路 :
[1] 創建frame窗體
[2] 對窗體進行基本設置 :  例子:大小,位置,布局
[3] 定義組件
[4] 將組件通過窗體的add方法添加到窗體中
 
[5] 讓窗體顯示,通過setVisible(true)
       
 */
 
 
 
 
  import java.awt.*;
  class GuiDemo
  {    
              public static void sop(Object obj)
              {
                     System.out.println(obj);
              } 
 
      
               //主方法
                public static void main(String args[])
                           {
                                        Frame fra =new Frame("my awt");  //初始化一個不可見的窗體 命名為 my awt,該窗體內可裝飾其它組件
                                        
                                        fra.setSize(500,400);  // 設置窗體大小 ,參數是先水平再垂直
                                        
                                        fra.setLocation(600,400);  //設置窗體位置, 參數是 左上右下
                                        
                                        fra.setLayout(new FlowLayout());  //設置布局為 流式布局
                                        
                                        Button but =new Button("俺是按鈕");  //初始化按鈕
                                        
                                        fra.add(but);  //按鈕添加入窗體
                                        
                                        fra.setVisible(true);   //讓不可見的窗體可見
                                        
                                        
                    
               }
             
      
  }    
 
            
 

小結 :  

             1.  每開啟一個圖形化界面便會多開啟一個線程
        
      
 

      四. GUI(事件監聽機制)

1 ) . 圖示密碼鎖流程 : 


2 ) . Demo:

/*
 
 
 
 本章講述 :
 
 1. 事件監聽機制的運行流程
 
 2. 事件監聽機制的組成 :
 
 [1]事件源
 [2]事件
 [3]監聽器
 [4]事件處理
 
 解析:
 
 事件源-->指awt包或者swing包中的那些圖形化界面組件
 事件-->指每一個事件源都有自己特有的對應事件和共性事件
 監聽器--> 指可以觸發某個事件的動作(不止是一個動作)將其封裝進到監聽器中
 
 以上三者,在java中都已定義好,直接調取其對象使用即可
 
 我們需要做的就是,對產生的動作進行處理即可
 
  
            

 
 
小結 :  

             1.   事件源因某種原因產生事件對象, 監聽器監聽到事件對象並作出相應的處理方式,
        
 

     五 .  GUI(窗體事件)

1 ) . 事件監聽機制包中的監聽機制的使用

2 ) . Demo:

/*
 
 
 
 本章講述 :
 
 
1.  事件監聽包: import java.awt.event.*;
2.  事件監聽器的三種獲取方式 :
       [1] 實現接口  implements WindowsListener 
       [2] 繼承抽象類 extends WindowAdapter     -->該方法中實現了WindowsListener所有方法,想要哪個覆蓋哪個即可
       [3]  WindowAdapter抽象類的匿名內部類
3.匿名內部類的方式:
       通過匿名內部類可通過不實例化對象便使用對象中的方法
 
       
 */
 
 
 
 
  import java.awt.*;
  import java.awt.event.*;
 
  //獲取監聽器方式一
/*   class myListener implements WindowsListener
  {
         //只是需要實現接口中七個方法,但我們只需要一個,因此不建議使用
        
  }
   */
  
  //獲取監聽器方式二
  class myListener extends WindowAdapter
  {
         public void windowClosing(WindowEvent e)   //關閉的方法
         {
              
               System.out.println("關閉");
              
               System.exit(0);  //關閉系統進程
         }
        
        
  }
 
 
  class GuiDemo
  {    
              public static void sop(Object obj)
              {
                     System.out.println(obj);
              } 
 
      
               //主方法
                public static void main(String args[])
                           {
                                        Frame fra =new Frame("my awt");  //初始化一個不可見的窗體 命名為 my awt,該窗體內可裝飾其它組件
                                        
                                        fra.setSize(500,400);  // 設置窗體大小 ,參數是先水平再垂直
                                        
                                        fra.setLocation(600,400);  //設置窗體位置, 參數是 左上右下
                                        
                                        fra.setLayout(new FlowLayout());  //設置布局為 流式布局
                                        
                                        Button but =new Button("俺是按鈕");  //初始化按鈕
                                        
                                        fra.add(but);  //按鈕添加入窗體
                                        
                                        fra.setVisible(true);   //讓不可見的窗體可見
                                        
                                 //     fra.addWindowListener(new myListener());   //給fram窗體添加監聽機制 獲取監聽器的方式二
                                 
                                 
                                  fra.addWindowListener(new WindowAdapter()   //通過匿名內部類的方式實現監聽機制
                                  {
                                        
                                           public void windowClosing(WindowEvent e)   //關閉的方法
                                               {
                                                      System.out.println("匿名內部類的方式關閉");
                                                            System.exit(0);
                                                            
                                               
                                               }
                                        
                                        
                                  });   //給fram窗體添加監聽機制 獲取監聽器的方式三  -->
                                 
                                 
                                 
                                        
                                        
                                        
                    
               }
             
      
  }    
 
            

3 ) .  關於如何尋找到自己想要的方法?

3.1 本身類中沒有便尋找父類,父類若是接口,則再查看下父類中的實現類中是否有

 
小結 :  

             1.  一個事件對應多個動作
        
           2.  實現接口必須實現接口中所有方法,而繼承抽象類可選擇性的選擇需要的方法

           3. 事件監聽器可以理解為一個類, 一個事件監聽器監聽很多的動作 , 每個動作可理解為 一個類中的多個方法

           4. 具體監聽哪個事件源就去哪個事件源中尋找事件監聽機制的方法
 

       六.  GUI(Action事件)


1 ) . Action事件 --> 是按鈕的監聽事件

2 ) . Demo:

/*
 
 
 
 本章講述 :
 
 
 [1] 獲取窗體監聽尋找窗體內的監聽方法,獲取按鈕監聽尋找按鈕內的監聽方法
 
 
 [2]button 中addActionListener()方法 可讓 button 擁有監聽機制
 
 
 */
 
 
 
 
  import java.awt.*;
  import java.awt.event.*;
   class FrameDemo
  {    
 
       private Frame fra;
       private Button but;
       
       
       FrameDemo()   //構造方法
       {
             init();
             
       }
       
       
       //初始化方法
       public void init()
       {
             
             fra=new Frame();
             //對窗體進行基本設置
             fra.setBounds(300,100,600,500);  //參數 : 坐標(橫坐標,縱坐標) , 大小 (水平,垂直)
             fra.setLayout(new FlowLayout()); //設置布局為流式布局
             
             but=new Button("my button");  //初始化按鈕
             
             fra.add(but);  //將按鈕組件添加入窗體
             
             myEvent();// 加載事件監聽
             
             fra.setVisible(true);  //顯示窗體
             
       
       }
       
       
       //相關事件監聽機制
       public void myEvent()
       {
             
             //讓窗體具備關閉程序的功能
             fra.addWindowListener(new WindowAdapter()  //匿名內部類使用窗體監聽事件中的關閉動作
             {
                    public void windowClosing(WindowEvent e)
                    {
                           System.exit(0);
                           
                    }
                    
             });
             
             
             //讓按鈕具備關閉程序的功能
             but.addActionListener(new ActionListener()//匿名內部類使用按鈕監聽事件中的關閉動作
             {
                    public void actionPerformed(ActionEvent e)
                    {
                           System.out.println("使用按鈕關閉程序");
                           System.exit(0);
                           
                    }
                    
             });
             
             
       }
 
               //主方法
                public static void main(String args[])
                           {
                                        
                                 FrameDemo fra =  new FrameDemo();
                    
                }
                           
                           
             
      
   
     

 

小結 :  

             1.  外部建立引用.,內部實例化的好處是范圍 作用於整個類
        
           2.  在GUI中,適配器的出現就是為了方便建立監聽對象

             3. 獲取窗體監聽尋找窗體內的監聽方法,獲取按鈕監聽尋找按鈕內的監聽方法
 


      七.  GUI(鼠標事件)

1 ) . Demo : 

/*
 
 
 
 本章講述 :
 
 [1] 鼠標事件:addMouseListener
 
 [2]  監聽器-->具體監聽事件-->監聽適配器實現類--> 適配器內封裝了很多細節的方法 : 比如鼠標單擊次數等
 
 
 */
 
 
 
 
  import java.awt.*;
  import java.awt.event.*;
   class MouseAndKeyDemo
  {    
 
       private Frame fra;
       private Button but;
       
       
       MouseAndKeyDemo()   //構造方法
       {
             init();
             
       }
       
       
       //初始化方法
       public void init()
       {
             
             fra=new Frame();
             //對窗體進行基本設置
             fra.setBounds(300,100,600,500);  //參數 : 坐標(橫坐標,縱坐標) , 大小 (水平,垂直)
             fra.setLayout(new FlowLayout()); //設置布局為流式布局
             
             but=new Button("my button");  //初始化按鈕
             
             fra.add(but);  //將按鈕組件添加入窗體
             
             myEvent();// 加載事件監聽
             
             fra.setVisible(true);  //顯示窗體
             
       
       }
       
       
       //相關事件監聽機制
       public void myEvent()
       {
             
             //讓窗體具備關閉程序的功能
             fra.addWindowListener(new WindowAdapter()  //匿名內部類使用窗體監聽事件中的關閉動作
             {
                    public void windowClosing(WindowEvent e)
                    {
                           System.exit(0);
                           
                    }
                    
             });
             
             
             but.addActionListener(new ActionListener()
             {
                    
                    public void actionPerformed(ActionEvent e) //按鈕活動時便觸發
                    {
                           
                           System.out.println("按鈕活動時觸發");
                           
                    //     System.exit(0);
                           
                    }
                    
                    
             });
 
             
             
             //讓按鈕產生鼠標事件
             but.addMouseListener(new MouseAdapter()
             {
                    
                    public void mouseClicked(MouseEvent e)   //鼠標單擊按鈕事件
                    {
                           
                           if((e.getClickCount()) ==2)
                                 System.out.println("鼠標雙擊按鈕時觸發");
                    }      
                    public void mouseEntered(MouseEvent e)  //鼠標進入按鈕單擊事件
                    {
                           
                                 System.out.println("鼠標進入按鈕時觸發");
                    }
 
                    
             });
 
       }
 
               //主方法
                public static void main(String args[])
                           {
                                        
                                   new MouseAndKeyDemo();
                    
                }
                           
                           
             
      
   
     

2 ) . 鼠標活動與鼠標點擊的區別?

2.1 鼠標活動內包含:   鼠標點擊  ,鍵盤Enter  

2.2 鼠標點擊 : 它只是鼠標點擊   

 

小結 :  

             1.  監聽機制 --->監聽器-->監聽的具體事件-->適配器實現類
        
      
 

     八 GUI(鍵盤事件)

1 ) . 鍵盤事件 : 我們可通過鍵盤事件操作鍵盤上的所有數值

2 ) . Demo:

/*
 
 
 
 本章講述 :
 
 鍵盤事件 : addKeyListener 
 
 
 [1] 單個鍵的值在KeyEvent鍵盤事件中查找
 
 [2] 組合鍵的使用方式在KeyEvent的父類中InputEvent查找
 
 [3] 我們可通過鍵盤限定,來指定可輸入的值
 
 
 思路:
 
 [1] 確定事件源
 
 [2] 確定事件
 
 [3] 確定監聽器
 
 [4] 確定適配器
 
 
 小結 : 事件可添加在任何的存在的組件上,可靈活使用 
 */
 
 
 
 
  import java.awt.*;
  import java.awt.event.*;
   class MouseAndKeyDemo
  {    
 
       private Frame fra;
       
       private Button but;
       
       private TextField tf ;
       
       
       MouseAndKeyDemo()   //構造方法
       {
             init();
             
       }
       
       
       //初始化方法
       public void init()
       {
             
             fra=new Frame();
             //對窗體進行基本設置
             fra.setBounds(300,100,600,500);  //參數 : 坐標(橫坐標,縱坐標) , 大小 (水平,垂直)
             fra.setLayout(new FlowLayout()); //設置布局為流式布局
             
             but=new Button("my button");  //初始化按鈕
             
             tf=new TextField(20);  //初始化文本框
             
             fra.add(but);  //將按鈕組件添加入窗體
             
             fra.add(tf);  //將文本框組件添加入窗體
             
             myEvent();// 加載事件監聽
             
             fra.setVisible(true);  //顯示窗體
             
       
       }
       
       
       //相關事件監聽機制
       public void myEvent()
       {
             
             //讓窗體具備關閉程序的功能
             fra.addWindowListener(new WindowAdapter()  //匿名內部類使用窗體監聽事件中的關閉動作
             {
                    public void windowClosing(WindowEvent e)
                    {
                           System.exit(0);
                           
                    }
                    
             });
             
             //為文本框添加鍵盤事件
             tf.addKeyListener(new KeyAdapter()
             {
                    
                    public void keyPressed(KeyEvent e)   //按下鍵盤時調用
                    {
                           
                           int code =e.getKeyCode();  //獲取鍵入的值
                           
                           int start = KeyEvent.VK_0 ;  //獲取數字0鍵
                           
                           int end = KeyEvent.VK_9 ;   //獲取數字9鍵
                           
                           if(!(code>=start && code<=end)) //判斷鍵入的值若不是數字則啟動下邊程序
                           {
                                 System.out.println("key codr 非法"+code);
                                 
                                 e.consume();  //消耗此事件,意味着 若不是 0-9的數則 不寫入
                                 
                           }
                           
                    }
                    
                    
             });
             
             //為按鈕添加鍵盤事件
             but.addKeyListener(new KeyAdapter()  //鍵盤事件
           {
                                         
                    public void keyPressed(KeyEvent e)   //按下鍵盤時調用
                    {
                           //玩單個鍵
                           if(e.getKeyCode() == KeyEvent.VK_ESCAPE)  //當鼠標點擊的值與KeyEvent中定義的靜態常量值 ESC鍵盤值相吻合時啟動下邊程序
                                 System.exit(0);  //關閉程序
                           
                           //玩組合鍵
                           if(e.isControlDown()&&e.getKeyCode() == KeyEvent.VK_ENTER)  //當鼠標點擊的值同時有ctrl 與 enter時 啟動下邊程序
                           System.out.println("ctrl enter is run...");
                    
                           
                           //System.out.println(e.getKeyModifiersText(e.getKeyCode()) +":"+e.getKeyCode());  //1.通過鍵的整數code獲取unicode字符,2.獲取鍵關聯的整數code
                           
                    }
                    
                                         
             });
                    
             
       
             
             
       }
 
               //主方法
                public static void main(String args[])
                           {
                                        
                                 new  MouseAndKeyDemo();
                }
                           
                           
             
      
   
     

 
  
          

     九 .   GUI(練習--列出指定目錄內容)  

1 ) . 列出指定目錄內容 :  路徑 --> 目錄

2 ) . Demo:

/*
 
 
 
 本章講述 :
 
 需求:  如何獲取電腦上的任意目錄名稱 ,並通過GUI的方式,按鈕點擊路徑獲取文件名,而后放入文本域中
 
 
 使用的方法:
 
 [1] getText();  獲取文本框數據
 [2] setText(); 設置文本框數據
 [3]  append(); 迭代添加數據
 
 */
 
 
 
 
  import java.awt.*;
  import java.awt.event.*;
  import java.io.*;
   class MyWindowDemo
  {    
 
        private Frame f;
        private TextField tf;
        private Button but;
        private TextArea ta;
       
        MyWindowDemo()
        {
              init();
             
        }
       
        public void init()
        {
              f= new Frame("My  Window");  //初始化框架
             
              f.setBounds(300,100,600,500);  //設置坐標及大小 : 前兩個坐標,后兩個大小
             
              f.setLayout(new FlowLayout()); //設置布局方式:流式布局
             
              tf= new TextField(60);   //初始化一個文本框,長度為 60
             
              but =new Button("轉到");   //初始化一個按鈕,名字為轉到
             
              ta =new TextArea(25,60);  //初始化一個文本域,水平25,垂直60
             
              f.add(tf);
              f.add(but);
              f.add(ta);
             
              //加載事件
              myEvent();
             
              f.setVisible(true);   //使框架(窗體)顯示
             
        }
       
        private void myEvent()
        {
              //按鈕點擊事件
              but.addActionListener(new ActionListener()
              {
                    public void  actionPerformed(ActionEvent e)
                    {
                           String dirPath = tf.getText();   //獲取文本框數據
                           
                           File dir =new File(dirPath);     //將文本框中的路徑放入文件內
                           
                           if(dir.exists() && dir.isDirectory())   //判斷該路徑是否存在,是否是目錄
                           {
                                 ta.setText("");   //清空文本域中的內容
                                 
                                 String[] listName =dir.list();  //獲取目錄列表名稱
                                 
                           //     ta.setText(str+"\r\n");  //  向文本域中設置文本
                           
                                 for(String str : listName)   //迭代目錄名稱
                                 {
                                        ta.append(str+"\r\n");  //向文本域中添加目錄名稱
                                        
                                 }
                                 
                           }
 
                    }
                    
              });
             
              //窗體的關閉事件
              f.addWindowListener(new WindowAdapter()
              {
                     public void windowClosing(WindowEvent e)
                     {
                            System.exit(0);
                           
                     }
                    
              });
             
             
             
        }
       
       
 
               //主方法
                public static void main(String args[])
                           {
                                        
                                 new  MyWindowDemo();
                }
                           
                           
             
      
   
     
     

  
 

       十.  GUI(對話框Dialog)


1 ) . Demo:

/*
 
 
 
 本章講述 :
 
 需求:  如何獲取電腦上的任意目錄名稱 ,並通過GUI的方式,按鈕點擊路徑獲取文件名,而后放入文本域中
 
 小細節實現 :
 
 [1] 路徑錯誤時彈出對話框
 
 [2] 輸入路徑后回車便可進行查詢
 
 
 
 
 
 */
 
 
 
 
  import java.awt.*;
  import java.awt.event.*;
  import java.io.*;
   class MyWindowDemo
  {    
 
        private Frame f;
        private TextField tf;
        private Button but;
        private TextArea ta;
        private Dialog d;
        private Label lab;
        private Button okBut;
       
        MyWindowDemo()
        {
              init();
             
        }
       
        //提示對話框
        public void dialog()
        {
             
             d= new Dialog(f,"提示信息",true);  // 參數是  1,關聯組件 2,提示內容 3,模式
             
             lab=new Label();   //初始化標簽
             
             d.setBounds(600,200,240,150);    //設置對話框基本信息
             
             d.setLayout(new FlowLayout());   //設置對話框布局方式
             
             okBut = new Button("確定");  //初始化按鈕
             
             String info="您輸入的路徑信息"+tf.getText()+"不正確,請重新輸入";
             
             lab.setText(info);  //將信息設置進標簽對象
             dialogEvent();   //加載事件
             
             d.add(lab);   //將標簽添加進對話框組件
             
             d.add(okBut);  //將按鈕添加進對話框組件
             
             
             
        }
       
        //初始化
        public void init()
        {
              f= new Frame("My  Window");  //初始化框架
             
              f.setBounds(300,100,600,500);  //設置坐標及大小 : 前兩個坐標,后兩個大小
             
              f.setLayout(new FlowLayout()); //設置布局方式:流式布局
             
              tf= new TextField(60);   //初始化一個文本框,長度為 60
             
              but =new Button("轉到");   //初始化一個按鈕,名字為轉到
             
              ta =new TextArea(25,60);  //初始化一個文本域,水平25,垂直60
             
             
             
              f.add(tf);
              f.add(but);
              f.add(ta);
             
              //加載事件
              FrameEvent();
             
              f.setVisible(true);   //使框架(窗體)顯示
             
        }
       
        //對話框上的事件
        private void dialogEvent()
        {
              //對話框事件
               d.addWindowListener(new WindowAdapter()
              {
                     public void windowClosing(WindowEvent e)
                     {
                           
                            d.setVisible(false);
                     }
                    
              });
             
              //對話框上的按鈕事件
               okBut.addActionListener(new ActionListener()
              {
                     public void actionPerformed(ActionEvent e)
                     {
                           
                            d.setVisible(false);
                     }
                    
              });
        }
       
        //將查看目錄文件的方法抽取出來
        private void showDir()
        {
             String dirPath = tf.getText();   //獲取文本框數據
                    
                    File dir =new File(dirPath);     //將文本框中的路徑放入文件內
                    
                    if(dir.exists() && dir.isDirectory())   //判斷該路徑是否存在,是否是目錄
                    {
                           ta.setText("");   //清空文本域中的內容
                           
                           String[] listName =dir.list();  //獲取目錄列表名稱
                           
                    //     ta.setText(str+"\r\n");  //  向文本域中設置文本
                    
                           for(String str : listName)   //迭代目錄名稱
                           {
                                 ta.append(str+"\r\n");  //向文本域中添加目錄名稱
                                 
                           }
                           
                    }
                    else
                    {
                           
                           
                           dialog();
                           d.setVisible(true);
                           
                           
                    }
             
        }     
       
        //窗體事件
        private void FrameEvent()
        {
 
             
              //對文本框啟動回車事件
              tf.addKeyListener(new KeyAdapter()
              {
                    
                     public void  keyPressed(KeyEvent e)
                    {
                           if(e.getKeyCode()==KeyEvent.VK_ENTER)  //判斷鍵入值是否與回車值相同
                                 showDir();
                    }      
 
              });
       
             
              //按鈕點擊事件
              but.addActionListener(new ActionListener()
              {
                    public void  actionPerformed(ActionEvent e)
                    {
                           showDir();
 
                    }
                    
              });
             
              //窗體的關閉事件
              f.addWindowListener(new WindowAdapter()
              {
                     public void windowClosing(WindowEvent e)
                     {
                            System.exit(0);
                           
                     }
                    
              });
             
             
             
        }
       
       
 
               //主方法
                public static void main(String args[])
                           {
                                        
                                 new  MyWindowDemo();
                }
                           
                           
             
      
   
     

 
 

      十一. GUI(菜單)

1 ) . Demo:

/*
 
 
 
 本章講述 :
 
 [1]菜單欄的基本實現
 
 [2]菜單欄的基本原理
 
 需求:   實現菜單欄
 關於二級窗口的原題 :
 
       其實就是兩個平級的菜單欄互相包含了,例如 :  A菜單欄里有子條目 , B菜單欄里有子條目, A菜單加入的B菜單內,則 B菜單內多了個菜單欄,並且多了個下一級的子條目
       
       總結 : 菜單內添加菜單就形成了子菜單
       
       
 
 
 
 */
 
 
 
 
  import java.awt.*;
  import java.awt.event.*;
  import java.io.*;
   class MyMenuDemo
  {    
 
        private Frame f;
        private MenuBar mb;
        private Menu me,subMenu;
        private MenuItem closeItem,subItem;
       
       
        MyMenuDemo()
        {
              init();
             
        }
       
       
        //初始化
        public void init()
        {
              f= new Frame("My  Window");  //初始化框架
             
              f.setBounds(300,100,600,500);  //設置坐標及大小 : 前兩個坐標,后兩個大小
             
              f.setLayout(new FlowLayout()); //設置布局方式:流式布局
             
             
              mb= new MenuBar();   //初始化菜單條
             
              me= new Menu("文件");    //初始化菜單欄
             
              subMenu=new Menu("子菜單");   //初始化子菜單欄
             
              subItem= new MenuItem("子條目");  //初始化子條目
             
            closeItem= new MenuItem("提出");   //初始化菜單項
             
              subMenu.add(subItem);   //將字條目加入子菜單欄
             
              me.add(subMenu);  //子菜單欄也加入菜單欄
              me.add(closeItem);  //菜單項加入菜單欄
       
              mb.add(me);    //菜單欄加入菜單條
             
              f.setMenuBar(mb);  //菜單條加入窗體
 
              
              //加載事件
              FrameEvent();
             
              f.setVisible(true);   //使框架(窗體)顯示
             
        }
       
 
       
        //窗體事件
        private void FrameEvent()
        {
 
              //窗體的關閉事件
              f.addWindowListener(new WindowAdapter()
              {
                     public void windowClosing(WindowEvent e)
                     {
                            System.exit(0);
                           
                     }
                    
              });
             
              //菜單欄退出事件
              closeItem.addActionListener(new ActionListener()
              {
                     public void actionPerformed(ActionEvent e)
                     {
                            System.exit(0);
                           
                     }
                    
                    
              });
             
             
        }
       
       
 
               //主方法
                public static void main(String args[])
                           {
                                        
                                 new  MyMenuDemo();
                }
                           
                           
             
      
   
     

 
小結 :  

             1.  菜單欄內有多個菜單,菜單內有多個菜單項,菜單項中有多個子菜單
        
           2.  兩個菜單相互迭加形成了一個主菜單,一個子菜單的現象

          
          
 

     十二 GUI(練習--打開文件)

1 ) . Demo:

/*
 
 
 
 本章講述 :
 
   [1] 實現對話框  FileDialog : 調用系統的對話框
  
   [2] 對話框打開文件   :  獲取路徑打開文件
  
   [3] 將對話框文件數據寫入到指定文本域中  : IO流寫入數據
       
       
 
 
 
 */
 
 
 
 
  import java.awt.*;
  import java.awt.event.*;
  import java.io.*;
   class MyOpenDemo
  {    
 
        private Frame f;
        private MenuBar bar;
        private Menu fileMenu;
        private MenuItem openItem,saveitem,closeItem;
        private FileDialog openDialog,saveDialog;
        private TextArea ta;
       
       
        MyOpenDemo()
        {
              init();
             
        }
       
        //打開對話框方法
        private void openDia()
        {
             openDialog = new FileDialog (f,"打開對話框",FileDialog.LOAD);
        
        }
       /* 
        //保存對話框方法
          private void saveDia()
        {
             saveDialog = new FileDialog (f,"保存對話框",FileDialog.SAVE);
             
             
             
             
        } */
        
       
        //初始化
        public void init()
        {
              f= new Frame("My  Window");  //初始化框架
             
              f.setBounds(300,100,600,500);  //設置坐標及大小 : 前兩個坐標,后兩個大小
             
             // f.setLayout(new FlowLayout()); //設置布局方式:流式布局
             
              bar= new MenuBar();   //初始化菜單條
             
              fileMenu= new Menu("文件");    //初始化菜單欄
             
              openItem= new MenuItem("打開");  //初始化子條目
             
              saveitem= new MenuItem("保存");  //初始化子條目
             
            closeItem= new MenuItem("退出");   //初始化菜單項
             
              ta=new TextArea();  //初始化文本域
             
              f.add(ta);  //將文本域添加入窗體組件
             
              //-----------------------------------------------------
             
              fileMenu.add(openItem);   //將條目加入菜單欄
             
              fileMenu.add(saveitem);    
             
              fileMenu.add(closeItem);   
              bar.add(fileMenu);    //將菜單加入菜單條
 
              f.setMenuBar(bar);  //將菜單條加入窗體
 
 
             //------------------------------------------------------
              
              //加載事件
              FrameEvent();
             
              f.setVisible(true);   //使框架(窗體)顯示
             
        }
       
 
       
        //窗體事件
        private void FrameEvent()
        {
              //打開對話框的監聽事件
              openItem.addActionListener(new ActionListener()
              {
                     public void actionPerformed(ActionEvent e)
                     {
                           
                             openDia();  //調用打開對話框的方法
                            
                             openDialog.setVisible(true);  //設置打開對話框顯示
                            
                           String dir =  openDialog.getDirectory();   //獲取目錄路徑
                           
                           String fil =  openDialog.getFile();   //獲取文件路徑
                           
                           if(dir==null||fil==null)    //判斷目錄或文件是否為空,防止空指針
                                 return ;
                           
                           ta.setText("");  //清空文本域內容
                           
                            File fi=null; 
                           
                            BufferedReader br =null;
                           
                            try
                            {
                                   fi =new File(dir,fil);   //初始化文件對象
                                 
                                   br  =new BufferedReader(new FileReader(fi));  //初始化字符流讀取對象
                                 
                                  String line =null;  //設置臨時存取器
                                 
                                  while((line=br.readLine())!=null) 
                                  {
                                        
                                          ta.append(line+"\r\n");  //數據添加至 文本域
                                  }
                                 
                                 
                                 
                            }
                            catch(IOException io)
                            {
                                  throw new RuntimeException("打開文件異常");
                                 
                            }
                            finally
                            {
                                  try
                                  {
                                        br.close();
                                  }
                                  catch(IOException ioe)
                                  {
                                         throw new RuntimeException("讀取流關閉失敗");
                                        
                                  }
                            }
                     }
 
                    
              });
             
       /*       //保存對話框的監聽事件
               saveitem.addActionListener(new ActionListener()
              {
                     public void actionPerformed(ActionEvent e)
                     {
                           
                             saveDia();  //調用打開對話框的方法
                            
                             saveDialog.setVisible(true);  //設置打開對話框顯示
                            
                           
                           
                     }
 
                    
              });  */
             
 
              //窗體的關閉事件
              f.addWindowListener(new WindowAdapter()
              {
                     public void windowClosing(WindowEvent e)
                     {
                            System.exit(0);
                           
                     }
                    
              });
             
              //菜單欄退出事件
              closeItem.addActionListener(new ActionListener()
              {
                     public void actionPerformed(ActionEvent e)
                     {
                            System.exit(0);
                           
                     }
                    
                    
              });
             
             
        }
       
       
 
               //主方法
                public static void main(String args[])
                           {
                                        
                                 new  MyOpenDemo();
                }
                           
                           
             
      
   
     

 

 
小結 :  

             1.  系統自帶的方法就用系統自帶,系統沒有再造 ,一般系統都有,自習閱讀API
        
        
          

     十三 .  GUI(練習--保存文件)

1 ) . Demo:

/*
 
 
 
 本章講述 :
 
   [1] 實現對話框  FileDialog : 調用系統的對話框
  
   [2] 對話框打開文件   :  獲取路徑打開文件
  
   [3] 將對話框文件數據寫入到指定文本域中  : IO流寫入數據
       
   [4] 修改文件后保存,有路徑則直接保存,沒有路徑則創建新的文件
  
 
 
 */
 
 
 
 
  import java.awt.*;
  import java.awt.event.*;
  import java.io.*;
   class MySaveDemo
  {    
 
        private Frame f;
        private MenuBar bar;
        private Menu fileMenu;
        private MenuItem openItem,saveitem,closeItem;
        private FileDialog openDialog,saveDialog;
        private TextArea ta;
       
        private File fi;
       
        MySaveDemo()
        {
              init();
             
        }
       
        //打開對話框方法
        private void openDia()
        {
             openDialog = new FileDialog (f,"打開對話框",FileDialog.LOAD);
        
        }
       
        //保存對話框方法
          private void saveDia()
        {
             saveDialog = new FileDialog (f,"保存對話框",FileDialog.SAVE);
       
        }
        
       
        //初始化
        public void init()
        {
              f= new Frame("My  Window");  //初始化框架
             
              f.setBounds(300,100,600,500);  //設置坐標及大小 : 前兩個坐標,后兩個大小
             
             // f.setLayout(new FlowLayout()); //設置布局方式:流式布局
             
              bar= new MenuBar();   //初始化菜單條
             
              fileMenu= new Menu("文件");    //初始化菜單欄
             
              openItem= new MenuItem("打開");  //初始化子條目
             
              saveitem= new MenuItem("保存");  //初始化子條目
             
            closeItem= new MenuItem("退出");   //初始化菜單項
             
              ta=new TextArea();  //初始化文本域
             
              f.add(ta);  //將文本域添加入窗體組件
             
              //-----------------------------------------------------
             
              fileMenu.add(openItem);   //將條目加入菜單欄
             
              fileMenu.add(saveitem);    
             
              fileMenu.add(closeItem);   
              bar.add(fileMenu);    //將菜單加入菜單條
 
              f.setMenuBar(bar);  //將菜單條加入窗體
 
 
             //------------------------------------------------------
              
              //加載事件
              FrameEvent();
             
              f.setVisible(true);   //使框架(窗體)顯示
             
        }
       
 
       
        //窗體事件
        private void FrameEvent()
        {
              //打開對話框的監聽事件
              openItem.addActionListener(new ActionListener()
              {
                     public void actionPerformed(ActionEvent e)
                     {
                           
                             openDia();  //調用打開對話框的方法
                            
                             openDialog.setVisible(true);  //設置打開對話框顯示
                            
                           String dir =  openDialog.getDirectory();   //獲取目錄路徑
                           
                           String fil =  openDialog.getFile();   //獲取文件路徑
                           
                           if(dir==null||fil==null)    //判斷目錄或文件是否為空,防止空指針
                                 return ;
                           
                           ta.setText("");  //清空文本域內容
                            BufferedReader br =null;
                           
                            try
                            {
                                   fi =new File(dir,fil);   //初始化文件對象
                                 
                                   br  =new BufferedReader(new FileReader(fi));  //初始化字符流讀取對象
                                 
                                  String line =null;  //設置臨時存取器
                                 
                                  while((line=br.readLine())!=null) 
                                  {
                                        
                                          ta.append(line+"\r\n");  //數據添加至 文本域
                                  }
                                 
                                 
                                 
                            }
                            catch(IOException io)
                            {
                                  throw new RuntimeException("打開文件異常");
                                 
                            }
                            finally
                            {
                                  try
                                  {
                                        br.close();
                                  }
                                  catch(IOException ioe)
                                  {
                                         throw new RuntimeException("讀取流關閉失敗");
                                        
                                  }
                            }
                     }
 
                    
              });
             
             //保存對話框的監聽事件
               saveitem.addActionListener(new ActionListener()
              {
                     public void actionPerformed(ActionEvent e)
                     {
                           
                           if(fi==null)   //判斷文件是否為空,為空則創建
                           {
                             saveDia();  //調用打開對話框的方法
                            
                             saveDialog.setVisible(true);  //設置打開對話框顯示
                        String dir =  saveDialog.getDirectory();   //獲取目錄路徑
                           
                           String fil =  saveDialog.getFile();   //獲取文件路徑
                           
                           if(dir==null||fil==null)    //判斷目錄或文件是否為空,防止空指針
                                 return ;
                            
                             fi =new File(dir,fil);   //初始化文件對象
                           }
                           
                           else  //否則則打開文件
                           {
                                 BufferedWriter bw =null; 
                           
                           
                                 try
                                 {
                                 
                                        bw =new BufferedWriter(new FileWriter(fi));  //初始化寫入字符流
                                        
                                        String text = ta.getText();   //獲取文本域數據
                                        
                                        bw.write(text);  //將文本域數據寫入到本地磁盤文件
 
                                 
                                 }
                                 catch(IOException ie)
                                 {
                                        throw new RuntimeException("寫入數據異常");
                                        
                                        
                                 }
                                 finally
                                 {
                                        try
                                        {
                                               bw.close();
                                        }
                                        catch(IOException ex)
                                        {
                                               throw new RuntimeException("關閉寫入流異常");
                                               
                                        }
                                        
                                 }
                                 
                                 
                                 
                           }
                           
                           
                     }
 
                    
              }); 
             
 
              //窗體的關閉事件
              f.addWindowListener(new WindowAdapter()
              {
                     public void windowClosing(WindowEvent e)
                     {
                            System.exit(0);
                           
                     }
                    
              });
             
              //菜單欄退出事件
              closeItem.addActionListener(new ActionListener()
              {
                     public void actionPerformed(ActionEvent e)
                     {
                            System.exit(0);
                           
                     }
                    
                    
              });
             
             
        }
       
       
 
               //主方法
                public static void main(String args[])
                           {
                                        
                                 new  MySaveDemo();
                }
                           
                           
             
      
   
     

  
       十四.  GUI(jar包雙擊執行)


1 ) . 步驟 : 

1.1 第一步 : 在.java文件中加入 package myMenu;     --> 解析  :  package  自定義包名

1.2 第二步 : 帶着包編譯 :  javac -d  c:\  文件名.java    -->  解析 : javac -d  包接下來要存放的地方 以及 要編譯的java文件

1.3第三步 : 自定義文件 ,在文件內寫入信息 :  Main-Class : myMenu.mySaveDemo  -->解析 :  Main-Class : 包名.文件名

[1] 注意 : 后的空格 ,以及 Demo后的回車

1.4 第四步  :生成jar包 :  jar  -cvfm my.jar 1.txt myMenu  : 解析  :  除了 jar - cvf 是固定命令,后邊的依次是 生成的jar的名字 配置信息 和 要生成jar的包名

1.5 第五步 :  jar文件需要在本地注冊過才可用 ,修改注冊表內容 在這個HKEY_CLASSES_ROOT\Applications\javaw.exe\shell\open\command”下修改,以下示例

[1] 注冊表原數值 : "S:\develop\DevDeploy\jdk1.8._10\bin\javaw.exe""%1"

[2]修改后的數值 :  "S:\develop\DevDeploy\jdk1.8._10\bin\javaw.exe" -jar "%1"

[3] 注意 -jar前后的空格 ,格式錯就會失敗哦

2 ) . 若發現一值失敗,則先用命令行運行下試試看:

2.1 命令行行jar包的方式 : cmd  -->前往jar存在的地址--> java -jar  文件名.jar  

3 ) .  Demo:

package myMenu;
 
  import java.awt.*;
  import java.awt.event.*;
  import java.io.*;
 public class MySaveDemo
  {    
 
        private Frame f;
        private MenuBar bar;
        private Menu fileMenu;
        private MenuItem openItem,saveitem,closeItem;
        private FileDialog openDialog,saveDialog;
        private TextArea ta;
       
        private File fi;
       
        MySaveDemo()
        {
              init();
             
        }
       
        //打開對話框方法
        private void openDia()
        {
             openDialog = new FileDialog (f,"打開對話框",FileDialog.LOAD);
        
        }
       
        //保存對話框方法
          private void saveDia()
        {
             saveDialog = new FileDialog (f,"保存對話框",FileDialog.SAVE);
       
        }
        
       
        //初始化
        public void init()
        {
              f= new Frame("My  Window");  
             
              f.setBounds(300,100,600,500); 
             
             // f.setLayout(new FlowLayout()); 
             
              bar= new MenuBar();  
             
              fileMenu= new Menu("文件");    
             
              openItem= new MenuItem("打開"); 
             
              saveitem= new MenuItem("保存"); 
             
            closeItem= new MenuItem("退出");  
             
              ta=new TextArea();  
             
              f.add(ta); 
             
              //-----------------------------------------------------
             
              fileMenu.add(openItem);  
             
              fileMenu.add(saveitem);    
             
              fileMenu.add(closeItem);   
              bar.add(fileMenu);    
 
              f.setMenuBar(bar);
 
 
             //------------------------------------------------------
              
             
              FrameEvent();
             
              f.setVisible(true);  
             
        }
       
 
       
        //窗體事件
        private void FrameEvent()
        {
              //打開對話框的監聽事件
              openItem.addActionListener(new ActionListener()
              {
                     public void actionPerformed(ActionEvent e)
                     {
                           
                             openDia();
                            
                             openDialog.setVisible(true);  
                            
                           String dir =  openDialog.getDirectory();   
                           
                           String fil =  openDialog.getFile();   
                           
                           if(dir==null||fil==null)   
                                 return ;
                           
                           ta.setText("");  
                            BufferedReader br =null;
                           
                            try
                            {
                                   fi =new File(dir,fil);   
                                 
                                   br  =new BufferedReader(new FileReader(fi));  
                                 
                                  String line =null; 
                                 
                                  while((line=br.readLine())!=null) 
                                  {
                                        
                                          ta.append(line+"\r\n");  
                                  }
                                 
                                 
                                 
                            }
                            catch(IOException io)
                            {
                                  throw new RuntimeException("打開文件異常");
                                 
                            }
                            finally
                            {
                                  try
                                  {
                                        br.close();
                                  }
                                  catch(IOException ioe)
                                  {
                                         throw new RuntimeException("讀取流關閉失敗");
                                        
                                  }
                            }
                     }
 
                    
              });
             
             //保存對話框的監聽事件
               saveitem.addActionListener(new ActionListener()
              {
                     public void actionPerformed(ActionEvent e)
                     {
                           
                           if(fi==null)  
                           {
                             saveDia(); 
                            
                             saveDialog.setVisible(true); 
                        String dir =  saveDialog.getDirectory();  
                           
                           String fil =  saveDialog.getFile();  
                           
                           if(dir==null||fil==null)   
                                 return ;
                            
                             fi =new File(dir,fil);  
                           }
                           
                           else  
                           {
                                 BufferedWriter bw =null; 
                           
                           
                                 try
                                 {
                                 
                                        bw =new BufferedWriter(new FileWriter(fi));
                                        
                                        String text = ta.getText(); 
                                        
                                        bw.write(text); 
 
                                 
                                 }
                                 catch(IOException ie)
                                 {
                                        throw new RuntimeException("寫入數據異常");
                                        
                                        
                                 }
                                 finally
                                 {
                                        try
                                        {
                                               bw.close();
                                        }
                                        catch(IOException ex)
                                        {
                                               throw new RuntimeException("關閉寫入流異常");
                                               
                                        }
                                        
                                 }
                                 
                                 
                                 
                           }
                           
                           
                     }
 
                    
              }); 
             
 
              //窗體的關閉事件
              f.addWindowListener(new WindowAdapter()
              {
                     public void windowClosing(WindowEvent e)
                     {
                            System.exit(0);
                           
                     }
                    
              });
             
              //菜單欄退出事件
              closeItem.addActionListener(new ActionListener()
              {
                     public void actionPerformed(ActionEvent e)
                     {
                            System.exit(0);
                           
                     }
                    
                    
              });
             
             
        }
       
       
 
               //主方法
                public static void main(String args[])
                           {
                                        
                                 new  MySaveDemo();
                }
                           
                           
             
      
   
     

4 ) . 大白話總結 : 

4.1 當我們想讓jar運行的前提是:  

第一步 : 先編譯,將class文件放入一個目錄, 

第二步 : 然后在目錄內加入自定義的清單文件(這份文件有規定格式)

第三步 : 之后通過命令打包成ajr,然后若運行不了,

第四步 :  則去給注冊表中jdk的javaw 添加 -jar屬性即可運行


 

      十五. 總結

1 ) . 關於相關java運行jar包的功能:

1.1 java.exe 用於啟動window console 控制台程序

1.2 javaw.exe 用於啟動GUI程序

1.3 javaws.exe 用於啟動通過web配置的程序,就是在web應用程序中應用

1.4 jvm.dll 就是java虛擬機規范在windows平台上的一種實現

2 ) . GUI主要功能組成 : 

2.1 frame --> 

3 ) .  開發時看圖找功能功能即可  :  

3.1 本類沒有,則找父類,父類沒有則找其實現類,只要是一個體系內的方法隨意用

3.2 適配器就是用來實現接口中所用方法,以此供開發者隨意挑選使用的

3.3 功能模塊化,盡量高內聚低耦合,能抽取成另一個獨立的方法那就盡量抽取,以便復用


 

小結 :  

             1.  GUI是圖形化界面的開發,善用API功能方法,善於發現可調用的系統內部方法
        
     
 

    





注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com