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

📄 notebook.txt

📁 记事本的源代码
💻 TXT
📖 第 1 页 / 共 3 页
字号:
					}						
					catch(IOException ioException){					
					}				
				}
			
			}//"另存为"处理结束
		
		//退出
		else if(e.getSource()==fileMenu_Exit){
			int exitChoose=JOptionPane.showConfirmDialog(this,"确定要退出么?","退出提示",JOptionPane.OK_CANCEL_OPTION);
			if(exitChoose==JOptionPane.OK_OPTION){
				System.exit(0);
			}
			else{			
				return;
			}
		}

		
		
	//撤消
		else if(e.getSource()==editMenu_Undo||e.getSource()==popupMenu_Redo)
		{
			editArea.requestFocus();
			if(undo.canUndo()) { 
				try { 
					undo.undo(); 
					
					
				}
				catch(CannotUndoException ex) { 
					System.out.println("Unable to undo: " + ex); 
					ex.printStackTrace(); 
				} 
				
				if(!undo.canUndo()){
				editMenu_Undo.setEnabled(false);
				popupMenu_Undo.setEnabled(false);
		}	
			}
		}
	
	//剪切
		else if(e.getSource()==editMenu_Cut||e.getSource()==popupMenu_Cut)
			{
				editArea.requestFocus();
				String text=editArea.getSelectedText();
      			StringSelection selection=new StringSelection(text);
      			clipBoard.setContents(selection,null);
      			editArea.replaceRange("",editArea.getSelectionStart(),editArea.getSelectionEnd());
      			checkMenuItemEnabled();	//设置剪切、复制、粘贴、删除等功能的可用性
			}
		
		//复制
		else if(e.getSource()==editMenu_Copy||e.getSource()==popupMenu_Copy)
			{
				editArea.requestFocus();
			    String text=editArea.getSelectedText();
      			StringSelection selection=new StringSelection(text);
      			clipBoard.setContents(selection,null);
      			checkMenuItemEnabled();	//设置剪切、复制、粘贴、删除等功能的可用性
			}
		
		//粘贴	
		else if(e.getSource()==editMenu_Paste||e.getSource()==popupMenu_Paste)
			{
			editArea.requestFocus();
				Transferable contents=clipBoard.getContents(this);
		        if(contents==null) return;
		        String text;
		        text="";
		        
		        try{
		            text=(String)contents.getTransferData(DataFlavor.stringFlavor);
		        }
		        catch(Exception exception){
		        }
		        
		       	editArea.replaceRange(text,editArea.getSelectionStart(),editArea.getSelectionEnd());
		       	checkMenuItemEnabled();	//设置剪切、复制、粘贴、删除等功能的可用性
			
			}
		//删除
		else if(e.getSource()==editMenu_Delete||e.getSource()==popupMenu_Delete)
			{
				editArea.requestFocus();
				editArea.replaceRange("",editArea.getSelectionStart(),editArea.getSelectionEnd());
				checkMenuItemEnabled();	//设置剪切、复制、粘贴、删除等功能的可用性
				
			}
		
		//查找
		else if(e.getSource()==editMenu_Find)
			{
				editArea.requestFocus();
				{
					editArea.requestFocus();
					editArea.setCaretPosition(0);
				}
				mySearch();
			}
		
		//查找下一个(此功能尚未能很好实现,所以就先用查找功能来代替)	
		else if(e.getSource()==editMenu_FindNext)
			{
				mySearch();
			}
			
		//替换(与查找功能集成在一起了)
		else if(e.getSource()==editMenu_Replace)
		{
			mySearch();
		}
		
