java记事本程序代码,记事本写代码java怎么运行
今天进入正题:
1.总体设计思路如下:
(1)使用顶层容器JFrame。
(2)设置功能菜单,通过Borderlayout管理BorderLayout。
(3)设置相应的按钮和文件编辑区。
(4)处理相应的事件。
2.各功能菜单的设计思路:
(1)开启功能:
用户点击打开后,可以在文件中选择对应的txt或dat文件。在用户确认选择后,他可以打开修改后的文件并显示文件的内容,并在程序的正上方显示当前的文件路径。
(2)新功能:用户点击新功能后,会显示一个空白的记事本,用户可以进行相应的编辑。
(3)保存功能:用户点击保存后,如果保存的文件已经有路径,则直接覆盖;如果没有,用户需要选择保存的路径并命名保存的文件。
(4)设置循环加解密规则如下:根据ASCII字符编码(0-255),每个字符加密10(超过255则减255),解密时进行相应的逆变换。我们可以在文件I/O期间执行相应的操作.再也不用担心妈妈偷看你的笔记本了。
的简单运行示例如下,其他人可以自行测试:
保存的txt文件如下所示:
注意:程序打开时会正常显示!因为相应的解密也是在阅读的时候进行的。
比如,公开了,所以和他在一起,不要怕小秘密被别人知道!
话不多说,关于源代码:
导入Java . awt . borderlayout;导入Java . awt . color;导入Java . awt . font;导入Java . awt . graphics environment;导入Java . awt . event . action event;导入Java . awt . event . action listener;导入javax . swing . jbutton;导入javax . swing . jcheckbox;导入javax . swing . jcolorchooser;导入javax . swing . jcombobox;导入javax . swing . jfile chooser;导入javax . swing . jframe;导入javax . swing . jmenu;导入javax . swing . jmenubar;导入javax . swing . jmenuitem;导入javax . swing . jscrollpane;导入javax . swing . jtextarea;导入javax . swing . jtoolbar;导入javax . swing . file chooser . filenameextensionfilter;导入Chen Hao . io . text tool;public类TextPad { private JTextArea contentArea;私有JFrame框架;私有字符串文件名;public textpad(){ frame=new jframe( Notepad );frame.setSize(500,500);frame . setdefaultcloseoperation(JFrame。EXIT _ ON _ CLOSE);//add menu JMenuBar menuBar=new JMenuBar();JMenu menu=new JMenu( file );JMenuItem new item=new JMenuItem( new );new action(new item);menu . add(new item);JMenuItem open item=new JMenuItem( open );open action(open item);menu . add(open item);JMenuItem save item=new JMenuItem( save );save action(save item);menu . add(save item);menuBar.add(菜单);frame.setJMenuBar(菜单栏);//layout frame . set layout(newborderlayout());JToolBar toolBar=new JToolBar();JComboBoxString font com=font action();toolbar . add(font com);JComboBoxString font size=font size action();toolbar . add(fontSize);fontStyleAction(工具栏);JButton colorbtn=font color action();toolbar . add(colorbtn);frame.add(也是
lBar, BorderLayout.NORTH);// 文件编辑区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() {@Overridepublic void actionPerformed(ActionEvent e) {Color color = colorbtn.getForeground();Color co = JColorChooser.showDialog(TextPad.this.frame, "设置字体颜色", 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() {@Overridepublic 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);}// 记事本,设置字体大小private JComboBox<String> fontSizeAction() {String[] fontSizes = new String[] { "10", "20", "30", "50" };JComboBox<String> fontSize = new JComboBox<>(fontSizes);fontSize.addActionListener(new ActionListener() {@Overridepublic 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;}// 记事本,设置字体private JComboBox<String> fontAction() {GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();String[] fontNames = environment.getAvailableFontFamilyNames();JComboBox<String> fontCom = new JComboBox<>(fontNames);fontCom.addActionListener(new ActionListener() {@Overridepublic 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() {@Overridepublic void actionPerformed(ActionEvent e) {contentArea.setText("");frame.setTitle("新建-记事本");fileName = null;}});}// 记事本打开文件操作private void openAction(JMenuItem openItem) {openItem.addActionListener(new ActionListener() {@Overridepublic 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() {@Overridepublic 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);elsetemp += 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);elsetemp += str.charAt(i);}return temp;}}
到此这篇关于Java完整实现记事本代码的文章就介绍到这了,更多相关Java记事本内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。