引用页_初学Java:仿写记事本_Notepad.java

<< 返回
  

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.undo.UndoManager;

public class Notepad extends WindowAdapter {
	private Notepad myself = null;
	private JFrame frame = null;			//主窗口
	private NoteMenu menu = null;			//菜单对象
	private JTextArea textArea = null;		//文本编辑区域
	private JScrollPane scrollPane = null;	//文本编辑区包含在这里,有滚动条
	private Font font = null;				//当前字体
	private File currentFile = null;
	private String savedString = "";
	private Status status = null;
	// 以下用于查找、替换
	private String findStr = null;			// 备注:三个属性,只有findStr是替换功能需要用到的,
	private boolean isCare = false;			// 替换的isCare不具有记忆功能,默认为false
	private String upOrDown = "DOWN";		// 替换的upOrDown永远为DOWN,不支持反向查找
	private String replaceStr = null;		// 替换功能专用
	
	private UndoManager undomg = new UndoManager();
	
	@SuppressWarnings("serial")
	public Notepad() throws Exception {
		this.myself = this;
		UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");	//使用Windows外观风格
		this.frame = new JFrame("无标题 - 记事本");	//默认的主窗口标题
		this.textArea = new JTextArea();					//创建
		this.scrollPane = new JScrollPane(this.textArea, 	//将文本编辑区放入滚动条区域内
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,		//始终显示垂直滚动条
				JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);	//仅在需要的时候显示水平滚动条
		this.menu = new NoteMenu(this);		//创建菜单
		this.font = new Font("宋体", 0, 16);	//默认字体  16 = 小四(与Windows程序不同)
		
		this.frame.setSize(750, 450);		//主窗口大小
		this.frame.setLocation(250, 150);	//主窗口位置
		// =============================================================//状态栏功能 - 开始
		this.status = new Status(this);
		this.status.setVisible(false);
		this.frame.add(this.status, BorderLayout.SOUTH);
        this.textArea.addMouseListener(new TextMouseListener(this.status, this, this.menu, null, null, null));
        this.textArea.addKeyListener(new TextKeyListener(this.status, this, this.menu, null, null, null));
		// =============================================================//状态栏功能 - 结束
        // =============================================================屏蔽重写JTextArea的 Ctrl + H 热键
        ActionMap actionMap = this.textArea.getActionMap();
        InputMap inputMap = this.textArea.getInputMap();
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_H, KeyEvent.CTRL_DOWN_MASK), "CTRL_H");
        actionMap.put("CTRL_H", new AbstractAction(){ 
		        	public void actionPerformed(ActionEvent e) { 
		                new Replace(myself);
		            }
		        });
        // =============================================================// 撤消功能开始
        this.textArea.getDocument().addUndoableEditListener(undomg);
        // =============================================================// 撤消功能结束
		this.frame.add(this.scrollPane, BorderLayout.CENTER);	//将滚动条区加入到主窗口中
		this.frame.setJMenuBar(this.menu);	//为主窗口设置菜单
		this.frame.addWindowListener(this);	//主窗口添加事件监听
		this.frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);	//设置点击主窗口红叉时,主窗口不会消失
		
		this.textArea.setEditable(true);	//设置文本编辑区允许编辑
		this.textArea.setFont(this.font);	//根据字体设置文本编辑区格式
		
		this.frame.setVisible(true);		//设置主窗口可见
	}
	
	public UndoManager getUndomg() {
		return this.undomg;
	}
	
	public void setFindStr(String str) {
		this.findStr = str;
	}
	
	public String getFindStr() {
		return this.findStr;
	}
	
	public void setReplaceStr(String str) {
		this.replaceStr = str;
	}
	
	public String getReplaceStr() {
		return this.replaceStr;
	}
	
	public void setUpOrDown(String upOrDown) {
		this.upOrDown = upOrDown;
	}
	
	public String getUpOrDown() {
		return this.upOrDown;
	}
	
	public void setIsCare(boolean isCare) {
		this.isCare = isCare;
	}
	
	public boolean getIsCare() {
		return this.isCare;
	}
	
	public void setNoteFont(Font font) {
		this.font = font;
		this.textArea.setFont(font);
	}
	
	public Font getNoteFont() {
		return this.font;
	}
	
	public NoteMenu getMenu() {
		return this.menu;
	}
	
	public void setPosition(int row) {	//光标定位到第row行开头
		int result = 0;
		String str = this.textArea.getText();
		char temp[] = str.toCharArray();
		for(int i = 0; i < temp.length; i++) {
			if('\n' == temp[i]) {
				System.out.println("找到一个换行符!");
				row--;
			}
			if(row == 1) {
				result = i;
				break;
			}
		}
		this.textArea.setCaretPosition(result + 1);
	}
	
	public void openFile() {	//“打开”功能套用“新建”功能
		this.createFile();
		new OpenFile(this);
	}
	
	public boolean saveAnother() {
		return new SaveFile(this, "ANOTHER").saveDone();
	}
	
	public boolean saveFile() {
		return new SaveFile(this).saveDone(); 
	}
	
	public void setCurrentFile(File file) {
		this.currentFile = file;
	}
	
	public File getCurrentFile() {
		return this.currentFile;
	}
	
	public void changTitle(String fileName) {
		this.frame.setTitle(fileName + " - 记事本");
	}
	
	public JTextArea getTextArea() {
		return this.textArea;
	}
	
	public JScrollPane getScrollPane() {
		return this.scrollPane;
	}
	
	public Status getStatus() {
		return this.status;
	}
	
	public JFrame getFrame() {
		return this.frame;
	}
	
	public void windowClosing(WindowEvent arg0) {	//窗口关闭时调用
		this.windowExit();
	}
	
	public void createFile() {
		int result = 0;
		if ((this.currentFile == null) && ("".equals(this.textArea.getText()))) {
			this.create();
		} else {
			if(this.checkSaved()) {	//如果当前文件已经保存过
				this.create();
			}
			result = JOptionPane.showConfirmDialog(this.frame, "是否保存更改?", "确认", 1);
			if (result == JOptionPane.YES_OPTION) {	//如果选择了“是”
				if(this.saveFile()){
					this.create();
				}
			} else if (result == JOptionPane.NO_OPTION) {
				this.create();
			}
		}
	}
	
	private void create() {
		this.savedString = "";
		this.findStr = "";
		this.textArea.setText("");
		this.frame.setTitle("无标题 - 记事本");
		this.currentFile = null;
	}
	
	public void windowExit() {
		int result = 0;
		if((this.currentFile == null) && ("".equals(this.textArea.getText()))) {	//如果是新文件,而且内容为空
			System.exit(1);
		} else {
			if(this.checkSaved()) {	//如果当前文件已经保存过
				System.exit(1);
			}
			result = JOptionPane.showConfirmDialog(this.frame, "是否保存更改?", "确认", 1);
			if (result == JOptionPane.YES_OPTION) {	//如果选择了“是”
				if(this.saveFile()){
					System.exit(1);	//保存成功则退出 
				}
			} else if (result == JOptionPane.NO_OPTION) {
				System.exit(1);
			}
		}
	}
	
	public void setSavedString(String str) {
		if (str == null) {
			try{
				throw new Exception();
			} catch (Exception e) {
				System.out.println("发生空字符串异常!\nNotepad.java: setSavedString()");
			}
		} else {
			this.savedString = str;
		}
	}
	
	public boolean checkSaved() {
		if (this.savedString.equals(this.textArea.getText())) {
			return true;
		} else {
			return false;
		}
	}
	
	public static void main(String args[]) throws Exception {
		new Notepad();
	}
}

  
<< 返回

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
出现以下报错的原因,改怎么解决,修改哪里2023-06-06 22:04:47.620 3151-3151/com.example.notepad E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.notepad, PID: 3151 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.notepad/com.example.notepad.MainActivity}: android.view.InflateException: Binary XML file line #32: addView(View, LayoutParams) is not supported in AdapterView at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2954) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3089) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1819) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6737) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860) Caused by: android.view.InflateException: Binary XML file line #32: addView(View, LayoutParams) is not supported in AdapterView Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView at android.widget.AdapterView.addView(AdapterView.java:503) at android.view.LayoutInflater.rInflate(LayoutInflater.java:867) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) at android.view.LayoutInflater.rInflate(LayoutInflater.java:866) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) at android.view.LayoutInflater.inflate(LayoutInflater.java:515) at android.view.LayoutInflater.inflate(LayoutInflater.java:423) at android.view.LayoutInflater.inflate(LayoutInflater.java:374) at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:420) at android.app.Activity.setContentView(Activity.java:2772) at com.example.notepad.MainActivity.onCreate(MainActivity.java:30) at android.app.Activity.performCreate(Activity.java:7144) at android.app.Activity.performCreate(Activity.java:7135) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2934) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3089) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1819) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6737) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860)
06-07

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值