《Java應用程式(Application)》

來源:互聯網
上載者:User

標籤:

  1 在編寫Java應用程式(Application)時可以這樣:  2   3 1,定義包名.  4 2, 匯入相關的包.  5 3, 定義一個類。  6 4,定義相關變數。  7 5,定義建構函式。(在建構函式內調用init()方法和addEvents()方法)  8 6, 在init()函數中組合各種組件。  9 7,在addEvents()方法中為各種組件添加事件監聽器。(可以有異常的捕獲及聲明)。 10 8,定義主函數。在主函數中建立一個本類的對象就可以了。 11    通過建構函式就可以完成調用程式的各種功能。 12  13 例子如下: 14  15 package myclass; 16 import java.awt.FileDialog; 17 import java.awt.Frame; 18 import java.awt.Menu; 19 import java.awt.MenuItem; 20 import java.awt.MenuBar; 21 import java.awt.TextArea; 22 import java.awt.event.ActionEvent; 23 import java.awt.event.ActionListener; 24 import java.awt.event.WindowEvent; 25 import java.awt.event.WindowAdapter; 26 import java.io.BufferedReader; 27 import java.io.File; 28 import java.io.FileReader; 29  30 public class Demo1  31 { 32     private Frame frame; 33     private MenuBar menuBar; 34     private Menu menu; 35     private Menu subMenu; 36     private MenuItem exitItem,openFileItem; 37     private FileDialog openFile; 38     private TextArea ta; 39  40     //建構函式. 41     public Demo1(){ 42         init(); 43         addEvents(); 44     } 45     public void init(){ 46         frame = new Frame("菜單測試"); 47         frame.setBounds(300,200,500,400); 48         //功能表列. 49         menuBar = new MenuBar(); 50         //菜單。 51         menu = new Menu("檔案"); 52         //功能表項目. 53         openFileItem = new MenuItem("開啟"); 54         exitItem = new MenuItem("退出"); 55         //添加功能表項目. 56         menu.add(openFileItem); 57         menu.add(exitItem); 58         //功能表列添加菜單. 59         menuBar.add(menu); 60         frame.setMenuBar(menuBar); 61         //文本域. 62         ta = new TextArea(); 63         frame.add(ta); 64         frame.setVisible(true); 65     } 66  67     public void addEvents(){ 68         frame.addWindowListener(new WindowAdapter(){ 69             public void windowClosing(WindowEvent e){ 70                 System.exit(0); 71             } 72         }); 73         //菜單單擊退出. 74         exitItem.addActionListener(new ActionListener(){ 75             public void actionPerformed(ActionEvent e){ 76                 System.exit(0); 77             } 78         }); 79         //開啟檔案. 80         openFileItem.addActionListener(new ActionListener(){ 81             public void actionPerformed(ActionEvent e){ 82                 openFile = new FileDialog(frame,"開啟檔案",FileDialog.LOAD); 83                 openFile.setVisible(true); 84                 String dirName = openFile.getDirectory(); 85                 String fileName = openFile.getFile(); 86                 System.out.println(dirName); 87                 //讀取展示檔案. 88                 if(dirName == null || fileName == null){ 89                     return; 90                 } 91                 File file = new File(dirName,fileName); 92                 try 93                 { 94                     BufferedReader br = new BufferedReader(new FileReader(file)); 95                     String line; 96                     StringBuilder text = new StringBuilder(); 97                     while((line = br.readLine())!=null){ 98                         text.append(line); 99                         text.append("\r\n");100                     }101                     ta.setText(text.toString());102                 }103                 catch (Exception e1)104                 {105                     e1.printStackTrace();106                 }107             }108         });109     }110     public static void main(String[] args) 111     {112         new Demo1();113     }114 }

 

《Java應用程式(Application)》

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.