⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainframe.java

📁 实现了Window的NotePad的功能
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
					}
				});
		
		//菜单——替换
		itemReplace.addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						replace();
					}
				});
		
		//菜单——自动换行
		itemAutoLine.addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						changeLineAuto();
					}
				});
		
		//菜单——字体 更改
		itemFont.addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						changeFont();
					}
				});
		
//		菜单——背景色 更改
		itemBackground.addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						
					}
				});
		
//		菜单——前景色 更改
		/*itemForeground.addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						
					}
				});
		*/
//		菜单——光标色 更改
		itemCaretColor.addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						
					}
				});
		
		
		//菜单——关于
		itemAbout.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				//
				JOptionPane.showOptionDialog(null,
						"程序名称:\n		JNotePadEditor \n" +
						"程序设计: \n		陈求江\n" +
						"简介:\n	一个简单的文字编辑器\n " + 
						"	可作为验收Java的实现对象\n" + 
						" 	欢迎网友下载研究交流\n\n" + 
						"http://ecolab.ruc.edu.cn/", 
						"JNotePadEditor By 2008.12.31",
						JOptionPane.DEFAULT_OPTION, 
						JOptionPane.INFORMATION_MESSAGE,
						null, null, null);
			}
		});
		
		//菜单——帮助
		itemHelp.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e) {
						openHelpFile();
					}
				});
		
		//编辑区域键盘事件
		areaText.addKeyListener(
				new KeyAdapter() {
					public void keyTyped(KeyEvent e) {
						//System.out.println("Sourse:" + e.getKeyModifiersText(KeyEvent.CTRL_DOWN_MASK));
						//System.out.println("KeyChar byte:" + (byte)e.getKeyChar());
						byte keyTypedChar = (byte)e.getKeyChar();
						//
						byte[] ctrlKeys = {6,18};
						Arrays.sort(ctrlKeys);
						//if(Arrays.binarySearch(ctrlKeys,keyTypedChar) == -1) {
//						说明CTRL+F(6) 或 CTRL+R(18) 键未被按下	
						if(keyTypedChar > 30) {
							//说明CTRL类的有效组合 键未被按下						
							stateBarChanged();				//状态栏 已修改
							//updateCurrentColRows();			//更新当前光标所在的列,行
							//if(areaText.getText()!="") {
							String x = areaText.getText();
							//x = x.replace(" |\t", "");
							if(!x.equals("")) {							
								setItemEnabled(true); 		//菜单——查找等可见
								//if(areaText.getCaretPosition()!=0) {
									//itemSearch.setEnabled(true);
								//}
							}
							else {
								setItemEnabled(false);
							}
						}
					}
				});
		
		//编辑区域鼠标事件
		areaText.addMouseListener(
				new MouseAdapter() {
					public void mouseReleased(MouseEvent e) {
						if(e.getButton() == MouseEvent.BUTTON3){	//右单击
							popUpMenu.show(menuEdit, e.getX(), e.getY());
							//popUpMenu.setVisible(true);
						}
					}
					
					public void mouseClicked(MouseEvent e) {
						//返回当前光标位置,这是为了使查找从当前光标开始
						textCurrentpos = areaText.getCaretPosition();		
						if(e.getButton() == MouseEvent.BUTTON1)	{//左单击
							popUpMenu.setVisible(false);
							//updateCurrentColRows();			//更新当前光标所在的列,行
						}
					}
				});		
		areaText.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent e) {
				//int dot = e.getDot();
				//int mark = e.getMark();
				if(!areaText.getLineWrap()) {
					//格式设置为:非自动换行
					Rectangle rec;
					try {
						int caretPos = areaText.getCaretPosition();
						rec = areaText.modelToView(caretPos);
						int row = (rec.y / rec.height + 1);
						int col = 1;
						int offset = -1;
						if(rec.x != 0) {
							 offset = areaText.getLineStartOffset(row-1);
							 //System.out.println("offset:" + offset);
							 int lastLinePos = offset-row+1;
							 col = caretPos-lastLinePos;
							 if(offset == 0)
								 col +=1;
						}	
						//System.out.println("caretPos:" + caretPos);
						//System.out.println("rec:" + rec.toString());
						//System.out.println("行号:" + row);
						//System.out.println("列号:" + col);
						updateRowCols(row,col);
					} catch (BadLocationException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			     
				
				//System.out.println("dot :" + dot);
				//System.out.println("mark :" + mark);
			}
		});
	}
	
	private void createInitFile() {
		//创建初始文件
		//创建无标题的新建文本文件
		this.setTitle("新建无标题文本文件");		//
		areaText.setText("");					//清空当前JTextArea,回到一个新建的模样
		this.stateBarUnChanged();		
	}
	
	private void createNewFile() {
		if(isCurrentFileSaved()) {	//文件是否为保存状态
			createInitFile();		//创建无标题的新建文本文件
		}
		else {
			//显示确认对话框
			int option = JOptionPane.showConfirmDialog(
					null, "文件已修改,是否保存?",
					"保存文件", JOptionPane.YES_NO_CANCEL_OPTION,
					JOptionPane.WARNING_MESSAGE, null);
			switch(option) {
			//确认文件保存
			case JOptionPane.YES_OPTION:	//保存文件
				saveFileAs();
				createInitFile();
				break;
			case JOptionPane.NO_OPTION:		//放弃文件保存,直接新建一个文件
				createInitFile();
				break;
			case JOptionPane.CANCEL_OPTION:	//取消当前操作
				break;
			}
		}
		
	}
	
	private void openFile(){
		if(isCurrentFileSaved()) {	//文件是否为保存状态
			openNew();
		}
		else {
			//显示确认对话框
			int option = JOptionPane.showConfirmDialog(
					null, "文件已修改,是否保存?",
					"保存文件", JOptionPane.YES_NO_CANCEL_OPTION,
					JOptionPane.WARNING_MESSAGE, null);
			switch(option) {
			//确认文件保存
			case JOptionPane.YES_OPTION:	//保存文件
				saveFile();
				break;
			case JOptionPane.NO_OPTION:		//放弃文件保存,打开一个新文件
				openNew();
				break;
			case JOptionPane.CANCEL_OPTION:	//取消当前操作
				break;
			}
		}
		
 
		//BufferedReader br = new BufferedReader(
				//new FileReader(file));
	}
