资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

用Java代码做记事本 JAVA记事本代码

《求助》怎样用JAVA做记事本

很久之前写过的,凑合着看吧,

创新互联-云计算及IDC服务提供商,涵盖公有云、IDC机房租用、达州托管服务器、等保安全、私有云建设等企业级互联网基础服务,欢迎联系:028-86922220

package 个人练习;

import java.awt.FileDialog;

import java.awt.Menu;

import java.awt.MenuBar;

import java.awt.MenuItem;

import java.awt.TextArea;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.awt.event.WindowListener;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.io.PrintWriter;

import java.io.Writer;

import java.sql.Savepoint;

import javax.swing.JFrame;

/**

* 创建一个类似记事本的程序

* 该程序拥有自己的书写窗口

* 可以自定义保存地址

* @author Administrator

*

*/

/*

* 创建记事本类

* 实现ActionListener接口

* 重写它的actionPerformed方法

*/

public class JiShiBen implements ActionListener {

//拥有自己的窗口

JFrame frame=new JFrame("记事本程序");

//菜单栏

MenuBar menubar=new MenuBar();

//创建下拉菜单

Menu wenjian=new Menu("文件");

//创建保存按钮

MenuItem dakai=new MenuItem("打开文件");

MenuItem baochun=new MenuItem("保存");

MenuItem xinjian=new MenuItem("新建");

//创建可以显示文本的多行区域

TextArea wenben=new TextArea();

//显示一个对话窗口,用户选择一个文件,如果模式是SAVE,则寻找一个地方去写入一个文件

FileDialog fd;

File file=null;

/*

 * 创建jishiben的构造函数

 */

public  JiShiBen(){

frame.setMenuBar(menubar);//将菜单栏关联到窗口

menubar.add(wenjian);//将下拉菜单添加到菜单栏

wenjian.add(xinjian);

wenjian.add(dakai);//将保存按钮添加到下拉菜单

wenjian.add(baochun);

dakai.addActionListener(this);//由baochuan按钮接受处理操作事件

baochun.addActionListener(this);

xinjian.addActionListener(this);

frame.add(wenben);//将文本区域添加到窗口

//设置窗口的属性

frame.setSize(600,480);

frame.setLocation(200, 200);

frame.setVisible(true);

/*

 * 

 * 用于接收窗口事件的侦听器接口。

 * 旨在处理窗口事件的类要么实现此接口(及其包含的所有方法),

 * 要么扩展抽象类 WindowAdapter(仅重写所需的方法)。

 * 然后使用窗口的 addWindowListener 方法将从该类所创建

 * 的侦听器对象向该 Window 注册。

 * 当通过打开、关闭、激活或停用、

 * 图标化或取消图标化而改变了窗口状态时,

 * 将调用该侦听器对象中的相关方法,

 * 并将 WindowEvent 传递给该方法。 

 * windowClosing(WindowEvent e) 

用户试图从窗口的系统菜单中关闭窗口时调用。

 *  

 */

frame.addWindowListener(new WindowAdapter() {

 public void windowClosing(WindowEvent e) {

 System.exit(0);//终止当前运行的java虚拟机

 }

});

}

/*

 * 实现ActionListener接口中的方法

 * (non-Javadoc)

 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)

 */

@Override

public void actionPerformed(ActionEvent e) {

if(e.getSource()==baochun){

//public Object getSource()最初发生 Event 的对象。 返回,最初发生 Event 的对象。

fd=new FileDialog(frame,"保存文本文件",FileDialog.SAVE);

/*

 * FileDialog(Dialog parent, String title, int mode) 

  创建一个具有指定标题的文件对话框窗口,用于加载或保存文件。

  

static int LOAD 

  此常量值指示文件对话框窗口的作用是查找要读取的文件。 

static int SAVE 

  此常量值指示文件对话框窗口的作用是查找要写入的文件。 

 */

fd.setVisible(true);

//文件的存放位置是通过fd获取的位置,文件名是通过fd获取的文件名

file=new File(fd.getDirectory(), fd.getFile()+".txt");

save(file);

}

if(e.getSource()==dakai){

fd=new FileDialog(frame,"打开文件",FileDialog.LOAD);

fd.setVisible(true);

/*

 * String getFile() 

  获取此文件对话框的选定文件。 

   String getDirectory() 

  获取此文件对话框的目录。        

 */

file=new File(fd.getDirectory()+fd.getFile());

System.out.println(fd.getFile());

System.out.println(fd.getDirectory());

open(file);

}

if(e.getSource()==xinjian){

wenben.setText("");

}

}

/*

 * 创建save方法

 */

public void save(File file){

try{

PrintWriter pw=new PrintWriter(file);

pw.write(wenben.getText());//wenben.getText()返回文本区域的文本

pw.close();

}catch(Exception e){

}

}

//创建open方法

public void open(File file){

try{

FileInputStream fis=new FileInputStream(file);

InputStreamReader isr=new InputStreamReader(fis);

BufferedReader br=new BufferedReader(isr);

String line=null;

while((line=br.readLine())!=null){

wenben.append(line+"\n");

}

br.close();

}catch(Exception e){

}

}

public static void main(String[] args) {

new JiShiBen();

}

}

