java制作记事本代码 java简单记事本代码

java编写简单记事本

方法/步骤

成都创新互联是专业的迎泽网站建设公司,迎泽接单;提供成都网站设计、成都网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行迎泽网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

下载JDK,并安装成功,CMD测试Java环境安装成功。出现如下提示则Java环境安装成功

打开mycelipse进入主界面,点击左上角file--new---java project,新建一个Java项目,项目名字任意取,这里我取名为note--然后点击完成,创建Java项目

然后右键项目下的src--new---package,新建一个包,包名我定为notetest,然后点击完成

然后右键我们建立好的包notetest--new--class,包名为Note,点击完成

首先我们想导入工程所需要的包,以便编写程序时进行调用

package notepad;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.io.BufferedReader;

import java.io.BufferedWriter;

首先定义好编写程序时变量,我们设为成员变量方便以后调用

public class Notepad{

//屏幕分辨率

int width = Toolkit.getDefaultToolkit().getScreenSize().width;

int height = Toolkit.getDefaultToolkit().getScreenSize().height;

//窗体

private Frame frm ;

//显示内容多行文本

private TextArea notetext;

//记事本菜单

private MenuBar mb;

//菜单

private Menu m ;

//菜单项

private MenuItem open,save;

为程序创建组件--Frame--菜单栏目--文本框TextArea

public Notepad(){init();}

private void init(){

frm = new Frame("模拟记事本");

//设置菜单

mb = new MenuBar();

m = new Menu("文件");

//显示文件的地方

notetext = new TextArea();

notetext.setBounds(30, 50, 750, 500);

frm.add(notetext);

myEvent();

为控件和按钮添加监听事件

private void myEvent() {

//打开菜单的事件处理

open.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

//判断路径和文件名是否为空 如果为空结束程序

if(dir == null || name == null)

//如果不为空讲路径名和文件名封装到File对象里

file = new File(dir,name);

//使用字符缓冲读取文件

BufferedReader bfr = null;

try {

bfr = new BufferedReader(new FileReader(file));

});

save.addActionListener(new ActionListener(){

//获取路径和文件名

String dir =fd.getDirectory();

String name = fd.getFile();

//如果不为空讲路径名和文件名封装到File对象里

file = new File(dir,name);

}

BufferedWriter bfr= null;

}

});

}

}

然后创建主方法,开始测试程序,新建一个NoteDemo类

package notepad;

public class NoteDemo {

public static void main(String[] args) {

new Notepad();

}

}

