java编写记事本
程序员文章站
2022-05-10 11:15:05
...
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 ArrayListal = new ArrayList (); 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
上一篇: Python中使用ElementTree解析XML示例
下一篇: php编写的抽奖程序中奖概率算法