如何用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编写一个记事本 ,具体步骤!

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JColorChooser;

import javax.swing.JDialog;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JOptionPane;

import javax.swing.JScrollPane;

import javax.swing.JTextPane;

import javax.swing.JToolBar;

public class TextEditorFrame extends JFrame {

File file = null;

Color color = Color.red;

TextEditorFrame() {

initTextPane();

initAboutDialog();

initToolBar();

initMenu();

}

void initTextPane() {

getContentPane().add(new JScrollPane(text));

}

JTextPane text = new JTextPane(); // 这是用来做文本框的

JFileChooser filechooser = new JFileChooser(); // 文件选择框

JColorChooser colorchooser = new JColorChooser();//

JDialog about = new JDialog(this); // 关于对话框

JMenuBar menubar = new JMenuBar();// 菜单

JMenu[] menus = new JMenu[] { new JMenu("文件"), new JMenu("编辑"),

new JMenu("帮助") };

JMenuItem menuitems[][] = new JMenuItem[][] {

{ new JMenuItem("新建"), new JMenuItem("打开"), new JMenuItem("保存"), new JMenuItem("退出") },

{new JMenuItem("复制"), new JMenuItem("剪切"), new JMenuItem("粘贴"), new JMenuItem("颜色") }, { new JMenuItem("关于") } };

void initMenu() {

for (int i = 0; i menus.length; i++) {

menubar.add(menus[i]);

for (int j = 0; j menuitems[i].length; j++) {

menus[i].add(menuitems[i][j]);

menuitems[i][j].addActionListener(action);

}

}

this.setJMenuBar(menubar);

}

ActionListener action = new ActionListener() { // when here have not wrong:

public void actionPerformed(ActionEvent e) {

JMenuItem mi = (JMenuItem) e.getSource();

String id = mi.getText();

if (id.equals("新建")) {

text.setText("");

file = null;

} else if (id.equals("打开")) {

if (file != null)

filechooser.setSelectedFile(file);

int returnVal = filechooser

.showOpenDialog(TextEditorFrame.this);

if (returnVal == JFileChooser.APPROVE_OPTION) {

file = filechooser.getSelectedFile();

openFile();

}

} else if (id.equals("保存")) {

if (file != null)

filechooser.setSelectedFile(file);

int returnVal = filechooser

.showSaveDialog(TextEditorFrame.this);

if (returnVal == JFileChooser.APPROVE_OPTION) {

file = filechooser.getSelectedFile();

saveFile();

}

} else if (id.equals("退出")) {

TextEditorFrame f = new TextEditorFrame();

int s = JOptionPane.showConfirmDialog(f, "你真的要结束吗", "结束程序",

JOptionPane.YES_NO_CANCEL_OPTION);

if (s == JOptionPane.YES_OPTION)

System.exit(0);

} else if (id.equals("剪切")) {

text.cut();

} else if (id.equals("复制")) {

text.copy();

} else if (id.equals("粘贴")) {

text.paste();

} else if (id.equals("color")) {

color = JColorChooser.showDialog(TextEditorFrame.this, "",

color);

text.setForeground(color);

} else if (id.equals("关于")) {

about.setSize(200, 150);

about.show();

}

}

};

void saveFile() {

try {

FileWriter fw = new FileWriter(file);

fw.write(text.getText());

fw.close();

} catch (Exception e) {

e.printStackTrace();

}

}

void openFile() {

try {

FileReader fr = new FileReader(file);

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

char[] buffer = new char[len];

fr.read(buffer, 0, len);

fr.close();

text.setText(new String(buffer));

} catch (Exception e) {

e.printStackTrace();

}

}

void initAboutDialog() {

about.getContentPane().add(new JLabel("作者-覃树新:记事本Q1.0版本"));

about.setModal(true);

about.setSize(200, 100);

}

JToolBar toolbar = new JToolBar();// 我来加上工具条

JButton[] buttons = new JButton[] {

new JButton("", new ImageIcon("qin.jpg")),

new JButton("", new ImageIcon("shu.jpg")),

new JButton("", new ImageIcon("xin.jpg")) };

void initToolBar() {

for (int i = 0; i buttons.length; i++)

toolbar.add(buttons[i]);

buttons[0].setToolTipText("复制");

buttons[0].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

text.copy();

}

});

