java记事本
import javax.swing.*; import javax.swing.event.*; import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.text.BadLocationException; import java.awt.*; import java.awt.event.*; import java.io.*; public class NotepadApp extends JFrame { private JTextArea textArea; // 文本区域 private JFileChooser fileChooser; // 文件选择器 private File currentFile = null; // 当前文件 private JTextArea lineNumbers; // 行号区域 private JLabel positionLabel; // 显示当前位置(行、列)的标签 private JLabel charCountLabel; // 显示字符数的标签 private JLabel encodingLabel; // 显示编码格式的标签 public NotepadApp() { setTitle("简易记事本"); setSize(600, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 创建文本区域,自动换行 textArea = new JTextArea(); textArea.setFont(new Font("Monospaced", Font.PLAIN, 14)); textArea.setLineWrap(true); // 自动换行 textArea.setWrapStyleWord(true); // 按单词换行 // 创建行号显示区域 lineNumbers = new JTextArea("1\n"); lineNumbers.setFont(new Font("Monospaced", Font.PLAIN, 14)); lineNumbers.setBackground(Color.LIGHT_GRAY); lineNumbers.setEditable(false); // 文本滚动面板 JScrollPane scrollPane = new JScrollPane(textArea); scrollPane.setRowHeaderView(lineNumbers); // 左侧显示行号 add(scrollPane, BorderLayout.CENTER); // 底部状态栏 JPanel statusBar = new JPanel(new GridLayout(1, 3)); positionLabel = new JLabel("行: 1, 列: 1"); charCountLabel = new JLabel("字符数: 0"); encodingLabel = new JLabel("编码: UTF-8"); statusBar.add(positionLabel); statusBar.add(charCountLabel); statusBar.add(encodingLabel); add(statusBar, BorderLayout.SOUTH); // 创建菜单栏 JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("文件"); JMenu editMenu = new JMenu("编辑"); JMenu formatMenu = new JMenu("格式"); JMenuItem newItem = new JMenuItem("新建"); JMenuItem openItem = new JMenuItem("打开"); JMenuItem saveItem = new JMenuItem("保存"); JMenuItem saveAsItem = new JMenuItem("另存为"); JMenuItem exitItem = new JMenuItem("退出"); JMenuItem findReplaceItem = new JMenuItem("查找/替换"); JMenuItem fontItem = new JMenuItem("字体选择"); fileMenu.add(newItem); fileMenu.add(openItem); fileMenu.add(saveItem); fileMenu.add(saveAsItem); fileMenu.addSeparator(); fileMenu.add(exitItem); editMenu.add(findReplaceItem); formatMenu.add(fontItem); menuBar.add(fileMenu); menuBar.add(editMenu); menuBar.add(formatMenu); setJMenuBar(menuBar); // 右键菜单 JPopupMenu popupMenu = new JPopupMenu(); JMenuItem cutItem = new JMenuItem("剪切"); JMenuItem copyItem = new JMenuItem("复制"); JMenuItem pasteItem = new JMenuItem("粘贴"); popupMenu.add(cutItem); popupMenu.add(copyItem); popupMenu.add(pasteItem); textArea.setComponentPopupMenu(popupMenu); // 文件选择器 fileChooser = new JFileChooser(); fileChooser.setFileFilter(new FileNameExtensionFilter("文本文件 (*.txt)", "txt")); // 事件监听 newItem.addActionListener(e -> newFile()); openItem.addActionListener(e -> openFile()); saveItem.addActionListener(e -> saveFile()); saveAsItem.addActionListener(e -> saveFileAs()); exitItem.addActionListener(e -> System.exit(0)); findReplaceItem.addActionListener(e -> showFindReplaceDialog()); fontItem.addActionListener(e -> showFontDialog()); cutItem.addActionListener(e -> textArea.cut()); copyItem.addActionListener(e -> textArea.copy()); pasteItem.addActionListener(e -> textArea.paste()); // 监听文本变化,自动更新行号和字符数 textArea.getDocument().addDocumentListener(new DocumentListener() { public void insertUpdate(DocumentEvent e) { updateStatusBar(); } public void removeUpdate(DocumentEvent e) { updateStatusBar(); } public void changedUpdate(DocumentEvent e) { updateStatusBar(); } }); // 监听光标位置变化 textArea.addCaretListener(e -> updateCursorPosition()); setVisible(true); } private void showFontDialog() { String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); JComboBox<String> fontBox = new JComboBox<>(fonts); Integer[] sizes = {10, 12, 14, 16, 18, 20, 24, 28, 32, 36, 40}; JComboBox<Integer> sizeBox = new JComboBox<>(sizes); JCheckBox boldBox = new JCheckBox("加粗"); JCheckBox italicBox = new JCheckBox("斜体"); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(4, 1)); panel.add(fontBox); panel.add(sizeBox); panel.add(boldBox); panel.add(italicBox); int result = JOptionPane.showConfirmDialog(this, panel, "字体选择", JOptionPane.OK_CANCEL_OPTION); if (result == JOptionPane.OK_OPTION) { String fontName = (String) fontBox.getSelectedItem(); int fontSize = (int) sizeBox.getSelectedItem(); int fontStyle = Font.PLAIN; if (boldBox.isSelected()) fontStyle |= Font.BOLD; if (italicBox.isSelected()) fontStyle |= Font.ITALIC; textArea.setFont(new Font(fontName, fontStyle, fontSize)); } } // 更新状态栏信息 private void updateStatusBar() { // 获取当前文本内容 String text = textArea.getText(); int charCount = text.length(); // 更新字符数 charCountLabel.setText("字符数: " + charCount); // 更新编码(这里假设编码为 UTF-8) encodingLabel.setText("编码: UTF-8"); } // 更新光标位置信息 private void updateCursorPosition() { // 获取光标位置 int caretPos = textArea.getCaretPosition(); try { // 计算光标所在行和列 int line = textArea.getLineOfOffset(caretPos) + 1; // 行从 1 开始 int column = caretPos - textArea.getLineStartOffset(line - 1) + 1; // 列从 1 开始 // 更新显示位置 positionLabel.setText("行: " + line + ", 列: " + column); } catch (BadLocationException e) { e.printStackTrace(); } } // 新建文件 private void newFile() { textArea.setText(""); currentFile = null; setTitle("新建 - 记事本"); updateStatusBar(); } // 打开文件 private void openFile() { if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { currentFile = fileChooser.getSelectedFile(); try (BufferedReader reader = new BufferedReader(new FileReader(currentFile))) { textArea.read(reader, null); setTitle(currentFile.getName() + " - 记事本"); updateStatusBar(); } catch (IOException e) { JOptionPane.showMessageDialog(this, "无法打开文件", "错误", JOptionPane.ERROR_MESSAGE); } } } // 保存文件 private void saveFile() { if (currentFile != null) { try (BufferedWriter writer = new BufferedWriter(new FileWriter(currentFile))) { textArea.write(writer); } catch (IOException e) { JOptionPane.showMessageDialog(this, "无法保存文件", "错误", JOptionPane.ERROR_MESSAGE); } } else { saveFileAs(); } } // 另存为 private void saveFileAs() { if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { currentFile = fileChooser.getSelectedFile(); if (!currentFile.getName().toLowerCase().endsWith(".txt")) { currentFile = new File(currentFile.getAbsolutePath() + ".txt"); } saveFile(); setTitle(currentFile.getName() + " - 记事本"); } } // 查找和替换 private void showFindReplaceDialog() { JDialog dialog = new JDialog(this, "查找/替换", true); dialog.setSize(400, 150); dialog.setLayout(new GridLayout(3, 2)); JLabel findLabel = new JLabel("查找:"); JTextField findField = new JTextField(); JLabel replaceLabel = new JLabel("替换:"); JTextField replaceField = new JTextField(); JButton findButton = new JButton("查找"); JButton replaceButton = new JButton("替换"); dialog.add(findLabel); dialog.add(findField); dialog.add(replaceLabel); dialog.add(replaceField); dialog.add(findButton); dialog.add(replaceButton); findButton.addActionListener(e -> { String findText = findField.getText(); String content = textArea.getText(); int index = content.indexOf(findText); if (index != -1) { textArea.setCaretPosition(index); textArea.select(index, index + findText.length()); } else { JOptionPane.showMessageDialog(dialog, "未找到文本", "提示", JOptionPane.INFORMATION_MESSAGE); } }); replaceButton.addActionListener(e -> { String findText = findField.getText(); String replaceText = replaceField.getText(); textArea.setText(textArea.getText().replace(findText, replaceText)); }); dialog.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(NotepadApp::new); } }
posted on 2025-02-01 23:16 shenhshihao 阅读(7) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」