java记事本程序代码
时间: 2023-11-18 09:55:01 浏览: 115
这个Java记事本程序的源码可以通过引用中提供的链接进行下载。该程序主要实现了新建、打开、保存和关闭四个简单的功能,同时也展示了如何使用Java创建界面、安装各种监听器、创建窗口的主菜单、创建JMenuItem并添加到对应的JMenu等操作。如果想要运行该程序,可以在main方法中调用new Notepad()方法。总体来说,这个简单的记事本程序带码具有一定的参考价值,可以帮助新手学习和使用窗口、菜单、IO流等相关知识。
相关问题
java实训记事本程序代码
根据提供的引用内容,我可以为您提供一个简单的Java记事本程序代码,代码如下:
```
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class JavaNotepad extends Frame implements ActionListener {
TextArea textArea;
MenuBar menuBar;
Menu fileMenu, editMenu;
MenuItem newMenuItem, openMenuItem, saveMenuItem, saveAsMenuItem, exitMenuItem, cutMenuItem, copyMenuItem, pasteMenuItem, findMenuItem, replaceMenuItem;
public JavaNotepad() {
setTitle("Java Notepad");
setSize(500, 500);
setLocationRelativeTo(null);
textArea = new TextArea();
add(textArea);
menuBar = new MenuBar();
fileMenu = new Menu("File");
editMenu = new Menu("Edit");
newMenuItem = new MenuItem("New");
openMenuItem = new MenuItem("Open");
saveMenuItem = new MenuItem("Save");
saveAsMenuItem = new MenuItem("Save As");
exitMenuItem = new MenuItem("Exit");
cutMenuItem = new MenuItem("Cut");
copyMenuItem = new MenuItem("Copy");
pasteMenuItem = new MenuItem("Paste");
findMenuItem = new MenuItem("Find");
replaceMenuItem = new MenuItem("Replace");
newMenuItem.addActionListener(this);
openMenuItem.addActionListener(this);
saveMenuItem.addActionListener(this);
saveAsMenuItem.addActionListener(this);
exitMenuItem.addActionListener(this);
cutMenuItem.addActionListener(this);
copyMenuItem.addActionListener(this);
pasteMenuItem.addActionListener(this);
findMenuItem.addActionListener(this);
replaceMenuItem.addActionListener(this);
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.add(saveAsMenuItem);
fileMenu.addSeparator();
fileMenu.add(exitMenuItem);
editMenu.add(cutMenuItem);
editMenu.add(copyMenuItem);
editMenu.add(pasteMenuItem);
editMenu.addSeparator();
editMenu.add(findMenuItem);
editMenu.add(replaceMenuItem);
menuBar.add(fileMenu);
menuBar.add(editMenu);
setMenuBar(menuBar);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
}); }
public void actionPerformed(ActionEvent e) {
if (e.getSource() == newMenuItem) {
textArea.setText("");
} else if (e.getSource() == openMenuItem) {
FileDialog fileDialog = new FileDialog(this, "Open File", FileDialog.LOAD);
fileDialog.setVisible(true);
if (fileDialog.getFile() != null) {
String fileName = fileDialog.getDirectory() + fileDialog.getFile();
setTitle(fileName);
readFile(fileName);
}
} else if (e.getSource() == saveMenuItem) {
String fileName = getTitle();
if (fileName.equals("Java Notepad")) {
FileDialog fileDialog = new FileDialog(this, "Save File", FileDialog.SAVE);
fileDialog.setVisible(true);
if (fileDialog.getFile() != null) {
fileName = fileDialog.getDirectory() + fileDialog.getFile();
setTitle(fileName);
writeFile(fileName);
}
} else {
writeFile(fileName);
}
} else if (e.getSource() == saveAsMenuItem) {
FileDialog fileDialog = new FileDialog(this, "Save File As", FileDialog.SAVE);
fileDialog.setVisible(true);
if (fileDialog.getFile() != null) {
String fileName = fileDialog.getDirectory() + fileDialog.getFile();
setTitle(fileName);
writeFile(fileName);
}
} else if (e.getSource() == exitMenuItem) {
dispose();
} else if (e.getSource() == cutMenuItem) {
textArea.cut();
} else if (e.getSource() == copyMenuItem) {
textArea.copy();
} else if (e.getSource() == pasteMenuItem) {
textArea.paste();
} else if (e.getSource() == findMenuItem) {
String findString = JOptionPane.showInputDialog(this, "Find:");
String text = textArea.getText();
int index = text.indexOf(findString);
if (index != -1) {
textArea.select(index, index + findString.length());
}
} else if (e.getSource() == replaceMenuItem) {
String findString = JOptionPane.showInputDialog(this, "Find:");
String replaceString = JOptionPane.showInputDialog(this, "Replace:");
String text = textArea.getText();
int index = text.indexOf(findString);
if (index != -1) {
textArea.replaceRange(replaceString, index, index + findString.length());
}
}
}
private void readFile(String fileName) {
try {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line = null;
while ((line = reader.readLine()) != null) {
textArea.append(line + "\n");
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void writeFile(String fileName) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
writer.write(textArea.getText());
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
JavaNotepad javaNotepad = new JavaNotepad();
javaNotepad.setVisible(true);
}
}
```
这个记事本程序可以实现编辑、保存、另存为、查找和替换等功能。您可以根据需要进行修改和扩展。
--相关问题--:
1. Java中如何实现文件的读写操作?
2. 如何在Java中实现GUI编程?
3.
阅读全文