设计一个别java程序模仿记事本的部分功能
1个回答
展开全部
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class MyNotepad implements ActionListener{
//声明所需组件
Frame f = new Frame("记事本 作者:刘欣 班级:java5");
MenuBar menubar;
Menu menufile;
MenuItem menuitemopen,menuitemsave,menuitemsaveas,menuitemclose;
TextArea textarea;
String filename = "";
FileDialog d1,d2;
public MyNotepad(){
}
void run(){
//设置大小并注册监听器
f.setSize(400,400);
f.addWindowListener(new WindowHander());
//实例化组件并添加至容器中,同时添加适配器
menubar = new MenuBar();
f.setMenuBar(menubar);
menufile = new Menu("文件");
menubar.add(menufile);
menuitemopen = new MenuItem("打开");
menufile.add(menuitemopen);
menuitemopen.addActionListener(this);
menuitemsave = new MenuItem("保存");
menufile.add(menuitemsave);
menuitemsave.addActionListener(this);
menuitemsaveas = new MenuItem("另存为");
menufile.add(menuitemsaveas);
menuitemsaveas.addActionListener(this);
menuitemclose = new MenuItem("关闭");
menufile.add(menuitemclose);
menuitemclose.addActionListener(this);
textarea = new TextArea();
f.add(textarea);
f.setVisible(true);
d1 = new FileDialog(f,"打开",FileDialog.LOAD);
d2 = new FileDialog(f,"另存为",FileDialog.SAVE);
}
public static void main(String[] args){
MyNotepad m = new MyNotepad();
m.run();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==menuitemclose){
System.exit(1);
}else
if(e.getSource()==menuitemopen){
textarea.setText("");
d1.setVisible(true);
filename = d1.getDirectory()+d1.getFile();
if(filename.endsWith(".txt"))
{
read(filename);
}
else{
textarea.append("不支持此类型文件");
}
}else
if(e.getSource()==menuitemsaveas){
d2.setVisible(true);
filename = d2.getDirectory()+d2.getFile();
write(filename);
}else if(e.getSource()==menuitemsave){
if(filename==""){
d2.setVisible(true);
filename = d2.getDirectory()+d2.getFile();
write(filename);
}else{
write(filename);
}
}
}
void write(String fileneme){
try{
File file1 = new File(filename);
FileWriter fw = new FileWriter(file1);
fw.write(textarea.getText());
fw.close();
}catch(IOException e){
System.out.println(e);
}
}
void read(String filename){
try{
File file2 = new File(filename);
FileReader fr = new FileReader(file2);
int length = (int)file2.length();
char[] cbuf = new char[length];
int r = 0;
while(fr.ready()){
r += fr.read(cbuf,r,length-r);
}
fr.close();
textarea.setText(new String(cbuf,0,r));
}catch(IOException e){
System.out.println(e);
}
}
}
class WindowHander extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
实现了打开保存新建另存为功能
import java.awt.event.*;
import java.io.*;
public class MyNotepad implements ActionListener{
//声明所需组件
Frame f = new Frame("记事本 作者:刘欣 班级:java5");
MenuBar menubar;
Menu menufile;
MenuItem menuitemopen,menuitemsave,menuitemsaveas,menuitemclose;
TextArea textarea;
String filename = "";
FileDialog d1,d2;
public MyNotepad(){
}
void run(){
//设置大小并注册监听器
f.setSize(400,400);
f.addWindowListener(new WindowHander());
//实例化组件并添加至容器中,同时添加适配器
menubar = new MenuBar();
f.setMenuBar(menubar);
menufile = new Menu("文件");
menubar.add(menufile);
menuitemopen = new MenuItem("打开");
menufile.add(menuitemopen);
menuitemopen.addActionListener(this);
menuitemsave = new MenuItem("保存");
menufile.add(menuitemsave);
menuitemsave.addActionListener(this);
menuitemsaveas = new MenuItem("另存为");
menufile.add(menuitemsaveas);
menuitemsaveas.addActionListener(this);
menuitemclose = new MenuItem("关闭");
menufile.add(menuitemclose);
menuitemclose.addActionListener(this);
textarea = new TextArea();
f.add(textarea);
f.setVisible(true);
d1 = new FileDialog(f,"打开",FileDialog.LOAD);
d2 = new FileDialog(f,"另存为",FileDialog.SAVE);
}
public static void main(String[] args){
MyNotepad m = new MyNotepad();
m.run();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==menuitemclose){
System.exit(1);
}else
if(e.getSource()==menuitemopen){
textarea.setText("");
d1.setVisible(true);
filename = d1.getDirectory()+d1.getFile();
if(filename.endsWith(".txt"))
{
read(filename);
}
else{
textarea.append("不支持此类型文件");
}
}else
if(e.getSource()==menuitemsaveas){
d2.setVisible(true);
filename = d2.getDirectory()+d2.getFile();
write(filename);
}else if(e.getSource()==menuitemsave){
if(filename==""){
d2.setVisible(true);
filename = d2.getDirectory()+d2.getFile();
write(filename);
}else{
write(filename);
}
}
}
void write(String fileneme){
try{
File file1 = new File(filename);
FileWriter fw = new FileWriter(file1);
fw.write(textarea.getText());
fw.close();
}catch(IOException e){
System.out.println(e);
}
}
void read(String filename){
try{
File file2 = new File(filename);
FileReader fr = new FileReader(file2);
int length = (int)file2.length();
char[] cbuf = new char[length];
int r = 0;
while(fr.ready()){
r += fr.read(cbuf,r,length-r);
}
fr.close();
textarea.setText(new String(cbuf,0,r));
}catch(IOException e){
System.out.println(e);
}
}
}
class WindowHander extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
实现了打开保存新建另存为功能
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询