//	打开一个指定文件路径的文件
	private void openFile(String openFileNm) {
		File file = new File(openFileNm);
    	System.out.println("You chose to open this file: " + openFileNm);
    	
    	try {
    		//打开选取的文件
			BufferedReader buf = new BufferedReader(
					new FileReader(file));
			//设置文件标题
			setTitle(openFileNm);
			//删除前一次的文件
			areaText.setText("");
			//设置状态栏为初始状态
			this.stateBarUnChanged();
			//取得系统相依的换行字符
			String lineSeparator = System.getProperty("line.separator");
			//读取文件并附加至文字编辑区
			String text;
			while((text = buf.readLine()) != null) {
				areaText.append(text);
				areaText.append(lineSeparator);
			}
			areaText.setCaretPosition(0);
			if(areaText.getText()!="") {
				setItemEnabled(true);
			}
			buf.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			JOptionPane.showMessageDialog(null, e.toString(),
					"打开文件失败", JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		} catch (IOException e){
			//TODO Auto-generated catch block
			JOptionPane.showMessageDialog(null, e.toString(),
					"打开文件失败", JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		}
	}
	
	private void openHelpFile(){
		openFile("help.txt");
	}
	
	private void saveFile(){
		String fileNm = this.getTitle();
		//从标题栏取得文件名称
		File file = new File(fileNm);
		//若指定的文件不存在
		if(!file.exists()) {
			//执行另存为...
			saveFileAs();
		}
		else {
			try {
				BufferedWriter buf = new BufferedWriter(
						new FileWriter(file));
				buf.write(areaText.getText());
				buf.flush();
				buf.close();
				//设置状态栏为:“未修改”
				this.stateBarUnChanged();
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				JOptionPane.showMessageDialog(null, e.toString(),
						"保存文件失败", JOptionPane.ERROR_MESSAGE);
				e.printStackTrace();
			}
		}
		
	}
	
	//保存字体
	 void saveFont() {
		if(fontChanged) {
			//如果字体设置被改动,则需要保存改动至外存中
			try {
				BufferedWriter buf = new BufferedWriter(
						new FileWriter(fileFontSet,false));
				//buf.write(selectedFont);
				//buf.write(selectedStyle);
				//buf.write(selectedSize);
				String fontStr = ""+selectedFont+"\t"+selectedStyle+"\t"+selectedSize;
				buf.write(fontStr);				
				buf.flush();
				buf.close();
				//设置状态栏为:“未修改”
				//this.stateBarUnChanged();
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				//JOptionPane.showMessageDialog(null, e.toString(),
						//"保存文件失败", JOptionPane.ERROR_MESSAGE);
				e.printStackTrace();
			}
		}
	}
	private void saveFileAs(){
		//显示文件对话框
		try{		
			
		//JFileChooser   filechooser   =   new   JFileChooser();  
		int option = chooser.showSaveDialog(this);
		//int option = chooser.showDialog(null, null);
		
		//如果确认选取文件
		if(option == JFileChooser.APPROVE_OPTION) {
			//取得选择的文件
			File file = chooser.getSelectedFile();
			
			//在标题栏上设置文件名称
			this.setTitle(file.toString());			
			
			try {
				//建立新文件
				file.createNewFile();
				//进行文件保存
				saveFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				JOptionPane.showMessageDialog(null, e.toString(),
						"新建文件失败", JOptionPane.ERROR_MESSAGE);
				e.printStackTrace();
			}
			
		}
		}catch(NullPointerException e) {
			System.out.println("exception : " + e.getMessage());
			e.printStackTrace();
		}
	}
	
	private void closeFile(){
		//是否已保存文件
		if(isCurrentFileSaved()) {
			//释放窗口资源, 而后关闭程序
			this.dispose();
		}
		else {
			int option = JOptionPane.showConfirmDialog(null, "文件已修改,是否保存?",
					"保存文件", JOptionPane.YES_NO_CANCEL_OPTION,
					JOptionPane.WARNING_MESSAGE, null);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -