JAVA 案例教程(1)

來源:互聯網
上載者:User
教程 // ************************************
// 一個簡單的JAVA 程式
// 功能是示範一個小視窗,並具有讀寫檔案的功能。
// @author Gaogao
// @date 2004-11-04
// ************************************

// ************************************
// 引入GUI和GUI事件處理的類,和I/O管理的類
// ************************************
import java.awt.*;
import java.awt.event.*;
import java.io.*;

// ************************************
// TestOne,一個示範菜單和按鈕的Application
// 程式。
// ************************************
public class TestOne extends Frame implements ActionListener {
//繼承自Frame,實現ActionListener介面
//Frame是一個表單的架構類,Actionlistener是事件介面,實現他
//可以做事件處理。

// ************************************
// 聲明變數
// ************************************
// 菜單控制
private MenuBar mb;
private Menu mFile;
private MenuItem miLoad;
private MenuItem miSave;
private MenuItem miExit;
// 按鈕
private TextArea textArea;
// 磁碟管理
private File file;
// 檔案名稱
public static final String FILENAME_STR = "Love.TXT";
// ************************************
// 構造
// ************************************
public TestOne() {
init();

putObjectsToMyFrame();
addListenersToObjects();
setTheWindow();
}
// ************************************
// 指派至記憶體
// ************************************
private void init() {
mb = new MenuBar();
mFile = new Menu("File");
miLoad = new MenuItem("Load");
miSave = new MenuItem("Save");
miExit = new MenuItem("Exit");

textArea = new TextArea("");
}

// ************************************
// 裝各個組件到視窗
// ************************************
private void putObjectsToMyFrame() {
setMenuBar(mb);
mb.add(mFile);
mFile.add(miLoad);
mFile.add(miSave);
mFile.add(miExit);
add(textArea);
}

// ************************************
// 設定視窗
// ************************************
private void setTheWindow() {
this.setSize(400,300);
this.show();
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
);
}
// ************************************
// 設定事件監聽器
// ************************************
private void addListenersToObjects() {
miLoad.addActionListener(this);//將miLoad這個菜單的事件監聽放在這個視窗的執行個體中
miSave.addActionListener(this);
miExit.addActionListener(this);

}
// ************************************
// 事件監聽器(就是這個類的案例)監聽到事件後
// 調用。此方法是ActionListener介面中抽象
// 方法的實現。
// ************************************
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == miLoad)
{

//
System.out.println("Loading");
DataInputStream dis;

try {
file = new File(FILENAME_STR);
dis = new DataInputStream(new FileInputStream(file));
textArea.setText("");
////沒JAVA DOC 文檔 不會寫了。待完善。
dis.close();
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}




}
else if (ae.getSource() == miSave)
{
//
System.out.println("Saving");
DataOutputStream dos;
try {
file = new File(FILENAME_STR);
dos = new DataOutputStream(new FileOutputStream(file));
int i = 0;
String temp = textArea.getText();
while (i < temp.length())
{
dos.writeChar(temp.charAt(i++));
}

dos.close();
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}


}
else if (ae.getSource() == miExit)
{
//退出應用程式
System.out.println("Exit");
System.exit(0);
}

}
// ************************************
// 程式入口。
// ************************************
public static void main(String[] args) {
new TestOne();
}
}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.