计算器问题...

import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class TextEditor {
	Frame ft=new Frame("TextEditor");
	TextArea ta=new TextArea("",80,60);
//	ft.setSize(800,600);                 ---------->如果放在这儿,就被报错误.      
	MenuBar mb=new MenuBar();
	Menu m=new Menu("Menu");
	MenuItem mis=new MenuItem("Save");
	MenuItem mie=new MenuItem("Exit");
	String str="FUCK";
	
	void init() {
	Jiant jt=new Jiant(this);
	Record r=new Record(this);
	ft.setSize(800,600);               //---------->应该放在这儿,就会没问题.难道方法外不能使用方法,只能定义成员变量?
	mis.addActionListener(jt);
	mie.addActionListener(jt);
	ta.addKeyListener(r);
	
	m.add(mis);
	m.add(mie);
	mb.add(m);

	
	ft.add(ta);
	ft.addWindowListener(r);
	ft.setMenuBar(mb);
	ft.setVisible(true);
	}
	
	public static void main(String args[]) {
	//	TextEditor te=new TextEditor();
	TextEditor te=new TextEditor();
	te.init();	
	}
}

class Jiant implements ActionListener {
	TextEditor te;
	Jiant(TextEditor tec) {
		te=tec;
	}
	public void actionPerformed(ActionEvent e) {
	if((e.getActionCommand()).equals("Save")) {
	//	Listen to Save
	//	te.ta.insert("this is a great test",0);
	try {
	PrintWriter pw=new PrintWriter(new FileOutputStream("C:/Users/IBM_ADMIN/Desktop/test.txt"),true);
	pw.print(te.str); 
	pw.close();} catch (Exception en) {}
	}
	if((e.getActionCommand()).equals("Exit")) {
	//	Listen to Exit
		te.ta.insert(te.str,0);
	}
	
	}
}

class Record implements KeyListener ,WindowListener{
	TextEditor te;
	Record(TextEditor tec) {
		te=tec;
	}
	public void keyPressed(KeyEvent e) {
		
	}
	public void keyReleased(KeyEvent e) {}
	public void keyTyped(KeyEvent e) {
	te.str+=e.getKeyChar();
	}
	
	public void windowClosing(WindowEvent e) {
		System.exit(0);
	}
	public void windowClosed(WindowEvent e){}
	public void windowIconified(WindowEvent e) {}
	public void windowDeiconified(WindowEvent e){}
	public void windowActivated(WindowEvent e){}
	public void windowDeactivated(WindowEvent e){}
	public void windowOpened(WindowEvent e) {}
	
}

 

类里面只能有属性和方法,不能直接出现方法的调用,方法的调用要在类的方法里面调用! 

 

posted @ 2012-06-30 02:29  胡.杰  阅读(227)  评论(0编辑  收藏  举报