buttons[1].setToolTipText("剪切");

buttons[1].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

text.cut();

}

});

buttons[2].setToolTipText("粘贴");

buttons[2].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

text.paste();

}

});

this.getContentPane().add(toolbar, BorderLayout.NORTH);

}

public static void main(String args[]) {

TextEditorFrame f = new TextEditorFrame();

f.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e)

{

TextEditorFrame f = new TextEditorFrame();

int s = JOptionPane.showConfirmDialog(f, "你真的要结束吗", "结束程序",

JOptionPane.YES_NO_OPTION);

if (s == JOptionPane.YES_OPTION)

System.exit(0);

}

});

f.setTitle("简单的记事本");

f.setSize(800, 600);

f.setVisible(true);

}

}

以前看到的一个程序

如何用JAVA程序编写一个记事本

import java.io.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.undo.CannotRedoException;

import javax.swing.undo.UndoManager;

import java.util.Date;

import java.text.SimpleDateFormat;

public class Notepad extends JFrame {

// 菜单

JMenuBar menub = new JMenuBar();

// 显示纯文本的多行区域

JTextArea text = new JTextArea();

JMenu files = new JMenu("文件(F)");

JMenu edit = new JMenu("编辑(E)");

JMenu formats = new JMenu("格式(O)");

JMenu help = new JMenu("帮助(H)");

JMenuItem newFile = new JMenuItem("新建(N)");

JMenuItem open = new JMenuItem("打开(O)");

JMenuItem save = new JMenuItem("保存(S)");

JMenuItem saveAs = new JMenuItem("另存为(A)");

JMenuItem exit = new JMenuItem("退出(X)");

JMenuItem undo = new JMenuItem("撤销(U)");

JMenuItem cut = new JMenuItem("剪切(T)");

JMenuItem copy = new JMenuItem("复制(C)");

JMenuItem paste = new JMenuItem("粘贴(P)");

JMenuItem selectAll = new JMenuItem("全选(A)");

JMenuItem timeDate = new JMenuItem("时间/日期(D)");

JCheckBoxMenuItem lineWrap = new JCheckBoxMenuItem("自动换行(M)");

JMenuItem fonts = new JMenuItem("字体");

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

JFrame th = this;

String name;

String openedPath = null;

boolean opened = false;

boolean reworked = false;

UndoManager undoManager = new UndoManager();

// 初始化窗体

public Notepad(String name) {

super(name);

this.name = name;

int x, y;

// 得到用户屏幕大小

Dimension size = Toolkit.getDefaultToolkit().getScreenSize();

x = (size.width - 600) / 2;

y = (size.height - 400) / 2;

setSize(600, 400);

// 让程序界面显示在屏幕中央

setLocation(x, y);

// 将此窗口的最小大小设置为一个常量值。

setMinimumSize(new Dimension(250, 150));

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

// 初始化布局

void init() {

files.setMnemonic('F');

edit.setMnemonic('E');

formats.setMnemonic('O');

help.setMnemonic('H');

newFile.setMnemonic('N');

open.setMnemonic('O');

save.setMnemonic('S');

saveAs.setMnemonic('A');

exit.setMnemonic('X');

undo.setMnemonic('U');

cut.setMnemonic('T');

copy.setMnemonic('C');

paste.setMnemonic('P');

selectAll.setMnemonic('A');

timeDate.setMnemonic('D');

lineWrap.setMnemonic('M');

about.setMnemonic('A');

// 为控件添加助记符

newFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,

InputEvent.CTRL_MASK));

open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,

InputEvent.CTRL_MASK));

save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,

InputEvent.CTRL_MASK));

exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,

InputEvent.CTRL_MASK));

cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,

InputEvent.CTRL_MASK));

copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,

InputEvent.CTRL_MASK));

paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,

InputEvent.CTRL_MASK));

selectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,

InputEvent.CTRL_MASK));

// 为控件添加快捷键

timeDate.setAccelerator(KeyStroke.getKeyStroke("F5"));

files.add(newFile);

files.add(open);

files.add(save);

files.add(saveAs);

files.addSeparator();

files.add(exit);

edit.add(undo);

edit.addSeparator();

edit.add(cut);

edit.add(copy);

edit.add(paste);

edit.addSeparator();

edit.add(selectAll);

edit.add(timeDate);

formats.add(lineWrap);

