实现记事本里面得打开,保存,另存为,退出得功能
保存这块不会写 其它得都已实现 麻烦高手指点一下。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class Test{
public static void main(String[] args){
final Frame f=new Frame("ten");
f.setSize(600,500);
f.setLocation(100,100);
MenuBar mb=new MenuBar();
final TextArea ta=new TextArea();
f.add(ta);
Menu m1=new Menu("文件");
Menu m2=new Menu("编辑");
MenuItem mi1=new MenuItem("新建");
MenuItem mi2=new MenuItem("打开");
mi2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
FileDialog fd=new FileDialog(f,"打开",FileDialog.LOAD);
fd.setVisible(true);
String s=fd.getDirectory()+fd.getFile();
try{
ta.setText("");
FileInputStream fis=new FileInputStream(s);
//if(s!=null)
byte[] b=new byte[10*1024];
int i=fis.read(b);
ta.append(new String(b,0,i));
fis.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
});
MenuItem mi3=new MenuItem("保存");
mi3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
});
MenuItem mi31=new MenuItem("另保存");
mi31.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
FileDialog fd=new FileDialog(f,"保存",FileDialog.SAVE);
fd.setVisible(true);
String s=fd.getDirectory()+fd.getFile();
try{
FileOutputStream fos=new FileOutputStream(s);
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(fos));
String ss=ta.getText();
bw.write(ss);
bw.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
});
MenuItem mi4=new MenuItem("退出");
mi4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
MenuItem mi5=new MenuItem("复制");
MenuItem mi6=new MenuItem("粘贴");
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
m1.add(mi31);
m1.add(mi4);
m2.add(mi5);
m2.add(mi6);
mb.add(m1);
mb.add(m2);
f.setMenuBar(mb);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
f.setVisible(true);
}
}
简简单单帮你完成个,添功能我喜欢。
如果你是打开文本,修改后,保存,它不会弹出对话框。
如果不是,添加文本后,保存,它会弹出对话框。。。。
以下是修改后的代码,能复制用的,我就懒的写,还是需要你的另存为代码。。。。。。。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Test{
private static boolean flag;
private static String path;
public static void main(String[] args){
final Frame f=new Frame("ten");
f.setSize(600,500);
f.setLocation(100,100);
MenuBar mb=new MenuBar();
final TextArea ta=new TextArea();
f.add(ta);
Menu m1=new Menu("文件");
Menu m2=new Menu("编辑");
MenuItem mi1=new MenuItem("新建");
MenuItem mi2=new MenuItem("打开");
mi2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
FileDialog fd=new FileDialog(f,"打开",FileDialog.LOAD);
fd.setVisible(true);
path=fd.getDirectory()+fd.getFile();
try{
ta.setText("");
FileInputStream fis=new FileInputStream(path);
byte[] b=new byte[10*1024];
int i=fis.read(b);
ta.append(new String(b,0,i));
fis.close();
flag=true;
}catch(Exception ex){
ex.printStackTrace();
}
}
});
MenuItem mi3=new MenuItem("保存");
mi3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
BufferedWriter bw;
FileOutputStream fos;
if(flag)
{
try {
fos = new FileOutputStream(path);
bw=new BufferedWriter(new OutputStreamWriter(fos));
String ss=ta.getText();
bw.write(ss);
bw.flush();
fos.flush();
fos.close();
bw.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
else
{
FileDialog fd=new FileDialog(f,"保存",FileDialog.SAVE);
fd.setVisible(true);
String s=fd.getDirectory()+fd.getFile();
try{
fos=new FileOutputStream(s);
bw=new BufferedWriter(new OutputStreamWriter(fos));
String ss=ta.getText();
bw.write(ss);
bw.flush();
fos.flush();
fos.close();
bw.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
}}
);
MenuItem mi31=new MenuItem("另保存");
mi31.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
FileDialog fd=new FileDialog(f,"保存",FileDialog.SAVE);
fd.setVisible(true);
String s=fd.getDirectory()+fd.getFile();
try{
FileOutputStream fos=new FileOutputStream(s);
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(fos));
String ss=ta.getText();
bw.write(ss);
bw.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
});
MenuItem mi4=new MenuItem("退出");
mi4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
MenuItem mi5=new MenuItem("复制");
MenuItem mi6=new MenuItem("粘贴");
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
m1.add(mi31);
m1.add(mi4);
m2.add(mi5);
m2.add(mi6);
mb.add(m1);
mb.add(m2);
f.setMenuBar(mb);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
f.setVisible(true);
}
}
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息