帮忙在JAVA程序写字板添加保存,另存为等其他功能
importjava.awt.*;importjava.awt.event.*;publicclassMenuDemoextendsFrameimplementsActi...
import java.awt.*;
import java.awt.event.*;
public class MenuDemo extends Frame implements ActionListener
{
TextArea tf=new TextArea();
MenuBar bar=new MenuBar();
Menu menu=new Menu("文件");
MenuItem newf=new MenuItem("新建");
MenuItem open=new MenuItem("打开");
MenuItem close=new MenuItem("关闭");
MenuItem quit=new MenuItem("退出");
public MenuDemo()
{
super("MenuDemo");
setMenuBar(bar);
menu.add(newf);
menu.add(open);
menu.add(close);
menu.addSeparator();
menu.add(quit);
bar.add(menu);
newf.addActionListener(this);
open.addActionListener(this);
close.addActionListener(this);
quit.addActionListener(this);
addWindowListener(new WinLis());
add(tf);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==newf) tf.setText("新建");
if(e.getSource()==open) tf.setText("打开");
if(e.getSource()==close) tf.setText("关闭");
if(e.getSource()==quit) System.exit(0);
}
class WinLis extends WindowAdapter
{
public void windowCosing(WindowEvent e)
{
System.exit(0);
}
}
public static void main(String[] args)
{
Frame f=new MenuDemo();
f.setSize(400,200);
f.setVisible(true);
}
} 展开
import java.awt.event.*;
public class MenuDemo extends Frame implements ActionListener
{
TextArea tf=new TextArea();
MenuBar bar=new MenuBar();
Menu menu=new Menu("文件");
MenuItem newf=new MenuItem("新建");
MenuItem open=new MenuItem("打开");
MenuItem close=new MenuItem("关闭");
MenuItem quit=new MenuItem("退出");
public MenuDemo()
{
super("MenuDemo");
setMenuBar(bar);
menu.add(newf);
menu.add(open);
menu.add(close);
menu.addSeparator();
menu.add(quit);
bar.add(menu);
newf.addActionListener(this);
open.addActionListener(this);
close.addActionListener(this);
quit.addActionListener(this);
addWindowListener(new WinLis());
add(tf);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==newf) tf.setText("新建");
if(e.getSource()==open) tf.setText("打开");
if(e.getSource()==close) tf.setText("关闭");
if(e.getSource()==quit) System.exit(0);
}
class WinLis extends WindowAdapter
{
public void windowCosing(WindowEvent e)
{
System.exit(0);
}
}
public static void main(String[] args)
{
Frame f=new MenuDemo();
f.setSize(400,200);
f.setVisible(true);
}
} 展开
2个回答
推荐于2016-07-12
展开全部
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JFrame;
import wyq.study.io.FileLister;
public class MenuDemo extends Frame implements ActionListener
{
TextArea tf = new TextArea();
MenuBar bar = new MenuBar();
Menu menu = new Menu("文件");
MenuItem newf = new MenuItem("新建");
MenuItem open = new MenuItem("打开");
MenuItem saveFile = new MenuItem("保存");
MenuItem saveFileAs = new MenuItem("另存为");
MenuItem close = new MenuItem("关闭");
MenuItem quit = new MenuItem("退出");
public MenuDemo()
{
super("MenuDemo");
setMenuBar(bar);
menu.add(newf);
menu.add(open);
menu.add(close);
menu.add(saveFile);
menu.add(saveFileAs);
menu.addSeparator();
menu.add(quit);
bar.add(menu);
newf.addActionListener(this);
open.addActionListener(this);
close.addActionListener(this);
quit.addActionListener(this);
saveFile.addActionListener(this);
saveFileAs.addActionListener(this);
//addWindowListener(new WinLis());
//关闭窗口
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}
});
add(tf);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == newf)
{
tf.setText("新建");
}
if (e.getSource() == open)
{
tf.setText("打开");
}
if (e.getSource() == close)
{
tf.setText("关闭");
}
if (e.getSource() ==saveFile)
{
tf.setText("保存");
saveFile();
}
if (e.getSource() == saveFileAs)
{
tf.setText("另存为");
saveFileAs();
}
if (e.getSource() == quit)
{
System.exit(0);
}
}
public void saveFile()
{
String directory = System.getProperty("user.dir");
FileDialog f = new FileDialog(this, "Save File", FileDialog.SAVE);
f.setDirectory(directory);
f.show();
String fileName =f.getFile(); // 得到保存的文件名
directory=f.getDirectory();//得到文件路径
//System.out.println(directory+" "+file);
String txt=tf.getText();
File file=new File(directory+fileName);
OutputStream out=null;
try
{
out=new FileOutputStream(file);
}
catch (FileNotFoundException e)
{
}
try
{
out.write(txt.getBytes());
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
if(out!=null)
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void saveFileAs()
{
saveFile();
}
class WinLis extends WindowAdapter
{
public void windowCosing(WindowEvent e)
{
System.exit(0);
}
}
public static void main(String[] args)
{
Frame f = new MenuDemo();
f.setSize(400, 200);
f.setVisible(true);
}
}
import java.awt.event.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JFrame;
import wyq.study.io.FileLister;
public class MenuDemo extends Frame implements ActionListener
{
TextArea tf = new TextArea();
MenuBar bar = new MenuBar();
Menu menu = new Menu("文件");
MenuItem newf = new MenuItem("新建");
MenuItem open = new MenuItem("打开");
MenuItem saveFile = new MenuItem("保存");
MenuItem saveFileAs = new MenuItem("另存为");
MenuItem close = new MenuItem("关闭");
MenuItem quit = new MenuItem("退出");
public MenuDemo()
{
super("MenuDemo");
setMenuBar(bar);
menu.add(newf);
menu.add(open);
menu.add(close);
menu.add(saveFile);
menu.add(saveFileAs);
menu.addSeparator();
menu.add(quit);
bar.add(menu);
newf.addActionListener(this);
open.addActionListener(this);
close.addActionListener(this);
quit.addActionListener(this);
saveFile.addActionListener(this);
saveFileAs.addActionListener(this);
//addWindowListener(new WinLis());
//关闭窗口
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}
});
add(tf);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == newf)
{
tf.setText("新建");
}
if (e.getSource() == open)
{
tf.setText("打开");
}
if (e.getSource() == close)
{
tf.setText("关闭");
}
if (e.getSource() ==saveFile)
{
tf.setText("保存");
saveFile();
}
if (e.getSource() == saveFileAs)
{
tf.setText("另存为");
saveFileAs();
}
if (e.getSource() == quit)
{
System.exit(0);
}
}
public void saveFile()
{
String directory = System.getProperty("user.dir");
FileDialog f = new FileDialog(this, "Save File", FileDialog.SAVE);
f.setDirectory(directory);
f.show();
String fileName =f.getFile(); // 得到保存的文件名
directory=f.getDirectory();//得到文件路径
//System.out.println(directory+" "+file);
String txt=tf.getText();
File file=new File(directory+fileName);
OutputStream out=null;
try
{
out=new FileOutputStream(file);
}
catch (FileNotFoundException e)
{
}
try
{
out.write(txt.getBytes());
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
if(out!=null)
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void saveFileAs()
{
saveFile();
}
class WinLis extends WindowAdapter
{
public void windowCosing(WindowEvent e)
{
System.exit(0);
}
}
public static void main(String[] args)
{
Frame f = new MenuDemo();
f.setSize(400, 200);
f.setVisible(true);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你都写了这么多相信百度,是很容易搞定余下的代码的
追问
这也是根据老师的改的,,我搞不定啊。。。。呜呜呜
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询