//插入时间日期	
		else if(e.getSource()==editMenu_TimeDate)
			{
			/**editArea.requestFocus();
				SimpleDateFormat currentDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
				editArea.insert(currentDateTime.format(new Date()),editArea.getCaretPosition());*/
				Date today=new Date();
	    		int startString;
	    	
	    		String dateString=new String();
	    		dateString=today.toLocaleString();
	    		startString=editArea.getSelectionStart();
	    		editArea.insert(dateString, startString); 
			}
		
    	   		 		
    	
		//全选	
		else if(e.getSource()==popupMenu_SelectAll||e.getSource()==editMenu_SelectAll)
			{
				editArea.selectAll();
			}
			
		//自动换行
		else if(e.getSource()==formatMenu_LineWrap)
			{
				if(formatMenu_LineWrap.getState()) 
				{
					editArea.setLineWrap(true);
				} 
				else
					editArea.setLineWrap(false);
			}
		
		//字体设置
		else if(e.getSource()==formatMenu_Font)
			{
				editArea.requestFocus();
				new MyFont();			
			}
		

		//帮助主题
		else if(e.getSource()==helpMenu_HelpTopics)
			{
				editArea.requestFocus();
				String helpTopicsStr=new String("<center><br>欢迎使用"+
						                        "<br>参考教材:java程序设计教程<br>"+
						                        "<a href=http://www.hzbook.com target=_blank>"+
												"http://www.hzbook.com</a><br>"+
												"2008年1月6日</center>");
												
				JEditorPane editPane=new JEditorPane("text/html",helpTopicsStr);
				editPane.setEditable(false);
			
				editPane.addHyperlinkListener(new HyperlinkListener(){
					public void hyperlinkUpdate(HyperlinkEvent hle){
				
						if (hle.getEventType()==HyperlinkEvent.EventType.ACTIVATED)
						{
							try
							{   
 								Process process = Runtime.getRuntime().exec("cmd.exe /c start http://gxbinbin.blog.edu.cn");   
 							}
 							catch(Exception e)
 							{
 								e.printStackTrace();
 							} 
						}
					}
				});
			
				JFrame helpTopicsFrame=new JFrame("帮助主题");
				helpTopicsFrame.setContentPane(new JScrollPane(editPane));
				helpTopicsFrame.setSize(250,200);
				helpTopicsFrame.setLocation(300,300);
				helpTopicsFrame.setVisible(true);
		}	
		
		//关于
		else if(e.getSource()==helpMenu_About){
			JOptionPane.showMessageDialog(this,
			"*****************************************************\n"+
			"* 课  程:Java记事本程序设计              *\n"+
			"* 设计时间:2008年1月2日——1月7日          *\n"+
			"* 指导老师:祝青老师                  *\n"+
			"* 地点:湖南城市学院实验楼计算机机房511       *\n"+
			"* 程序设计人员:0506202*12 杨娟           *\n"+
            "*****************************************************\n"		
			,"关于记事本",JOptionPane.INFORMATION_MESSAGE);			
		}
		
			
	}/*方法actionPerformed()结束*/
	
	//实现DocumentListener接口中的方法 
	public void removeUpdate(DocumentEvent e) { 
			editMenu_Undo.setEnabled(true); 
			popupMenu_Undo.setEnabled(true);
			
		} 
	public void insertUpdate(DocumentEvent e) { 
			editMenu_Undo.setEnabled(true);
			popupMenu_Undo.setEnabled(true); 
			
		} 
	public void changedUpdate(DocumentEvent e) { 
			editMenu_Undo.setEnabled(true);
			popupMenu_Undo.setEnabled(true);
			
		} 
	//End of DocumentListener 
	
	//main 方法
	public static void main(String args[])
	{
		//设置界面为WindowsLookAndFeel
		try 
		{ 
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 
		}
		catch(Exception e) { 
		} 
	
		NoteBook	frame = new NoteBook();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}/*main 方法结束*/
	
	//实现了接口UndoableListener的类UndoHandler
	class UndoHandler implements UndoableEditListener { 
		public void undoableEditHappened(UndoableEditEvent uee){ 
		undo.addEdit(uee.getEdit()); 
		} 
	}
	//用于设置字体的类MyFont
	class MyFont implements ActionListener{
		final JDialog fontDialog;
		final JTextField tfFont,tfSize,tfStyle;
		final int fontStyleConst[]={Font.PLAIN,Font.BOLD,Font.ITALIC,Font.BOLD+Font.ITALIC};
		final JList listStyle,listFont,listSize;
		JLabel sample;
		
		//构造函数MyFont
		public MyFont(){
		
			fontDialog = new JDialog(NoteBook.this,"字体设置",true);
			Container con=fontDialog.getContentPane();
			con.setLayout(new FlowLayout(FlowLayout.LEFT));
			
			Font currentFont=editArea.getFont();
			
			JLabel lblFont=new JLabel("字体(F):");
			lblFont.setPreferredSize(new Dimension(100,20));
			JLabel lblStyle=new JLabel("字形(Y):");
			lblStyle.setPreferredSize(new Dimension(100,20));
			JLabel lblSize=new JLabel("大小(S):");
			lblSize.setPreferredSize(new Dimension(100,20));
			tfFont=new JTextField(15);
			tfFont.setText(currentFont.getFontName());
			tfFont.selectAll();
			tfFont.setPreferredSize(new Dimension(200,20));
			tfStyle=new JTextField(15);
			if(currentFont.getStyle()==Font.PLAIN)
				tfStyle.setText("常规");
			else if(currentFont.getStyle()==Font.BOLD)
				tfStyle.setText("粗体");
			else if(currentFont.getStyle()==Font.ITALIC)
				tfStyle.setText("斜体");
			else if(currentFont.getStyle()==(Font.BOLD+Font.ITALIC))
				tfStyle.setText("粗斜体");
				
			tfFont.selectAll();
			tfStyle.setPreferredSize(new Dimension(200,20));
			tfSize=new JTextField(8);
			tfSize.setText(currentFont.getSize()+"");
			tfSize.selectAll();
			tfSize.setPreferredSize(new Dimension(200,20));
		
								
						
			final String fontStyle[]={"常规","粗体","斜体","粗斜体"};
			listStyle=new JList(fontStyle);
						
			GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
			final String fontName[]=ge.getAvailableFontFamilyNames();
			int defaultFontIndex=0;
			for(int i=0;i<fontName.length;i++)
				{
					if(fontName[i].equals(currentFont.getFontName()))
						{
							defaultFontIndex=i;
							break;
						}
				}
			listFont=new JList(fontName);
			listFont.setSelectedIndex(defaultFontIndex);			
			listFont.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			listFont.setVisibleRowCount(7);
			listFont.setFixedCellWidth(82);
			listFont.setFixedCellHeight(20);
			listFont.addListSelectionListener(
				new ListSelectionListener(){
					public void valueChanged(ListSelectionEvent event){
						tfFont.setText(fontName[listFont.getSelectedIndex()]);
						tfFont.requestFocus();
						tfFont.selectAll();	
						updateSample();
					}
				}
				);

			listStyle.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			if(currentFont.getStyle()==Font.PLAIN)
				listStyle.setSelectedIndex(0);
			else if(currentFont.getStyle()==Font.BOLD)
				listStyle.setSelectedIndex(1);
			else if(currentFont.getStyle()==Font.ITALIC)
				listStyle.setSelectedIndex(2);
			else if(currentFont.getStyle()==(Font.BOLD+Font.ITALIC))
				listStyle.setSelectedIndex(3);
			
			listStyle.setVisibleRowCount(7);
			listStyle.setFixedCellWidth(99);
			listStyle.setFixedCellHeight(20);
			listStyle.addListSelectionListener(
				new ListSelectionListener(){
					public void valueChanged(ListSelectionEvent event){
						tfStyle.setText(fontStyle[listStyle.getSelectedIndex()]);
						tfStyle.requestFocus();
						tfStyle.selectAll();
						updateSample();	
					}
				}
				);			
			
			final String fontSize[]={"8","9","10","11","12","14","16","18","20","22","24","26","28","36","48","72"};
			listSize=new JList(fontSize);
			int defaultFontSizeIndex=0;
			for(int i=0;i<fontSize.length;i++)
			{
				if(fontSize[i].equals(currentFont.getSize()+""))
				{
					defaultFontSizeIndex=i;
					break;
				}
			}
			listSize.setSelectedIndex(defaultFontSizeIndex);
			
			listSize.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			listSize.setVisibleRowCount(7);
			listSize.setFixedCellWidth(39);
			listSize.setFixedCellHeight(20);
			listSize.addListSelectionListener(
				new ListSelectionListener(){
					public void valueChanged(ListSelectionEvent event){
						tfSize.setText(fontSize[listSize.getSelectedIndex()]);
						tfSize.requestFocus();
						tfSize.selectAll();	
						updateSample();
					}
				}
				);
			fontOkButton=new JButton("确定");
			fontOkButton.addActionListener(this);
			JButton cancelButton=new JButton("取消");
			cancelButton.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					fontDialog.dispose();
				}
			});
			
			sample=new JLabel("记事本-HuangBin's NoteBook");
			sample.setHorizontalAlignment(SwingConstants.CENTER);
			sample.setPreferredSize(new Dimension(300,50));
			
			JPanel samplePanel=new JPanel();
			samplePanel.setBorder(BorderFactory.createTitledBorder("示例"));
			samplePanel.add(sample);
			
			con.add(lblFont);
			con.add(lblStyle);
			con.add(lblSize);
			con.add(tfFont);
			con.add(tfStyle);
			con.add(tfSize);
			con.add(fontOkButton);
			con.add(new JScrollPane(listFont));
			con.add(new JScrollPane(listStyle));
			con.add(new JScrollPane(listSize));
			con.add(cancelButton);
			con.add(samplePanel);
			updateSample();
			
			
			fontDialog.setSize(350,340);
			fontDialog.setLocation(200,200);
			fontDialog.setResizable(false);
			fontDialog.setVisible(true);
			}//构造函数结束
			
			//更新示例显示的字体和风格大小等
			public void updateSample(){
				Font sampleFont=new Font(tfFont.getText(),fontStyleConst[listStyle.getSelectedIndex()],Integer.parseInt(tfSize.getText()));
				sample.setFont(sampleFont);
			}//End method updateSample
			
			//设置文本编辑区的字体
			public void actionPerformed(ActionEvent e){
				if(e.getSource()==fontOkButton){
					Font tempFont=new Font(tfFont.getText(),fontStyleConst[listStyle.getSelectedIndex()],Integer.parseInt(tfSize.getText()));
					editArea.setFont(tempFont);
					fontDialog.dispose();					
				}
			}	
	}
		 
}


⌨️ 快捷键说明

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