用java编写记事本只要有打开 保存和退出功能就行,代码越简单越好,谢谢

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class JNotePadUI extends JFrame {

private JMenuItem menuOpen;

private JMenuItem menuSave;

private JMenuItem menuSaveAs;

private JMenuItem menuClose;

private JMenuItem menuChoose;

private JMenu editMenu;

private JMenuItem menuCut;

private JMenuItem menuCopy;

private JMenuItem menuPaste;

private JMenuItem menuAbout;

private JTextArea textArea;

private JLabel stateBar;

private JFileChooser fileChooser;

private JPopupMenu popUpMenu;

public JNotePadUI() {

super("新建文本文档-记事本");

setUpUIComponent();

setUpEventListener();

setVisible(true);

}

private void setUpUIComponent() {

setSize(640, 480);

// 菜单栏

JMenuBar menuBar = new JMenuBar();

// 设置「文件」菜单

JMenu fileMenu = new JMenu("文件");

menuOpen = new JMenuItem("打开");

// 快捷键设置

menuOpen.setAccelerator(

KeyStroke.getKeyStroke(

KeyEvent.VK_O,

InputEvent.CTRL_MASK));

menuSave = new JMenuItem("保存");

menuSave.setAccelerator(

KeyStroke.getKeyStroke(

KeyEvent.VK_S,

InputEvent.CTRL_MASK));

menuSaveAs = new JMenuItem("另存为");

menuChoose=new JMenuItem("全选");

menuChoose.setAccelerator(

KeyStroke.getKeyStroke(

KeyEvent.VK_A,

InputEvent.CTRL_MASK));

menuClose = new JMenuItem("关闭");

menuClose.setAccelerator(

KeyStroke.getKeyStroke(

KeyEvent.VK_Q,

InputEvent.CTRL_MASK));

fileMenu.add(menuOpen);

fileMenu.addSeparator(); // 分隔线

fileMenu.add(menuSave);

fileMenu.add(menuSaveAs);

fileMenu.addSeparator(); // 分隔线

fileMenu.add(menuChoose);

fileMenu.addSeparator();// 分隔线

fileMenu.add(menuClose);

// 设置「编辑」菜单

JMenu editMenu = new JMenu("编辑");

menuCut = new JMenuItem("剪切");

menuCut.setAccelerator(

KeyStroke.getKeyStroke(KeyEvent.VK_X,

InputEvent.CTRL_MASK));

menuCopy = new JMenuItem("复制");

menuCopy.setAccelerator(

KeyStroke.getKeyStroke(KeyEvent.VK_C,

InputEvent.CTRL_MASK));

menuPaste = new JMenuItem("粘贴");

menuPaste.setAccelerator(

KeyStroke.getKeyStroke(KeyEvent.VK_V,

InputEvent.CTRL_MASK));

editMenu.add(menuCut);

editMenu.add(menuCopy);

editMenu.add(menuPaste);

// 设置「关于」菜单

JMenu aboutMenu = new JMenu("关于");

menuAbout = new JMenuItem("关于记事本");

aboutMenu.add(menuAbout);

menuBar.add(fileMenu);

menuBar.add(editMenu);

menuBar.add(aboutMenu);

setJMenuBar(menuBar);

// 文字编辑区域

textArea = new JTextArea();

textArea.setFont(new Font("宋体", Font.PLAIN, 16));

textArea.setLineWrap(true);

JScrollPane panel = new JScrollPane(textArea,

ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,

ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

Container contentPane = getContentPane();

contentPane.add(panel, BorderLayout.CENTER);

// 状态栏

stateBar = new JLabel("未修改");

stateBar.setHorizontalAlignment(SwingConstants.LEFT);

stateBar.setBorder(

BorderFactory.createEtchedBorder());

contentPane.add(stateBar, BorderLayout.SOUTH);

popUpMenu = editMenu.getPopupMenu();

fileChooser = new JFileChooser();

}

private void setUpEventListener() {

// 按下窗口关闭钮事件处理

addWindowListener(

new WindowAdapter() {

public void windowClosing(WindowEvent e) {

closeFile();

}

}

);

// 菜单 - 打开

menuOpen.addActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent e) {

openFile();

}

}

);

// 菜单 - 保存

menuSave.addActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent e) {

saveFile();

}

}

);

// 菜单 - 另存为

menuSaveAs.addActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent e) {

saveFileAs();

}

}

);

// 菜单 -关闭

menuClose.addActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent e) {

closeFile();

}

}

);

// 菜单 - 剪切

menuCut.addActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent e) {

cut();

}

}

);

// 菜单 - 复制

menuCopy.addActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent e) {

copy();

}

}

);

// 菜单 - 粘贴

menuPaste.addActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent e) {

paste();

}

}

);

// 菜单 - 关于

menuAbout.addActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent e) {

// 显示对话框

JOptionPane.showOptionDialog(null,

"程序名称:\n JNotePad \n" +

"程序设计:\n ???\n" +

"简介:\n 一个简单的文字编辑器\n",

"关于JNotePad",

JOptionPane.DEFAULT_OPTION,

JOptionPane.INFORMATION_MESSAGE,

null, null, null);

}

}

);

// 编辑区键盘事件

textArea.addKeyListener(

new KeyAdapter() {

public void keyTyped(KeyEvent e) {

processTextArea();

}

}

);

// 编辑区鼠标事件

textArea.addMouseListener(

new MouseAdapter() {

public void mouseReleased(MouseEvent e) {

if(e.getButton() == MouseEvent.BUTTON3)

popUpMenu.show(editMenu, e.getX(), e.getY());

}

public void mouseClicked(MouseEvent e) {

if(e.getButton() == MouseEvent.BUTTON1)

popUpMenu.setVisible(false);

}

}

);

}

private void openFile() {

if(isCurrentFileSaved()) { // 文件是否为保存状态

open(); // 打开

}

else {

// 显示对话框

int option = JOptionPane.showConfirmDialog(

null, "文件已修改,是否保存?",

"保存文件?", JOptionPane.YES_NO_OPTION,

JOptionPane.WARNING_MESSAGE, null);

switch(option) {

// 确认文件保存

case JOptionPane.YES_OPTION:

saveFile(); // 保存文件

break;

// 放弃文件保存

case JOptionPane.NO_OPTION:

open();

break;

}

}

}

