Java完整實(shí)現(xiàn)記事本代碼
進(jìn)入今天的正題:
1.整體設(shè)計(jì)思路如下:
(1)使用頂層容器JFrame。
(2)設(shè)置功能菜單并通過(guò)BorderLayout進(jìn)行邊框布局管理。
(3)設(shè)置相應(yīng)按鈕與文件編輯區(qū)。
(4)進(jìn)行相應(yīng)事件處理。
2.各功能菜單設(shè)計(jì)思路:
(1)打開(kāi)功能:
用戶點(diǎn)擊打開(kāi)后,可以選擇文件中對(duì)應(yīng)的txt或dat文件,用戶確定選擇后即可打開(kāi)改文件并展示文件中的內(nèi)容,并在程序正上方展示當(dāng)前文件路徑。
(2)新建功能: 用戶點(diǎn)擊新建功能后,將展示一個(gè)空白的記事本,用戶可進(jìn)行相應(yīng)編輯。
(3)保存功能: 用戶點(diǎn)擊保存后,如果保存的文件已經(jīng)存在路徑,則直接進(jìn)行覆蓋,若不存在,則需用戶自己選擇保存的路徑,并對(duì)保存的文件進(jìn)行命名。
(4)設(shè)定循環(huán)加解密規(guī)則如下:按照ASCII字符編碼(0-255),加密時(shí)對(duì)每一字符+10,(若超過(guò)255,減去255),解密時(shí)作對(duì)應(yīng)反變換。我們可以在文件I/O時(shí)進(jìn)行相應(yīng)操作。 再也不用擔(dān)心媽媽偷看你的筆記本啦??????
簡(jiǎn)單的運(yùn)行示例如下,其他的大家可以自行測(cè)試:

保存后的txt文件是這樣滴:

注意:用程序打開(kāi)時(shí)是會(huì)正常顯示哦!因?yàn)樵谧x取的時(shí)候也做了相應(yīng)解密。
例如,這是打開(kāi)的,所以有了他,是不是在也不用怕小秘密被別人知道啦?。?!??????

