用AWT实现的,有些功能还没有实现。
/**
* AWT的记事本应用程序
* @author Rabbit
*/
package com.sinojava.two;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MenuOne {
//记事本的组件变量声明
private Frame f;
private MenuBar mb;
private Menu m1,m2,m3;
private MenuItem mi1,mi2,mi3,mi4,mi5;
private CheckboxMenuItem cmi;
private TextArea ta;
private FileDialog fd1,fd2;
private Dialog d;
private Button b1,b2,b3;
private String str1,str2,line,result="";
//MenuOne的构造器,构造记事本
public MenuOne() {
//窗体的定义
f = new Frame("MyMenu");
fd1 = new FileDialog(f,"Open",FileDialog.LOAD);
fd2 = new FileDialog(f,"Save",FileDialog.SAVE);
ta = new TextArea();
//对话框的实现
d = new Dialog(f,"是否保存文件?");
d.setLocation(300, 300);
b1 = new Button("Yes");
b2 = new Button("Cancel");
b3 = new Button("No");
d.setLayout(new FlowLayout());
d.add(b1);
d.add(b3);
d.add(b2);
d.setSize(100,100);
d.pack();
//菜单的创建和添加
f.add(ta,BorderLayout.CENTER);
mb = new MenuBar();
m1 = new Menu("File");
m2 = new Menu("Edit");
m3 = new Menu("Help");
mb.add(m1);
mb.add(m2);
mb.setHelpMenu(m3);
//子菜单的创建
f.setMenuBar(mb);
mi1 = new MenuItem("New");
mi2 = new MenuItem("Open");
mi4 = new MenuItem("Save");
mi5 = new MenuItem("Print");
mi3 = new MenuItem("Exit");
cmi = new CheckboxMenuItem("Select");
//向File内加入子菜单
m1.add(mi1);
m1.add(mi2);
m1.add(mi4);
m1.addSeparator();
m1.add(mi5);
m1.addSeparator();
m1.add(mi3);
m1.addSeparator();
m1.add(cmi);
//退出整个记事本
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//新建Yes的状态
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fd2.show();
str2 = fd2.getDirectory()+fd2.getFile();
String a = ta.getText();
writeFile(str2,a);
d.setVisible(false);
ta.setText(null);
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
d.setVisible(false);
ta.setText(null);
}
});
//新建Cancel的状态
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
d.setVisible(false);
}
});
//新建一个文本
mi1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String b = ta.getText();
System.out.println(b);
if(b.equals(""))
{
d.setVisible(false);
}
else
{
d.setVisible(true);
}
}
});
//打开一个文本
mi2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fd1.show();
str1 = fd1.getDirectory()+fd1.getFile();
ta.setText(readFile(str1));
}
});
//保存一个文本
mi4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fd2.show();
str2 = fd2.getDirectory()+fd2.getFile();
String a = ta.getText();
writeFile(str2,a);
}
});
//点击退出
mi3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(1);
}
});
//选择性条目
cmi.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange()==ItemEvent.SELECTED) {
ta.setText("Select");
}
if(e.getStateChange()==ItemEvent.DESELECTED) {
ta.setText(null);
}
}
});
//实现打印在后台上
mi5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String c = ta.getText();
System.out.println(c);
}
});
f.setSize(500,500);
f.setVisible(true);
}
//读取一个文件
public String readFile(String s)
{
try
{
FileReader fr = new FileReader(str1);
BufferedReader br = new BufferedReader(fr);
while((line=br.readLine())!=null)
{
result+=line+"\n";
}
fr.close();
br.close();
}catch(IOException e)
{
System.out.println("Error:"+e.getMessage());
}
return result;
}
//写入一个文件
public void writeFile(String s,String ss) {
try {
//BufferedWriter bw = new (fw);
PrintWriter out
= new PrintWriter(new FileWriter(s),true);
out.println(ss+"\n");
out.flush();
out.close();
}
catch(IOException e) {
System.out.println("Error:"+e.getMessage());
}
}
}
public class MyMenu {
public static void main(String[] args) {
//创建构造函数MenuOne来运行
MenuOne mo = new MenuOne();
}
}