首页 > 代码库 > 【J2SE】java仿windows记事本
【J2SE】java仿windows记事本
基于J2SE实现的一个记事本程序。
运行程序截图:
工程目录结构:
程序代码:
package com.hblg; import java.awt.BorderLayout; import java.awt.FileDialog; import java.awt.Font; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import javax.swing.ImageIcon; import javax.swing.JCheckBoxMenuItem; 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.JTextArea; import javax.swing.KeyStroke; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class NotePad extends JFrame implements ActionListener, ItemListener { static JTextArea ta; JMenuBar mb; FileDialog dialog_load, dialog_save; JMenu mFile, mEdit, mForm, mCheck, mHelp; JMenuItem open, save, exit, copy, cut, paste, search, about; JCheckBoxMenuItem lineWrap, state; FileReader fr; BufferedReader br; FileWriter fw; BufferedWriter bw; JLabel label = new JLabel("", JLabel.RIGHT); String copyString; public static void main(String[] args) { // TODO Auto-generated method stub // 设置界面风格 try { UIManager .setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); // SwingUtilities.updateComponentTreeUI(this); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedLookAndFeelException e) { // TODO Auto-generated catch block e.printStackTrace(); } new NotePad(); } public NotePad() { dialog_load = new FileDialog(this, "打开文件对话框", FileDialog.LOAD); dialog_save = new FileDialog(this, "保存文件对话框", FileDialog.SAVE); open = new JMenuItem("打开"); save = new JMenuItem("保存"); exit = new JMenuItem("退出"); copy = new JMenuItem("复制"); cut = new JMenuItem("剪切"); paste = new JMenuItem("粘贴"); search = new JMenuItem("查找"); about = new JMenuItem("关于"); lineWrap = new JCheckBoxMenuItem("自动换行"); state = new JCheckBoxMenuItem("状态栏"); save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK)); copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK)); cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK)); paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK)); mFile = new JMenu("文件(F)"); mFile.setMnemonic(KeyEvent.VK_F); mEdit = new JMenu("编辑(E)"); mForm = new JMenu("格式(O)"); mCheck = new JMenu("查找(V)", true); mHelp = new JMenu("帮助(H)"); mb = new JMenuBar(); ta = new JTextArea(); ta.setFont(new Font("华康少女文字W5(P)", Font.PLAIN, 12)); label.setFont(new Font("楷体", Font.PLAIN, 12)); JScrollPane sp = new JScrollPane(ta, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); mFile.add(open); mFile.add(save); mFile.add(exit); mEdit.add(copy); mEdit.add(cut); mEdit.add(paste); mEdit.add(search); mCheck.add(state); mForm.add(lineWrap); mHelp.add(about); about.addActionListener(this); open.addActionListener(this); save.addActionListener(this); exit.addActionListener(this); copy.addActionListener(this); cut.addActionListener(this); paste.addActionListener(this); search.addActionListener(this); lineWrap.addItemListener(this); state.addItemListener(this); mb.add(mFile); mb.add(mEdit); mb.add(mForm); mb.add(mCheck); mb.add(mHelp); this.setJMenuBar(mb); this.add(sp); // int w = Toolkit.getDefaultToolkit().getScreenSize().width; // int h = Toolkit.getDefaultToolkit().getScreenSize().height; this.setTitle("记事本"); this.setIconImage(Toolkit.getDefaultToolkit().getImage( NotePad.class.getClassLoader().getResource("images/Word.png"))); this.setSize(700, 600); // this.setLocation((w - 600) / 2, (h - 400) / 2); dialog_load.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { dialog_load.setVisible(false); } }); dialog_save.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { dialog_save.setVisible(false); } }); this.setLocationRelativeTo(null); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // System.out.println(System.getProperty("line.separator")); new Thread(new Check()).start(); } public void showState() { this.add(label, BorderLayout.SOUTH); this.setVisible(true); new Thread(new Show()).start(); } public void checkEditable() { if (lineWrap.isSelected()) { state.setSelected(false); state.setEnabled(false); } else { state.setEnabled(true); } } // 用于检查状态栏是否可用的线程 class Check implements Runnable { @Override public void run() { // TODO Auto-generated method stub while (true) { try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } checkEditable(); } } } // 用于显示状态栏的线程 class Show implements Runnable { @Override public void run() { // TODO Auto-generated method stub while (true) { try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } int rowCount = 0; String str = ta.getText().substring(0, ta.getCaretPosition()); for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == '\n') { rowCount++; } } int columnCount = 0; int temp = 0; String strs[] = ta.getText() .substring(0, ta.getCaretPosition()).split("\n"); if (strs.length > 1) { for (int i = 0; i < strs.length - 1; i++) { temp += strs[i].length(); } if (ta.getCaretPosition() == temp + rowCount) { columnCount = 0; } columnCount = ta.getCaretPosition() - temp - rowCount; } else { columnCount = ta.getCaretPosition() + 1; } // label.setText(""+ta.getText().length()); label.setText("字数:" + ta.getText().length() + " " + "第" + (rowCount + 1) + "行," + "第" + (columnCount) + "列" + " "); // System.out.println(ta.getCaretPosition()); } } } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getSource() == open) { dialog_load.setVisible(true); String s; if (dialog_load.getFile() != null) { try { ta.setText(null); ta.setFont(new Font("宋体", Font.PLAIN, 16)); File f = new File(dialog_load.getDirectory(), dialog_load.getFile()); fr = new FileReader(f); br = new BufferedReader(fr); while ((s = br.readLine()) != null) { ta.append(s + '\n'); } this.setTitle(dialog_load.getFile() + " - 记事本"); // System.out.println(ta.getLineCount()); fr.close(); } catch (IOException e1) { e1.printStackTrace(); } } } else if (e.getActionCommand() == save.getText()) { dialog_save.setVisible(true); if (dialog_save.getFile() != null) { try { File f = new File(dialog_save.getDirectory(), dialog_save.getFile()); fw = new FileWriter(f); bw = new BufferedWriter(fw); String str = ta.getText(); String lines[] = str.split("\n"); for (String line : lines) { bw.write(line + "\r\n"); } bw.close(); fw.close(); } catch (IOException e2) { e2.printStackTrace(); } } } else if (e.getActionCommand() == exit.getText()) { System.exit(0); } if (e.getSource() == copy) { copyString = ta.getSelectedText(); } if (e.getSource() == cut) { copyString = ta.getSelectedText(); ta.replaceRange("", ta.getSelectionStart(), ta.getSelectionEnd()); } if (e.getSource() == paste) { ta.insert(copyString, ta.getCaretPosition()); } if (e.getSource() == search) { new SearchFrame(this, "查找/替换", false).setVisible(true); } if (e.getSource() == about) { JOptionPane.showMessageDialog( this, "一个简单的记事本小程序,具有一些简单的编辑,查找功能", "^(oo)^", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(Toolkit.getDefaultToolkit().getImage( NotePad.class.getClassLoader().getResource( "images/0.gif")))); } } @Override public void itemStateChanged(ItemEvent e) { // TODO Auto-generated method stub if (lineWrap.isSelected()) { ta.setLineWrap(true); ta.setWrapStyleWord(true); } else { ta.setLineWrap(false); } if (state.isSelected()) { showState(); } else { this.remove(label); this.setVisible(true); } } }
package com.hblg; import java.awt.Dialog; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class SearchFrame extends Dialog implements WindowListener,ActionListener { JPanel p1, p2, p3; JLabel lSearch, lReplace; JTextField tSearch, tReplace; JButton b; public SearchFrame(Frame arg0, String arg1, boolean arg2) { super(arg0, arg1, arg2); p1 = new JPanel(); p2 = new JPanel(); p3 = new JPanel(); b = new JButton("确定"); lSearch = new JLabel("查找"); lReplace = new JLabel("替换"); tSearch = new JTextField(12); tReplace = new JTextField(12); p1.add(lSearch); p1.add(tSearch); p2.add(lReplace); p2.add(tReplace); p3.setLayout(new FlowLayout(FlowLayout.CENTER)); p3.add(b); this.add(p1); this.add(p2); this.add(p3); b.addActionListener(this); this.addWindowListener(this); this.setSize(300, 200); this.setModal(true); this.setLayout(new GridLayout(3, 1)); this.setLocationRelativeTo(null); } @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub String string = NotePad.ta.getText(); String toFind = this.tSearch.getText(); if(!toFind.equals("")) { int index = string.indexOf(toFind); int count = 0; while (index != -1) { //String str = string.substring(index, index+toFind.length()); NotePad.ta.replaceRange(this.tReplace.getText(), index, index+toFind.length()); index = string.indexOf(toFind, index+toFind.length()); count ++; } System.out.println(count); } else { JOptionPane.showMessageDialog(this, "`(*∩_∩*)′,什么都没写啊!"); } } @Override public void windowClosing(WindowEvent arg0) { // TODO Auto-generated method stub this.setVisible(false); } @Override public void windowActivated(WindowEvent arg0) { // TODO Auto-generated method stub } @Override public void windowClosed(WindowEvent arg0) { // TODO Auto-generated method stub } @Override public void windowDeactivated(WindowEvent arg0) { // TODO Auto-generated method stub } @Override public void windowDeiconified(WindowEvent arg0) { // TODO Auto-generated method stub } @Override public void windowIconified(WindowEvent arg0) { // TODO Auto-generated method stub } @Override public void windowOpened(WindowEvent arg0) { // TODO Auto-generated method stub } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。