日期:2014-05-20  浏览次数:20652 次

求救!急(在线等)
用java写个applicacation,
一个文本输入框,输入文本后保存到本地的.txt。
一个文本输入框,一个保存选项.

急!谢谢大家了!

------解决方案--------------------
给你一个简单的记事本程序
希望能帮到你


/*
* NotePad.java
*
* Created on 2003年11月20日, 下午10:31
*/

/**
*
* @author Java2_XM.K
*/
import java.io.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
public class NotePad extends Frame implements ActionListener
{
TextArea textarea=new TextArea();
MenuBar menubar=new MenuBar();
Menu filemenu=new Menu( "File ");
MenuItem newitem=new MenuItem( "New ");
MenuItem openitem=new MenuItem( "Open ");
MenuItem saveitem=new MenuItem( "Save ");
MenuItem saveasitem=new MenuItem( "Save As ");
MenuItem exititem=new MenuItem( "Exit ");
Menu editmenu=new Menu( "Edit ");
MenuItem selectitem=new MenuItem( "Select All ");
MenuItem copyitem=new MenuItem( "Copy ");
MenuItem cutitem=new MenuItem( "Cut ");
MenuItem pasteitem=new MenuItem( "Paste ");
Menu aboutmenu=new Menu( "About ");
MenuItem info=new MenuItem( "About NotePad ");
String fileName= "NoName ";//设置默认的文件名
Toolkit toolkit=Toolkit.getDefaultToolkit();
Clipboard clipboard=toolkit.getSystemClipboard();//剪贴板对象
private FileDialog openFileDialog=new FileDialog(this, "Open File ",FileDialog.LOAD);
private FileDialog saveAsFileDialog=new FileDialog(this, "Save File As ",FileDialog.SAVE);
public NotePad()
{
setTitle( "NotePad ");
setFont(new Font( "Times New Roman ",Font.PLAIN,12));
setBackground(Color.white);
setSize(400,300);
filemenu.add(newitem);
filemenu.add(openitem);
filemenu.addSeparator();
filemenu.add(saveitem);
filemenu.add(saveasitem);
filemenu.addSeparator();
filemenu.add(exititem);
editmenu.add(selectitem);
editmenu.add(copyitem);
editmenu.addSeparator();
editmenu.add(cutitem);
editmenu.add(pasteitem);
menubar.add(filemenu);
menubar.add(editmenu);
menubar.add(aboutmenu);
aboutmenu.add(info);
setMenuBar(menubar);
add(textarea);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
newitem.addActionListener(this);
openitem.addActionListener(this);
saveitem.addActionListener(this);
saveasitem.addActionListener(this);
exititem.addActionListener(this);
selectitem.addActionListener(this);
copyitem.addActionListener(this);
cutitem.addActionListener(this);
pasteitem.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Object eventSource=e.getSource();//利用事件源判断被单击的菜单项
if(eventSource==newitem)
{
textarea.setText( " ");
}
else if(eventSource==openitem)
{
openFileDialog.show();
fileName=openFileDialog.getDirectory()+openFileDialog.getFile();
if(fileName!=null)
readFile(fileName);
}
else if(eventSource==saveitem)
{
if(fileName!=null)
writeFile(fileName);
}
else if(eventSource==saveasitem)
{
saveAsFileDialog.show();
fileName=saveAsFileDialog.getDirectory()+saveAsFileDialog.getFile();
if(fileName!=null)
writeFile(fileName);
}
else if(eventSource==selectitem)
{
textarea.selectAll();
}
else if(eventSource==cutitem)
{
String text=textarea.getSelectedText();
StringSelection selection=new StringSelection(text);