private boolean isCurrentFileSaved() {

if(stateBar.getText().equals("未修改")) {

return true;

}

else {

return false;

}

}

private void open() {

// fileChooser 是 JFileChooser 的实例

// 显示文件选取的对话框

int option = fileChooser.showDialog(null, null);

// 使用者按下确认键

if(option == JFileChooser.APPROVE_OPTION) {

/*

TODO: 添加读取文件的代码

*/

}

}

private void saveFile() {

/*

TODO: 添加保存文件的代码

*/

}

private void saveFileAs() {

/*

TODO: 添加另存为的代码

*/

}

private void closeFile() {

// 是否保存文件

if(isCurrentFileSaved()) {

// 释放窗口资源,而后关闭程序

dispose();

}

else {

int option = JOptionPane.showConfirmDialog(

null, "文件已修改,是否保存?",

"保存文件?", JOptionPane.YES_NO_OPTION,

JOptionPane.WARNING_MESSAGE, null);

switch(option) {

case JOptionPane.YES_OPTION:

saveFile();

break;

case JOptionPane.NO_OPTION:

dispose();

}

}

}

private void cut() {

textArea.cut();

stateBar.setText("已修改");

popUpMenu.setVisible(false);

}

private void copy() {

textArea.copy();

popUpMenu.setVisible(false);

}

private void paste() {

textArea.paste();

stateBar.setText("已修改");

popUpMenu.setVisible(false);

}

private void processTextArea() {

stateBar.setText("已修改");

}

public static void main(String[] args) {

new JNotePadUI();

}

用Java编写简易记事本源代码