formats.add(fonts);

help.add(about);

menub.add(files);

menub.add(edit);

menub.add(formats);

menub.add(help);

setJMenuBar(menub);

getContentPane().add(new JScrollPane(text));

Listen listen = new Listen();

Listen1 listen1 = new Listen1();

// 为控件添加事件侦听器

newFile.addActionListener(listen);

undo.addActionListener(listen);

open.addActionListener(listen);

save.addActionListener(listen);

saveAs.addActionListener(listen);

exit.addActionListener(listen);

cut.addActionListener(listen);

copy.addActionListener(listen);

paste.addActionListener(listen);

selectAll.addActionListener(listen);

timeDate.addActionListener(listen);

lineWrap.addActionListener(listen);

about.addActionListener(listen);

open.addActionListener(listen1);

save.addActionListener(listen1);

saveAs.addActionListener(listen1);

// 暂时没有实现的功能 :设置字体

//undo功能没有实现

fonts.setEnabled(false);

}

class Listen implements ActionListener {

// 实现用于一般操作的事件侦听器

public void actionPerformed(ActionEvent e) {

Object source = e.getSource();

if (source == newFile) {

text.setText("");

// 设置标题

th.setTitle(name);

openedPath = null;

opened = false;

} else if (source == exit)

setVisible(false);

else if (source == undo)

try {

//此功能没有实现 撤销要用栈?

// undo.setEnabled(undoManager.canUndo());

undoManager.undo();

} catch (CannotRedoException cre) {

cre.printStackTrace();

}

else if (source == selectAll)

text.selectAll();

else if (source == cut)

text.cut();

else if (source == copy)

text.copy();

else if (source == paste)

text.paste();

else if (source == lineWrap)

// 设置文本区的换行策略(获取文本区的换行策略)

text.setLineWrap(!text.getLineWrap());

else if (source == about) {

String message = "--------\n版本:1.0\n作者:时超" +

"\n撤销功能要用堆栈存贮操作" +

"\n还有字体格式"+

"\n暂时没有实现" +

"\n\n感谢您的使用";

JOptionPane.showMessageDialog(th, message, "关于",

JOptionPane.PLAIN_MESSAGE);

} else if (source == timeDate) {

Date nowTime = new Date();

SimpleDateFormat times = new SimpleDateFormat(

"HH:mm yyyy-MM-dd");

text.insert(times.format(nowTime), text.getCaretPosition());

}

}

}

class Listen1 implements ActionListener {

// 实现用于对文件进行操作的事件侦听器

public void actionPerformed(ActionEvent e) {

Object source = e.getSource();

// 打开文件事件

if (source == open) {

// 显示对话窗口 以便选择文件

FileDialog openFile = new FileDialog(th, "打开文件",

FileDialog.LOAD);

openFile.setVisible(true);

// 获取文件路径

String filePath = openFile.getDirectory() + openFile.getFile();

try {

FileInputStream fis = new FileInputStream(filePath);

byte[] content = new byte[fis.available()];

fis.read(content);

text.setText(new String(content));

// 设置 TextComponent 的文本插入符的位置

text.setCaretPosition(0);

if (openFile.getFile() != null) {

th.setTitle(openFile.getFile() + name);

openedPath = filePath;

opened = true;

}

fis.close();

} catch (Exception ex) {

ex.printStackTrace();

}

opened = true;

}

// 保存及另存为事件

else if (source == save || source == saveAs) {

String savePath = openedPath;

if (savePath == null || source == saveAs) {

// 如果 mode 的值为 LOAD,那么文件对话框将查找要读取的文件,所显示的文件是当前目录中的文件

// 如果 mode 的值为 SAVE,则文件对话框将查找要写入文件的位置。

FileDialog saveFile = new FileDialog(th, "保存文件",

FileDialog.SAVE);

saveFile.setVisible(true);

savePath = saveFile.getDirectory() + saveFile.getFile();

}

try {

FileOutputStream fos = new FileOutputStream(savePath);

fos.write(text.getText().getBytes());

fos.close();

} catch (Exception ex) {

ex.printStackTrace();

}

if (source == save)

openedPath = savePath;

}

}

}

public static void main(String[] args) {

try {

// 使用当前线程的上下文类加载器加载给定类名称所指定的 LookAndFeel

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

} catch (Exception e) {

System.err.println("Couldn't use the system look and feel:" + e);

}

String name = ("--记事本 版本1.0--");

Notepad note = new Notepad(name);

note.init();

note.setVisible(true);

}

}


分享题目:用Java代码做记事本 JAVA记事本代码
标题来源:http://cdkjz.cn/article/hgddjh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220