java编写记事本
2018-07-20 来源:open-open
import java.awt.FileDialog; import java.awt.FlowLayout; import java.awt.event.*; import java.io.*; import java.util.*; import javax.swing.*; public class Not implements ActionListener, MouseListener { private ArrayList<String> al = new ArrayList<String>(); private int set = 0; private JFrame jf = new JFrame("Notepad"); private JTextArea jta = new JTextArea(20,40); JScrollPane jsp = new JScrollPane(jta); private JTextField jtf = new JTextField(10); private JTextField jtft = new JTextField(10); private JPopupMenu pp = new JPopupMenu(); private String target = null; private JDialog jd = new JDialog(jf, "警告"); public void actionPerformed(ActionEvent e) { String comm = e.getActionCommand(); if (comm.equals("新建...")) { if(!jta.getText().equals("")){ jd.setLocation(300, 300); jd.setVisible(true); }else{ jta.setText("1234548882"); } } if(comm.equals("否")){ jta.setText(""); jd.dispose(); } if(comm.equals("取消")){ jd.dispose(); } if (comm.equals("保存") || comm.equals("另存...")||comm.equals("是")) { if(comm.equals("是")) jd.dispose(); FileDialog fd = new FileDialog(jf, "打开...", FileDialog.SAVE); fd.setFilenameFilter(new FilenameFilter() { public boolean accept(File dir, String name) { return name.endsWith(".txt") || name.endsWith(".java"); } }); if (target == null || "另存...".equals(comm)) { fd.setVisible(true); } if (fd.getFile() != null) { target = fd.getFile(); try { FileOutputStream fos = new FileOutputStream(fd .getDirectory() + target); byte[] b = jta.getText().getBytes(); for (byte by : b) { fos.write(by); } fos.close(); } catch (IOException e1) { e1.printStackTrace(); } } } if (comm.equals("打开...")) { al.add(jta.getText()); FileDialog fd = new FileDialog(jf, "打开...", FileDialog.LOAD); fd.setFilenameFilter(new FilenameFilter() { public boolean accept(File dir, String name) { return name.endsWith(".txt") || name.endsWith(".java"); } }); fd.setVisible(true); // 以下是打开文件 target = fd.getFile(); String str = fd.getDirectory() + target; if (fd.getFile() != null) { try { String res = ""; FileInputStream fis = new FileInputStream(str); byte[] b = new byte[1024]; int count; while ((count = fis.read(b)) != -1) { res = res + new String(b, 0, count); } fis.close(); jta.setText(res); } catch (IOException e1) { e1.printStackTrace(); } } } if (comm.equals("退出")) { System.exit(0); } if (comm.equals("撤销")) { if(!al.isEmpty()){ jta.setText(al.get(al.size()-1)); al.remove(al.size()-1); } } if (comm.equals("剪切")) { al.add(jta.getText()); jta.cut(); } if (comm.equals("复制")) { al.add(jta.getText()); jta.copy(); } if (comm.equals("粘贴")) { al.add(jta.getText()); jta.paste(); } if (comm.equals("删除")) { al.add(jta.getText()); jta.cut(); } if ("查找...".equals(comm) || "替换...".equals(comm)) { JDialog jdct = new JDialog(jf,comm); String label = comm.substring(0, 2); JButton jb = new JButton(label); jb.addActionListener(this); jdct.setLayout(new FlowLayout()); jdct.add(jtf); if ("替换...".equals(comm)) { JLabel jlt = new JLabel(">"); jdct.add(jlt); jdct.add(jtft); } jdct.add(jb); if ("替换...".equals(comm)) { JButton jball = new JButton("替换全部"); jball.addActionListener(this); jdct.add(jball); } jdct.pack(); jdct.setResizable(false); jdct.setLocation(400, 240); jdct.setVisible(true); } if (comm.equals("全选")) { jta.selectAll(); } if (comm.equals("关于...")) { JDialog jdgy = new JDialog(jf, "关于我..."); jdgy.setLayout(new FlowLayout()); JLabel jl = new JLabel("制作者: xxx "); JLabel j2 = new JLabel("Email:xxxx@.com "); jdgy.add(jl); jdgy.add(j2); jdgy.setSize(180, 100); jdgy.setResizable(false); jdgy.setLocation(400, 240); jdgy.setVisible(true); } //查找实现 if (comm.equals("查找")) { String str = jtf.getText(); String strall = jta.getText(); int n = strall.indexOf(str, set); if (n != -1) { set = n + 1; jta.setSelectionStart(n); jta.setSelectionEnd(n + str.length()); } } //替换实现 if (comm.equals("替换") || comm.equals("替换全部")) { al.add(jta.getText()); String str = jtf.getText(); String strall = jta.getText(); if (comm.equals("替换全部")) { int n1 = 0; while (true) { set = strall.indexOf(str, n1); if (set == -1) break; n1 = set + 1; jta.setSelectionStart(set); jta.setSelectionEnd(set + str.length()); jta.replaceSelection(jtft.getText()); } set = 0; } int n = strall.indexOf(str, set); if (n == -1) { set = 0; } else { set = n + 1; jta.setSelectionStart(n); jta.setSelectionEnd(n + str.length()); jta.replaceSelection(jtft.getText()); } } } public Notepad() {//构造器 jta.setLineWrap(true); jta.addMouseListener(this); JMenuBar jmb = new JMenuBar(); JMenu jm1 = new JMenu("文件"); String[] label1 = { "新建...", "打开...", "保存", "另存...", "退出" }; JMenuItem[] jmi1 = new JMenuItem[label1.length]; for (int i = 0; i < jmi1.length; i++) { jmi1[i] = new JMenuItem(label1[i]); jm1.add(jmi1[i]); jmi1[i].addActionListener(this); } jm1.insertSeparator(1); jm1.insertSeparator(3); jm1.insertSeparator(6); JMenu jm2 = new JMenu("编辑"); String[] label2 = { "撤销", "剪切", "复制", "粘贴", "删除", "查找...", "替换...", "全选" }; JMenuItem[] jmi2 = new JMenuItem[label2.length]; for (int i = 0; i < jmi2.length; i++) { jmi2[i] = new JMenuItem(label2[i]); jm2.add(jmi2[i]); jmi2[i].addActionListener(this); } jm2.insertSeparator(1); jm2.insertSeparator(5); jm2.insertSeparator(7); jm2.insertSeparator(10); JMenu jm3 = new JMenu("帮助"); JMenuItem jmi3 = new JMenuItem("关于..."); jm3.add(jmi3); jmi3.addActionListener(this); jmb.add(jm1); jmb.add(jm2); jmb.add(jm3); jf.setJMenuBar(jmb); jf.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { jf.removeAll(); jf.dispose(); } }); //鼠标右健弹出菜单 JMenuItem jmi11, jmi22, jmi33, jmi44; pp.add(jmi11 = new JMenuItem("剪切")); pp.add(jmi22 = new JMenuItem("复制")); pp.add(jmi33 = new JMenuItem("粘贴")); pp.addSeparator(); pp.add(jmi44 = new JMenuItem("全选")); jmi11.addActionListener(this); jmi22.addActionListener(this); jmi33.addActionListener(this); jmi44.addActionListener(this); jf.add(pp); // //新建菜单 jd.setLayout(new FlowLayout()); JButton jb1 = new JButton("是"); JButton jb2 = new JButton("否"); JButton jb3 = new JButton("取消"); jd.add(new JLabel("您当前编辑未保存,是否保存?")); jb1.addActionListener(this); jb2.addActionListener(this); jb3.addActionListener(this); jd.add(jb1); jd.add(jb2); jd.add(jb3); jd.pack(); jf.add(jsp); jf.pack(); jf.setLocation(300, 200); jf.setVisible(true); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new Notepad(); } public void mouseClicked(MouseEvent e) { if (e.getButton() == 3) { pp.setVisible(true); pp.show(e.getComponent(), e.getX(), e.getY()); } } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } }
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
最新资讯
热门推荐