JAVA swing 记事本代码

package tarena;
//老师代码
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.awt.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import javax.swing.*;

public class Notepad implements ActionListener {
	private JTextArea jta = new JTextArea(20, 40);
	private JFrame jf = new JFrame("NotePad");
	private String copy = "";
	private JDialog jd = new JDialog(jf, "查找");
	private JTextField jtf = new JTextField(20);
	private String resu = "";
	private String tempResu = "";
	private int p = 0;
	private String file = "";

	public Notepad() {
		JMenuBar jmb = new JMenuBar();
		JMenu jm1 = new JMenu("文件");
		JMenu jm2 = new JMenu("编辑");
		JMenu jm3 = new JMenu("帮助");
		String[] label = { "新建", "打开", "保存", "另存为", "退出", "撤销", "剪切", "复制",
				"粘贴", "删除", "查找", "替换", "全选", "关于记事本" };
		JMenuItem[] jmi = new JMenuItem[label.length];
		for (int i = 0; i < jmi.length; i++) {
			jmi[i] = new JMenuItem(label[i]);
			jmi[i].addActionListener(this);
			if (i < 5) {
				jm1.add(jmi[i]);
			} else if (i < 13) {
				jm2.add(jmi[i]);
			} else {
				jm3.add(jmi[i]);
			}
		}
		jm1.insertSeparator(2);
		jm2.insertSeparator(1);
		jm2.insertSeparator(8);
		jmb.add(jm1);
		jmb.add(jm2);
		jmb.add(jm3);

		JPanel jp = new JPanel();
		jp.add(jtf);
		JButton jb1 = new JButton("查询");
		jb1.addActionListener(this);
		jp.add(jb1);
		jd.add(jp);
		jd.pack();
		jf.setJMenuBar(jmb);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.add(new JScrollPane(jta));
		jf.pack();
		jf.setVisible(true);
		jf.validate();
	}

	public void actionPerformed(ActionEvent e) {
		String comm = e.getActionCommand();
		if (comm.equals("新建")) {
			jta.setText("");
		} else if (comm.equals("打开")) {
			FileDialog fd = new FileDialog(jf, "请选择要打开的文件", FileDialog.LOAD);
			fd.setVisible(true);
			if (fd.getFile() != null) {
				file = fd.getDirectory() + fd.getFile();
				try {
					FileInputStream fis = new FileInputStream(file);
					byte[] b = new byte[1024];
					int count = 0;
					String resu = "";
					while ((count = fis.read(b)) != -1) {
						resu = resu + new String(b, 0, count);
					}
					fis.close();
					System.out.println(resu);
					jta.setText(resu);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		} else if (comm.equals("保存")) {
			String tempFile = "c:\\t.txt";
			if (!file.equals("")) {
				tempFile = file;
			}
			String temp = jta.getText();
			try {
				FileOutputStream fos = new FileOutputStream(tempFile);
				fos.write(temp.getBytes());
				fos.close();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		} else if (comm.equals("另存为")) {
			FileDialog fd = new FileDialog(jf, "请选择要打开的文件", FileDialog.SAVE);
			fd.setVisible(true);
			if (fd.getFile() != null) {
				file = fd.getDirectory() + fd.getFile();
				String temp = jta.getText();
				try {
					FileOutputStream fos = new FileOutputStream(file);
					fos.write(temp.getBytes());
					fos.close();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		} else if (comm.equals("退出")) {
			System.exit(0);
		} else if (comm.equals("复制")) {
			jta.copy();
		} else if (comm.equals("粘贴")) {
			copy = jta.getText();// 为了撤销用
			jta.paste();
		} else if (comm.equals("剪切")) {
			copy = jta.getText();
			jta.cut();
		} else if (comm.equals("全选")) {
			jta.selectAll();
		} else if (comm.equals("撤销")) {
			jta.setText(copy);
		} else if (comm.equals("查找")) {
			jd.setVisible(true);
			resu = jta.getText();
			tempResu = resu;
		} else if (comm.equals("查询")) {
			// jd.setVisible(false);
			String temp = jtf.getText();
			// tempResu为子字符串,resu是全部
			int begin = tempResu.indexOf(temp);
			// System.out.println(begin);
			if (begin != -1) {
				jta.select(p + begin, p + begin + temp.length());
				p = p + begin + temp.length();
				tempResu = resu.substring(p);
				System.out.println(p);
			} else {
				// 重新开始查找
				tempResu = jta.getText();
				p = 0;
			}
			System.out.println(resu);

		}
	}

	public static void main(String[] args) {
		new Notepad();
		System.out.println("main");

	}

}

 

你可能感兴趣的:(java,swing)