我做的记事本怎么一直打开不了文件,也刷新不了很是郁闷,代码如下,希望得到高手指点import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.datatransfer.*;public class MyMemo extends Frame implements ActionListener{
private TextArea editor=new TextArea();
private TextArea editor1=new TextArea();//another textarea
private MyFile mf=new MyFile(this);
public MyMemo(String title){
super(title);
MyMenuBar mb=new MyMenuBar(this);
mb.addMenus(new String[]{"File","Do"});
mb.addMenuItem(0,new String[]{"New","Open","Save","Exit"});
mb.addMenuItem(1,new String[]{"Point","Chart"});
add(editor);
mb.addActionListener(this);
add(editor1);
mb.addActionListener(this);//add a new textarea
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
MyMemo.this.dispose();
}
});
}
public void actionPerformed(ActionEvent e){
String selected=e.getActionCommand();
if(selected.equals("New"))
editor.setText("");
else if(selected.equals("Open")){
try{editor.setText(mf.getData());
}catch(IOException ie){}
}
else if(selected.equals("Save")){
try{
mf.setData(editor.getText());
}catch(IOException ie){}
}
else if(selected.equals("Exit")){
dispose();
}
}
public static void main(String[] args){
MyMemo memo=new MyMemo("Compiler");
memo.setSize(600,600);
memo.setVisible(true);
}
}class MyMenuBar extends MenuBar{
public MyMenuBar(Frame parent){
parent.setMenuBar(this);
}
public void addMenus(String[] menus){
for(int i=0;i<menus.length;i++)
add(new Menu(menus[i]));
}
public void addMenuItem(int menuNumber,String[] items){
for(int i=0;i<items.length;i++){
if(items[i]!=null)
getMenu(menuNumber).add(new MenuItem(items[i]));
else getMenu(menuNumber).addSeparator();
}
}
public void addActionListener(ActionListener al){
for(int i=0;i<getMenuCount();i++)
for(int j=0;j<getMenu(i).getItemCount();j++)
getMenu(i).getItem(j).addActionListener(al);
}
}class MyFile {
private FileDialog fDlg;
public MyFile(Frame parent){
fDlg=new FileDialog(parent,"",FileDialog.LOAD);
}
private String getPath(){
return fDlg.getDirectory()+"\\"+fDlg.getFile();
}
public String getData() throws IOException{
fDlg.setTitle("Open");
fDlg.setMode(FileDialog.LOAD);
fDlg.setVisible(true);
BufferedReader br=new BufferedReader(new FileReader(getPath()));
StringBuffer sb=new StringBuffer();
String aline;
while((aline=br.readLine())!=null)
sb.append(aline+"\n");
//System.out.println(br.readLine());//
br.close();
return sb.toString();
}
public void setData(String data) throws IOException{
fDlg.setTitle("save");
fDlg.setMode(FileDialog.SAVE);
fDlg.setVisible(true);
BufferedWriter bw=new BufferedWriter(new FileWriter(getPath()));
bw.write(data);
bw.close();
}
}
class MyClipboard{
private Clipboard cb;
public MyClipboard(){
cb=Toolkit.getDefaultToolkit().getSystemClipboard();
}
public void setData(String data){
cb.setContents(new StringSelection(data),null);
}
public String getData(){
Transferable content=cb.getContents(null);
try{
return (String)content.getTransferData(DataFlavor.stringFlavor);
}catch(Exception ue){}
return null;
}
}
import java.awt.event.*;
import java.io.*;
import java.awt.datatransfer.*;public class MyMemo extends Frame implements ActionListener{
private TextArea editor=new TextArea();
private TextArea editor1=new TextArea();//another textarea
private MyFile mf=new MyFile(this);
public MyMemo(String title){
super(title);
MyMenuBar mb=new MyMenuBar(this);
mb.addMenus(new String[]{"File","Do"});
mb.addMenuItem(0,new String[]{"New","Open","Save","Exit"});
mb.addMenuItem(1,new String[]{"Point","Chart"});
add(editor);
mb.addActionListener(this);
add(editor1);
mb.addActionListener(this);//add a new textarea
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
MyMemo.this.dispose();
}
});
}
public void actionPerformed(ActionEvent e){
String selected=e.getActionCommand();
if(selected.equals("New"))
editor.setText("");
else if(selected.equals("Open")){
try{editor.setText(mf.getData());
}catch(IOException ie){}
}
else if(selected.equals("Save")){
try{
mf.setData(editor.getText());
}catch(IOException ie){}
}
else if(selected.equals("Exit")){
dispose();
}
}
public static void main(String[] args){
MyMemo memo=new MyMemo("Compiler");
memo.setSize(600,600);
memo.setVisible(true);
}
}class MyMenuBar extends MenuBar{
public MyMenuBar(Frame parent){
parent.setMenuBar(this);
}
public void addMenus(String[] menus){
for(int i=0;i<menus.length;i++)
add(new Menu(menus[i]));
}
public void addMenuItem(int menuNumber,String[] items){
for(int i=0;i<items.length;i++){
if(items[i]!=null)
getMenu(menuNumber).add(new MenuItem(items[i]));
else getMenu(menuNumber).addSeparator();
}
}
public void addActionListener(ActionListener al){
for(int i=0;i<getMenuCount();i++)
for(int j=0;j<getMenu(i).getItemCount();j++)
getMenu(i).getItem(j).addActionListener(al);
}
}class MyFile {
private FileDialog fDlg;
public MyFile(Frame parent){
fDlg=new FileDialog(parent,"",FileDialog.LOAD);
}
private String getPath(){
return fDlg.getDirectory()+"\\"+fDlg.getFile();
}
public String getData() throws IOException{
fDlg.setTitle("Open");
fDlg.setMode(FileDialog.LOAD);
fDlg.setVisible(true);
BufferedReader br=new BufferedReader(new FileReader(getPath()));
StringBuffer sb=new StringBuffer();
String aline;
while((aline=br.readLine())!=null)
sb.append(aline+"\n");
//System.out.println(br.readLine());//
br.close();
return sb.toString();
}
public void setData(String data) throws IOException{
fDlg.setTitle("save");
fDlg.setMode(FileDialog.SAVE);
fDlg.setVisible(true);
BufferedWriter bw=new BufferedWriter(new FileWriter(getPath()));
bw.write(data);
bw.close();
}
}
class MyClipboard{
private Clipboard cb;
public MyClipboard(){
cb=Toolkit.getDefaultToolkit().getSystemClipboard();
}
public void setData(String data){
cb.setContents(new StringSelection(data),null);
}
public String getData(){
Transferable content=cb.getContents(null);
try{
return (String)content.getTransferData(DataFlavor.stringFlavor);
}catch(Exception ue){}
return null;
}
}
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.datatransfer.*;public class MyMemo extends Frame implements ActionListener {
private TextArea editor = new TextArea(); private TextArea editor1 = new TextArea();// another textarea private MyFile mf = new MyFile(this); public MyMemo(String title) {
super(title);
MyMenuBar mb = new MyMenuBar(this); mb.addMenus(new String[] { "File", "Do" });
mb.addMenuItem(0, new String[] { "New", "Open", "Save", "Exit" });
mb.addMenuItem(1, new String[] { "Point", "Chart" }); add(editor);
mb.addActionListener(this); //add(editor1);
//mb.addActionListener(this);// add a new textarea addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
MyMemo.this.dispose();
}
}); } public void actionPerformed(ActionEvent e) {
String selected = e.getActionCommand(); if (selected.equals("New"))
editor.setText(""); else if (selected.equals("Open")) {
try {
editor.setText(mf.getData());
} catch (IOException ie) {
ie.printStackTrace();
}
} else if (selected.equals("Save")) {
try {
mf.setData(editor.getText());
} catch (IOException ie) {
ie.printStackTrace();
}
} else if (selected.equals("Exit")) {
dispose();
}
} public static void main(String[] args) {
MyMemo memo = new MyMemo("Compiler");
memo.setSize(600, 600);
memo.setVisible(true);
}}class MyMenuBar extends MenuBar {
public MyMenuBar(Frame parent) {
parent.setMenuBar(this);
} public void addMenus(String[] menus) {
for (int i = 0; i < menus.length; i++)
add(new Menu(menus[i]));
} public void addMenuItem(int menuNumber, String[] items) {
for (int i = 0; i < items.length; i++) {
if (items[i] != null)
getMenu(menuNumber).add(new MenuItem(items[i]));
else
getMenu(menuNumber).addSeparator();
}
} public void addActionListener(ActionListener al) {
for (int i = 0; i < getMenuCount(); i++)
for (int j = 0; j < getMenu(i).getItemCount(); j++)
getMenu(i).getItem(j).addActionListener(al);
}
}class MyFile {
private FileDialog fDlg; public MyFile(Frame parent) {
fDlg = new FileDialog(parent, "", FileDialog.LOAD);
} private String getPath() {
return fDlg.getDirectory() + "\\" + fDlg.getFile();
} public String getData() throws IOException {
fDlg.setTitle("Open");
fDlg.setMode(FileDialog.LOAD);
fDlg.setVisible(true);
BufferedReader br = new BufferedReader(new FileReader(getPath()));
StringBuffer sb = new StringBuffer();
String aline;
while ((aline = br.readLine()) != null)
sb.append(aline + "\n");
// System.out.println(br.readLine());//
br.close();
return sb.toString();
} public void setData(String data) throws IOException {
fDlg.setTitle("save");
fDlg.setMode(FileDialog.SAVE);
fDlg.setVisible(true);
BufferedWriter bw = new BufferedWriter(new FileWriter(getPath()));
bw.write(data);
bw.close();
}
}class MyClipboard {
private Clipboard cb; public MyClipboard() {
cb = Toolkit.getDefaultToolkit().getSystemClipboard();
} public void setData(String data) {
cb.setContents(new StringSelection(data), null);
} public String getData() {
Transferable content = cb.getContents(null);
try {
return (String) content.getTransferData(DataFlavor.stringFlavor);
} catch (Exception ue) {
}
return null;
}
}
能否再请教一下,如果我想在我的记事本上设置两个TextArea,一个用来打开,一个用来输出,那应该如何实现呢,谢谢