話不多說(shuō),上源碼:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.filechooser.FileNameExtensionFilter;
import chenhao.io.TextTool;
public class TextPad {
private JTextArea contentArea;
private JFrame frame;
private String fileName;
public TextPad() {
frame = new JFrame("記事本");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 添加菜單
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("文件");
JMenuItem newItem = new JMenuItem("新建");
newAction(newItem);
menu.add(newItem);
JMenuItem openItem = new JMenuItem("打開(kāi)");
openAction(openItem);
menu.add(openItem);
JMenuItem saveItem = new JMenuItem("保存");
saveAction(saveItem);
menu.add(saveItem);
menuBar.add(menu);
frame.setJMenuBar(menuBar);
// 布局
frame.setLayout(new BorderLayout());
JToolBar toolBar = new JToolBar();
JComboBox<String> fontCom = fontAction();
toolBar.add(fontCom);
JComboBox<String> fontSize = fontSizeAction();
toolBar.add(fontSize);
fontStyleAction(toolBar);
JButton colorbtn = fontColorAction();
toolBar.add(colorbtn);
frame.add(toolBar, BorderLayout.NORTH);
// 文件編輯區(qū)
contentArea = new JTextArea();
JScrollPane pane = new JScrollPane(contentArea);
frame.add(pane);
frame.setVisible(true);
}
private JButton fontColorAction() {
JButton colorbtn = new JButton("■");
colorbtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Color color = colorbtn.getForeground();
Color co = JColorChooser.showDialog(TextPad.this.frame, "設(shè)置字體顏色", color);
colorbtn.setForeground(co);
contentArea.setForeground(co);
}
});
return colorbtn;
}
// 記事本,字體格式
private void fontStyleAction(JToolBar toolBar) {
JCheckBox boldBox = new JCheckBox("粗體");
JCheckBox itBox = new JCheckBox("斜體");
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
boolean bold = boldBox.isSelected();
boolean it = itBox.isSelected();
int style = (bold ? Font.BOLD : Font.PLAIN) | (it ? Font.ITALIC : Font.PLAIN);
Font font = contentArea.getFont();
contentArea.setFont(new Font(font.getName(), style, font.getSize()));
//contentArea.setFont(new Font(font.getName(), style, font.getSize()));
}
};
boldBox.addActionListener(actionListener);
itBox.addActionListener(actionListener);
toolBar.add(boldBox);
toolBar.add(itBox);
}
// 記事本,設(shè)置字體大小
private JComboBox<String> fontSizeAction() {
String[] fontSizes = new String[] { "10", "20", "30", "50" };
JComboBox<String> fontSize = new JComboBox<>(fontSizes);
fontSize.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int size = Integer.valueOf((String) fontSize.getSelectedItem());
Font font = TextPad.this.contentArea.getFont();
TextPad.this.contentArea.setFont(new Font(font.getName(), font.getStyle(), size));
}
});
return fontSize;
}
// 記事本,設(shè)置字體
private JComboBox<String> fontAction() {
GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontNames = environment.getAvailableFontFamilyNames();
JComboBox<String> fontCom = new JComboBox<>(fontNames);
fontCom.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String fontName = (String) fontCom.getSelectedItem();
Font font = TextPad.this.contentArea.getFont();
TextPad.this.contentArea.setFont(new Font(fontName, font.getStyle(), font.getSize()));
}
});
return fontCom;
}
// 記事本新建操作
private void newAction(JMenuItem newItem) {
newItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
contentArea.setText("");
frame.setTitle("新建-記事本");
fileName = null;
}
});
}
// 記事本打開(kāi)文件操作
private void openAction(JMenuItem openItem) {
openItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Text & dat", "txt", "dat");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
String fileName = chooser.getSelectedFile().getPath();
TextPad.this.fileName = fileName;
String content = TextTool.read(fileName);
contentArea.setText(content);
TextPad.this.frame.setTitle(fileName + "- 記事本");
}
}
});
}
// 菜單 保存操作
private void saveAction(JMenuItem saveItem) {
saveItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (TextPad.this.fileName != null) {
String content = TextPad.this.contentArea.getText();
TextTool.write(TextPad.this.fileName, content);
} else {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Text & dat", "txt", "dat");
chooser.setFileFilter(filter);
int returnVal = chooser.showSaveDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
String fileName = chooser.getSelectedFile().getPath();
TextPad.this.fileName = fileName;
String content = TextPad.this.contentArea.getText();
TextTool.write(TextPad.this.fileName, content);
TextPad.this.frame.setTitle(fileName + "- 記事本");
}
}
}
});
}
public static void main(String[] args) {
TextPad pad = new TextPad();
}
}import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Reader;
import java.io.Writer;
import javax.swing.JOptionPane;
public class TextTool {
public static String read(String fileName) {
try (Reader reader = new FileReader(fileName); BufferedReader buff = new BufferedReader(reader);) {
String str;
StringBuilder sb = new StringBuilder();
while ((str = buff.readLine()) != null) {
str = decoding(str);
sb.append(str + "\n");
}
return sb.toString();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "找不到文件路徑" + fileName);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void write(String fileName, String content) {
try (Writer writer = new FileWriter(fileName);) {
content = encoding(content);
writer.write(content);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String encoding(String str) {
String temp = "";
for (int i = 0; i < str.length(); i++) {
if(str.charAt(i)=='\n')
{
temp+=str.charAt(i);
}
else if (0 <= str.charAt(i) && str.charAt(i) <= 255)
temp += (char) ((str.charAt(i) - '0' + 10) % 255);
else
temp += str.charAt(i);
}
return temp;
}
public static String decoding(String str) {
String temp = "";
for (int i = 0; i < str.length(); i++) {
if(str.charAt(i)=='\n')
{
temp+=str.charAt(i);
}
else if (0 <= str.charAt(i) && str.charAt(i) <= 255)
temp += (char) ((str.charAt(i) + '0' - 10 + 255) % 255);
else
temp += str.charAt(i);
}
return temp;
}
}到此這篇關(guān)于Java完整實(shí)現(xiàn)記事本代碼的文章就介紹到這了,更多相關(guān)Java記事本內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring框架基于AOP實(shí)現(xiàn)簡(jiǎn)單日志管理步驟解析
這篇文章主要介紹了Spring框架基于AOP實(shí)現(xiàn)簡(jiǎn)單日志管理步驟解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
使用JAVA8 filter對(duì)List多條件篩選的實(shí)現(xiàn)
這篇文章主要介紹了使用JAVA8 filter對(duì)List多條件篩選的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
springboot項(xiàng)目打包成jar包的圖文教程
有時(shí)候我們會(huì)用IDEA來(lái)開(kāi)發(fā)一些小工具,需要打成可運(yùn)行的JAR包,這篇文章主要給大家介紹了關(guān)于springboot項(xiàng)目打包成jar包的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
關(guān)于springmvc-servlet中的配置小知識(shí)詳解
這篇文章主要介紹了關(guān)于springmvc-servlet中的配置小知識(shí)詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
SpringBoot集成Redis—使用RedisRepositories詳解
這篇文章主要介紹了SpringBoot集成Redis—使用RedisRepositories詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Java如何導(dǎo)入Jsoup庫(kù)做一個(gè)有趣的爬蟲(chóng)項(xiàng)目
Jsoup庫(kù)是一款Java的HTML解析器,可用于從網(wǎng)絡(luò)或本地文件中獲取HTML文檔并解析其中的數(shù)據(jù),這篇文章給大家介紹Java導(dǎo)入Jsoup庫(kù)做一個(gè)有趣的爬蟲(chóng)項(xiàng)目,感興趣的朋友跟隨小編一起看看吧2023-11-11
springboot+vue2+elementui實(shí)現(xiàn)時(shí)間段查詢方法
這篇文章主要介紹了springboot+vue2+elementui實(shí)現(xiàn)時(shí)間段查詢方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-05-05
淺談Map集合中g(shù)et不存在的key值,會(huì)拋出異常嗎?
這篇文章主要介紹了淺談Map集合中g(shù)et不存在的key值,會(huì)拋出異常嗎?具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09

