计算器问题...

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 @   胡.杰  阅读(230)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示