importjava.awt.BorderLayout;importjava.awt.Container;importjava.awt.Font;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.InputEvent;importjava.awt.event.KeyAdapter;importjava.awt.event.KeyEvent;importjava.awt.event.MouseAdapter;importjava.awt.event.MouseEvent;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;importjavax.swing.BorderFactory;importjavax.swing.JFileChooser;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JMenu;importjavax.swing.JMenuBar;importjavax.swing.JMenuItem;importjavax.swing.JOptionPane;importjavax.swing.JPopupMenu;importjavax.swing.JScrollPane;importjavax.swing.JTextArea;importjavax.swing.KeyStroke;importjavax.swing.ScrollPaneConstants;importjavax.swing.SwingConstants;publicclassJNotePadUIextendsJFrame{privateJMenuItemmenuOpen;privateJMenuItemmenuSave;privateJMenuItemmenuSaveAs;privateJMenuItemmenuClose;privateJMenueditMenu;privateJMenuItemmenuCut;privateJMenuItemmenuCopy;privateJMenuItemmenuPaste;privateJMenuItemmenuAbout;privateJTextAreatextArea;privateJLabelstateBar;privateJFileChooserfileChooser;privateJPopupMenupopUpMenu;publicJNotePadUI(){super("新建文本文件");setUpUIComponent();setUpEventListener();setVisible(true);}privatevoidsetUpUIComponent(){setSize(640,480);//菜单栏JMenuBarmenuBar=newJMenuBar();//设置「文件」菜单JMenufileMenu=newJMenu("文件");menuOpen=newJMenuItem("打开");//快捷键设置menuOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));menuSave=newJMenuItem("保存");menuSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));menuSaveAs=newJMenuItem("另存为");menuClose=newJMenuItem("关闭");menuClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,InputEvent.CTRL_MASK));fileMenu.add(menuOpen);fileMenu.addSeparator();//分隔线fileMenu.add(menuSave);fileMenu.add(menuSaveAs);fileMenu.addSeparator();//分隔线fileMenu.add(menuClose);//设置「编辑」菜单JMenueditMenu=newJMenu("编辑");menuCut=newJMenuItem("剪切");menuCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));menuCopy=newJMenuItem("复制");menuCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));menuPaste=newJMenuItem("粘贴");menuPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));editMenu.add(menuCut);editMenu.add(menuCopy);editMenu.add(menuPaste);//设置「关于」菜单JMenuaboutMenu=newJMenu("关于");menuAbout=newJMenuItem("关于JNotePad");aboutMenu.add(menuAbout);menuBar.add(fileMenu);menuBar.add(editMenu);menuBar.add(aboutMenu);setJMenuBar(menuBar);//文字编辑区域textArea=newJTextArea();textArea.setFont(newFont("宋体",Font.PLAIN,16));textArea.setLineWrap(true);JScrollPanepanel=newJScrollPane(textArea,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);ContainercontentPane=getContentPane();contentPane.add(panel,BorderLayout.CENTER);//状态栏stateBar=newJLabel("未修改");stateBar.setHorizontalAlignment(SwingConstants.LEFT);stateBar.setBorder(BorderFactory.createEtchedBorder());contentPane.add(stateBar,BorderLayout.SOUTH);popUpMenu=editMenu.getPopupMenu();fileChooser=newJFileChooser();}privatevoidsetUpEventListener(){//按下窗口关闭钮事件处理addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){closeFile();}});//菜单-打开menuOpen.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){openFile();}});//菜单-保存menuSave.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){saveFile();}});//菜单-另存为menuSaveAs.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){saveFileAs();}});//菜单-关闭文件menuClose.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){closeFile();}});//菜单-剪切menuCut.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){cut();}});//菜单-复制menuCopy.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){copy();}});//菜单-粘贴menuPaste.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){paste();}});//菜单-关于menuAbout.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){//显示对话框JOptionPane.showOptionDialog(null,"程序名称:\nJNotePad\n"+"程序设计:\n\n"+"简介:\n一个简单的文字编辑器\n"+"可作为验收Java的实现对象\n"+"欢迎网友下载研究交流\n\n"+"/","关于JNotePad",JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE,null,null,null);}});//编辑区键盘事件textArea.addKeyListener(newKeyAdapter(){publicvoidkeyTyped(KeyEvente){processTextArea();}});//编辑区鼠标事件textArea.addMouseListener(newMouseAdapter(){publicvoidmouseReleased(MouseEvente){if(e.getButton()==MouseEvent.BUTTON3)popUpMenu.show(editMenu,e.getX(),e.getY());}publicvoidmouseClicked(MouseEvente){if(e.getButton()==MouseEvent.BUTTON1)popUpMenu.setVisible(false);}});}privatevoidopenFile(){if(isCurrentFileSaved()){//文件是否为保存状态open();//打开}else{//显示对话框intoption=JOptionPane.showConfirmDialog(null,"文件已修改,是否保存?","保存文件?",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null);switch(option){//确认文件保存caseJOptionPane.YES_OPTION:saveFile();//保存文件break;//放弃文件保存caseJOptionPane.NO_OPTION:open();break;}}}privatebooleanisCurrentFileSaved(){if(stateBar.getText().equals("未修改")){returnfalse;}else{returntrue;}}privatevoidopen(){//fileChooser是JFileChooser的实例//显示文件选取的对话框intoption=fileChooser.showDialog(null,null);//使用者按下确认键if(option==JFileChooser.APPROVE_OPTION){try{//开启选取的文件BufferedReaderbuf=newBufferedReader(newFileReader(fileChooser.getSelectedFile()));//设定文件标题setTitle(fileChooser.getSelectedFile().toString());//清除前一次文件textArea.setText("");//设定状态栏stateBar.setText("未修改");//取得系统相依的换行字符StringlineSeparator=System.getProperty("line.separator");//读取文件并附加至文字编辑区Stringtext;while((text=buf.readLine())!=null){textArea.append(text);textArea.append(lineSeparator);}buf.close();}catch(IOExceptione){JOptionPane.showMessageDialog(null,e.toString(),"开启文件失败",JOptionPane.ERROR_MESSAGE);}}}privatevoidsaveFile(){//从标题栏取得文件名称Filefile=newFile(getTitle());//若指定的文件不存在if(!file.exists()){//执行另存为saveFileAs();}else{try{//开启指定的文件BufferedWriterbuf=newBufferedWriter(newFileWriter(file));//将文字编辑区的文字写入文件buf.write(textArea.getText());buf.close();//设定状态栏为未修改stateBar.setText("未修改");}catch(IOExceptione){JOptionPane.showMessageDialog(null,e.toString(),"写入文件失败",JOptionPane.ERROR_MESSAGE);}}}privatevoidsaveFileAs(){//显示文件对话框intoption=fileChooser.showSaveDialog(null);//如果确认选取文件if(option==JFileChooser.APPROVE_OPTION){//取得选择的文件Filefile=fileChooser.getSelectedFile();//在标题栏上设定文件名称setTitle(file.toString());try{//建立文件file.createNewFile();//进行文件保存saveFile();}catch(IOExceptione){JOptionPane.showMessageDialog(null,e.toString(),"无法建立新文件",JOptionPane.ERROR_MESSAGE);}}}privatevoidcloseFile(){//是否已保存文件if(isCurrentFileSaved()){//释放窗口资源,而后关闭程序dispose();}else{intoption=JOptionPane.showConfirmDialog(null,"文件已修改,是否保存?","保存文件?",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null);switch(option){caseJOptionPane.YES_OPTION:saveFile();break;caseJOptionPane.NO_OPTION:dispose();}}}privatevoidcut(){textArea.cut();stateBar.setText("已修改");popUpMenu.setVisible(false);}privatevoidcopy(){textArea.copy();popUpMenu.setVisible(false);}privatevoidpaste(){textArea.paste();stateBar.setText("已修改");popUpMenu.setVisible(false);}privatevoidprocessTextArea(){stateBar.setText("已修改");}publicstaticvoidmain(String[]args){newJNotePadUI();}}

用java编写记事本的代码是什么

楼主应该是想编译记事本的写的java代码吧?

1.从sun.com上免费下载jdk5.0

2.安装jdk

3.配置环境变量,在"控制面版"的"系统"的"高级"标签,

在"path="键上添加你的jdk/bin路径;

4.添加"java_home"键,值等于jdk路径;

配置就ok了.

5.测试在cmd命令下输入"java -version"回车;

就会显示出你的jdk版本,否则就照以上步骤从来;

6.编译%--在你的"FileName.java"的文件夹里,(当然是在cmd命令下即dos模式)输入"javac FileName.java"回车,就完完成了编译.

例如:你的文件是Hello.java

就输入 javac Hello.java

7.运行文件当你成功编译后,输入"java FileName"就可以得到结果.

例如:java Hello

不推荐这样做。编程和编译其实用工具实现起来很容易。比如

netbeans,eclips==都是免费的强大编译软件!推荐使用!

如何用JAVA编写简单的记事本程序?

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import java.io.*;

import java.awt.datatransfer.*;public class NewEdit

{

public static void main(String args[])

{

MyFrame EditFrame=new MyFrame();

EditFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

EditFrame.setVisible(true);

}

}class MyFrame extends JFrame

{

public MyFrame()

{ Dimension screenSize=toolKit.getScreenSize();

int screenHeight=screenSize.height;

int screenWidth=screenSize.width;

setSize(screenWidth/2,screenHeight/2);

setLocation(screenWidth/4,screenHeight/4);

Image img=toolKit.getImage("icon.gif");

setIconImage(img);

setTitle("MyEdit");

JMenuBar menuBar=new JMenuBar();

setJMenuBar(menuBar);

JMenu fileMenu=new JMenu("文件");

JMenu editMenu=new JMenu("编辑");

JMenu helpMenu=new JMenu("帮助");

menuBar.add(fileMenu);

menuBar.add(editMenu);

menuBar.add(helpMenu);

JMenuItem newItem=new JMenuItem("新建");

newItem.addActionListener(new NewListener());

JMenuItem openItem=new JMenuItem("打开");

openItem.addActionListener(new OpenListener());

JMenuItem saveItem=new JMenuItem("保存");

saveItem.addActionListener(new SaveListener());

JMenuItem escItem=new JMenuItem("退出");

escItem.addActionListener(new

ActionListener()

{

public void actionPerformed(ActionEvent myE)

{

System.exit(0);

}

});

fileMenu.add(newItem);

fileMenu.add(openItem);

fileMenu.add(saveItem);

fileMenu.add(escItem);

JMenuItem allItem=new JMenuItem("全选");

//JMenuItem copyItem=new JMenuItem("复制");

//CopyAction copyAction=new CopyAction();

//copyItem.addActionListener(copyAction);

JMenuItem cutItem=new JMenuItem("剪切");

//JMenuItem pasteItem=new JMenuItem("粘贴");

//Action pasteAction=new PasteAction();

//pasteItem.addActionListener(pasteAction);

editMenu.add(allItem);

allItem.addActionListener(new

ActionListener()

{

public void actionPerformed(ActionEvent myE)

{

textArea.selectAll();

}

});

CopyAction copyAction=new CopyAction("剪切");

editMenu.add(copyAction);

CutAction cutAction=new CutAction("复制");

editMenu.add(cutAction);

Action pasteAction=new PasteAction("粘贴");

editMenu.add(pasteAction);

popup=new JPopupMenu();

popup.add(copyAction);

popup.add(pasteAction);

popup.add(cutAction);

textArea = new JTextArea();

textArea.add(popup);

textArea.addMouseListener(new

MouseListener(){

public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {

if (e.getButton() == MouseEvent.BUTTON3)

{

popup.show(textArea, e.getX(), e.getY());

}

} public void mouseReleased(MouseEvent e) {} });//可尝试用MouseAdapter,代码可能更简单

JScrollPane scroller = new JScrollPane(textArea);

add(scroller);

//OR--this.getContentPane().add(scroller)

//scroller.setComponentPopupMenu(popup);

//textArea.setInheritsPopupMenu(true);

//scroller.addMouseListener(new MouseAdapter(){});

}

public void writeFile(String fileName)

{

try

{

File file = new File(fileName);

FileWriter write = new FileWriter(file);

write.write(textArea.getText());

write.close();

}

catch(Exception e){

System.out.println("Error closing file!");

}

}

public void openFile(String fileName)

{

try

{

File file = new File(fileName);

FileReader readIn = new FileReader(file);

int size = (int)file.length();

int charsRead = 0;

char[] content = new char[size];

while(readIn.ready())

charsRead += readIn.read(content,charsRead,size-charsRead);

readIn.close();

textArea.setText(new String(content,0,charsRead));

}

catch(IOException e)

{

System.out.println("Error opening file!");

}

}

private class NewListener implements ActionListener

{

public void actionPerformed(ActionEvent myE)

{

textArea.setText("");

}

}

private class OpenListener implements ActionListener

{

public void actionPerformed(ActionEvent myE)

{

openFileDialog.setVisible(true);

fileName = openFileDialog.getDirectory()+openFileDialog.getFile();

if(fileName != null)

{

openFile(fileName);

}

}

}

private class SaveListener implements ActionListener

{

public void actionPerformed(ActionEvent myE)

{

saveFileDialog.setVisible(true);

fileName = saveFileDialog.getDirectory()+saveFileDialog.getFile();

if(fileName !=null)

{

writeFile(fileName);

}

}

}

private class CutAction extends AbstractAction

{

public CutAction(String name)

{

super(name);

}

public void actionPerformed(ActionEvent event)

{

String text = textArea.getSelectedText();

StringSelection selection = new StringSelection(text);

clipboard.setContents(selection,null);

textArea.replaceRange("",textArea.getSelectionStart(),

textArea.getSelectionEnd());

}

}

private class CopyAction extends AbstractAction

{

public CopyAction(String name)

{

super(name);

}

public void actionPerformed(ActionEvent event)

{

String text = textArea.getSelectedText();

StringSelection selection= new StringSelection(text);

clipboard.setContents(selection,null);

}

}

private class PasteAction extends AbstractAction

{

public PasteAction(String name)

{

super(name);

}

public void actionPerformed(ActionEvent event)

{

Transferable contents = clipboard.getContents(this);

if(contents==null)

return;

String text;

text="";

try

{

text = (String)contents.getTransferData(DataFlavor.stringFlavor);

}

catch(Exception ex){}

textArea.replaceRange(text,

textArea.getSelectionStart(),textArea.getSelectionEnd());

}

}

private JTextArea textArea;

private JPopupMenu popup;

private String fileName="";

private FileDialog openFileDialog=new FileDialog(this,"OpenFile",FileDialog.LOAD); private FileDialog saveFileDialog=new FileDialog(this,"SaveFile",FileDialog.SAVE);

Toolkit toolKit = Toolkit.getDefaultToolkit();

private Clipboard clipboard = toolKit.getSystemClipboard();}


新闻名称:java制作记事本代码 java简单记事本代码
网页网址:http://hbruida.cn/article/hhogcg.html