java期末作业!!求大神帮忙!!! 50

1240430684@.qqcom... 1240430684@.qqcom 展开
 我来答
liuyang054
2016-06-07 · TA获得超过9093个赞
知道大有可为答主
回答量:5317
采纳率:78%
帮助的人:5147万
展开全部
/**
 *@author young
 *@Description: 记事本
 *@date 2016年5月19日下午5:37:06
 */
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.datatransfer.*;

public class NotePad01 {
public static void main(String args[]) {
TextEdit TE = new TextEdit("记事本");
}
}

class TextEdit extends Frame implements ActionListener {
MenuBar m;
Menu m1, m2;
MenuItem xinjian, dakai, baocun, tuichu, jianqie, fuzhi, zhantie;
TextArea text;
String filename;
FileDialog openFD, saveFD;
BufferedReader in;
FileReader read;
BufferedWriter out;
FileWriter writer;
Clipboard cb;

TextEdit(String s) {
super(s);
m = new MenuBar();
m1 = new Menu("File");
xinjian = new MenuItem("New");
dakai = new MenuItem("Open");
baocun = new MenuItem("Save");
tuichu = new MenuItem("Exit");
m2 = new Menu("Edit");
jianqie = new MenuItem("Cut");
fuzhi = new MenuItem("Copy");
zhantie = new MenuItem("Paste");
text = new TextArea();
openFD = new FileDialog(this, "Open", FileDialog.LOAD);
saveFD = new FileDialog(this, "Save", FileDialog.SAVE);
filename = "NoName";

m1.add(xinjian);
m1.addSeparator();
m1.add(dakai);
m1.addSeparator();
m1.add(baocun);
m1.addSeparator();
m1.add(tuichu);
m2.add(jianqie);
m2.addSeparator();
m2.add(fuzhi);
m2.addSeparator();
m2.add(zhantie);
m.add(m1);
m.add(m2);

// 关键部分,没有为cb申请内存,下面对cb操作会出错
cb = new Clipboard("nothing");
//

setMenuBar(m);
setSize(300, 400);
setVisible(true);
add(text, "Center");

xinjian.addActionListener(this);
dakai.addActionListener(this);
baocun.addActionListener(this);
tuichu.addActionListener(this);
jianqie.addActionListener(this);
fuzhi.addActionListener(this);
zhantie.addActionListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == xinjian) {
text.setText("");
}
if (e.getSource() == dakai) {
openFD.show();
String s;
filename = openFD.getDirectory() + openFD.getFile();
if (filename != null) {
try {
File file = new File(filename);
read = new FileReader(file);
in = new BufferedReader(read);
while ((s = in.readLine()) != null)
text.append(s + '\n');
in.close();
read.close();
} catch (IOException e2) {
}
}
}

if (e.getSource() == baocun) {
saveFD.show();
filename = saveFD.getDirectory() + saveFD.getFile();
if (filename != null) {
try {
File file = new File(filename);
writer = new FileWriter(file);
out = new BufferedWriter(writer);
out.write(text.getText(), 0, (text.getText()).length());

out.close();
writer.close();
} catch (IOException e2) {
}
}
}
if (e.getSource() == tuichu) {
System.exit(0);
}

if (e.getSource() == jianqie) {
// 类text中没有cut方法,不能使用text.cut
String s = text.getSelectedText();
StringSelection select = new StringSelection(s);
cb.setContents(select, null);
text.replaceRange("", text.getSelectionStart(),
text.getSelectionEnd());
}
if (e.getSource() == fuzhi) {
// 同上,没有copy这个方法
String s = text.getSelectedText();
StringSelection select = new StringSelection(s);
cb.setContents(select, null);
}
if (e.getSource() == zhantie) {
// 同上,没有paste方法
String s = "";
Transferable t = cb.getContents(null);
try {
if (t != null
&& t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
// 因为原系的剪贴板里有多种信息, 如文字, 图片, 文件等
// 先判断开始取得的可传输的数据是不是文字, 如果是, 取得这些文字

s = (String) t.getTransferData(DataFlavor.stringFlavor);
// 同样, 因为Transferable中的DataFlavor是多种类型的,
// 所以传入DataFlavor这个参数, 指定要取得哪种类型的Data.
// System.out.println(s);
}
} catch (UnsupportedFlavorException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}

text.insert(s, text.getCaretPosition());

}

}

}
生活he家
2016-06-07 · 一个属于大家的搬运工
生活he家
采纳数:1536 获赞数:6828

向TA提问 私信TA
展开全部
有一个 五子棋源码
追问
能弄个记事本写三个人各自负责什么吗   你可以随便弄三个人名字。。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式