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

📄 kpad.java

📁 WINDOWS记事本的代码,在JAVA下运行
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
			file=chooser.getSelectedFile();		
		
		FileInputStream fileInStream=null;
		
		if(file!=null)
		{
			try{
				fileInStream=new FileInputStream(file);
			}catch(FileNotFoundException fe){
				return;
			}
			
			int readbyte;
			
			try{
				while((readbyte=fileInStream.read())!=-1)
				{
					theArea.append(String.valueOf((char)readbyte));
				}
			}catch(IOException ioe){
				JOptionPane.showMessageDialog(null, "读取文件错误!", "错误", JOptionPane.ERROR_MESSAGE); 
			}
			finally{
				try{
					if(fileInStream!=null)
						fileInStream.close();
					
				}catch(IOException ioe2){}
			}
			
		}}else if(result==chooser.CANCEL_OPTION)
			chooser.setVisible(false);	
		
		
	}
    
    public void saveDocument(){
    	JFileChooser saver=new JFileChooser();    	
    	File file=null;
    	
    	int result=saver.showSaveDialog(this);
    	
    	if(result==saver.APPROVE_OPTION)
    	{
    		file=saver.getSelectedFile();
    	}
    	else if(result==saver.CANCEL_OPTION)
    	{
    		
    	}
    	FileOutputStream fileOutStream=null;
    	
    	if (file!=null)
    	{
    		try{
    			fileOutStream=new FileOutputStream(file);    			
    		}catch(FileNotFoundException fe){
    			return;
    		}
    		
    		String content=theArea.getText();
    		
    		try{
    			fileOutStream.write(content.getBytes());
    		}catch(IOException ioe){    		
    		}
    		finally{
    			try{
    				if(fileOutStream!=null)
    					fileOutStream.close();
    			}catch(IOException ioe2){}    			
    		}
    	}
    	
    	
    	
    	
    	
    }
	
    
    public void showAboutBox(){
		   JOptionPane.showMessageDialog(this,AboutMsg);
	}

    
    public int getOldFontIndex(){
		int i,j=0;
		for (i=0;i<=fontStr.length;i++)		
			if(fontStr[i].equals(theArea.getFont().getFamily())) 
				{			   
				   j=i;
				   break;
				}
							
		return j;
	}
	
    public int getOldAttributeIndex(){
		int i,j=0;
		for (i=0;i<=fontAttributeStr.length;i++)
		
			if(i==(theArea.getFont().getStyle())) 
				{
				   j=i;
				   break;
				}				
		return j;
	}
	
    public int getOldSizeIndex(){
		int i,j=0;
		try{
		  for (i=0;i<=fontSizeStr.length;i++)
		
			if(new Integer(fontSizeStr[i])==(theArea.getFont().getSize())) 
				{
				   j=i;
				   break;
				}	
		}catch(ArrayIndexOutOfBoundsException e){
			
		}
	 	return j;
	}
	
    
    
    public UndoManager getUndoManager()
    {
    	return myUndo;
    }
    
    
    public static void main(String[] args){
		Kpad F=new Kpad();
		F.setVisible(true);
	}
	
	
    
    public void valueChanged(ListSelectionEvent le){
		
	}
	
	
    
    class ActionSet extends AbstractAction{

	      /**
		 * 
		 */
	      private static final long serialVersionUID = 1L;

		  public ActionSet(String name,Icon icon){
	    	  super(name,icon);
	      }

	      public void updateUndoState(){
	    	  undo=getUndoManager();
	    	  JMenuItem undoItem=MBar.getMenu(1).getItem(0);
	    	  if(undo.canUndo()){
	    		  undoItem.setEnabled(true);
	    		  undoButton.setEnabled(true);
	    		  undoPop.setEnabled(true);
	    	  }else{
	    		  undoItem.setEnabled(false);
	    		  undoButton.setEnabled(false);
	    		  undoPop.setEnabled(false);
	    	  }
	    	  
	      }
		  
	      public void updateRedoState(){
	    	  undo=getUndoManager();
	    	  JMenuItem redoItem=MBar.getMenu(1).getItem(1);
	    	  if(undo.canRedo()){
	    		  redoItem.setEnabled(true);
	    		  redoButton.setEnabled(true);
	    		  redoPop.setEnabled(true);
	    	  }else{
	    		  redoItem.setEnabled(false);
	    		  redoButton.setEnabled(false);
	    		  redoPop.setEnabled(false);
	    	  }
	      }
	      
		  public void actionPerformed(ActionEvent e){
	  		String cmd=e.getActionCommand();	  		
	      
	  		if(cmd.equals("New"))
	  		{
	  			theArea.setText("");
	  		}
	  		
	  		if(cmd.equals("Open"))
	  		{
	  			openDocument();
	  		}
	  		
	  		if(cmd.equals("Save"))
	  		{
	  			saveDocument();
	  		}
	  		
	  		if(cmd.equals("Quit"))
	  		{
	  			dispose();
				System.exit(0);
	  		}
	  		
	  		if(cmd.equals("Undo"))
	  		{
	  			undo=getUndoManager();
	  			try{
	  				undo.undo();
	  			}catch(CannotUndoException ex){
	  				JOptionPane.showMessageDialog(null, "无法撤消!", "错误", JOptionPane.ERROR_MESSAGE);	  				
	  			}
	  			updateUndoState();
	  			updateRedoState();
	  		}
	  		
	  		if(cmd.equals("Redo"))
	  		{
	  			undo=getUndoManager();
	  			try{
	  				undo.redo();
	  			}catch(CannotRedoException ex){
	  				JOptionPane.showMessageDialog(null, "无法恢复!", "错误", JOptionPane.ERROR_MESSAGE);
	  			}
	  			updateRedoState();
	  			updateUndoState();	  			
	  		}
	  		
	  		if(cmd.equals("Cut"))
	  		{
	  			theArea.cut();
	  		}
	  		
	  		if(cmd.equals("Copy"))
	  		{
	  			theArea.copy();
	  		}
	  		
	  		if(cmd.equals("Paste"))
	  		{
	  			theArea.paste();
	  		}
	  		
	  		if(cmd.equals("Delete"))
	  		{
	  			
	  		}
	  		
	  		if(cmd.equals("SelectAll"))
	  		{
	  			theArea.selectAll();
	  		}
	  		
	  		if(cmd.equals("TimeAndDate"))
	  		{
	  			SimpleDateFormat ft=null;
	  	        Date date=null;
	  	        Calendar cl= Calendar.getInstance();
	  	        date=cl.getTime();	  	       
	  	        ft=new SimpleDateFormat("HH:mm yyyy-MM-dd");
	  	        String dateTime = ft.format(date);
	  	        if (theArea.getSelectedText()==null) theArea.append(dateTime);
	  	        else theArea.replaceSelection(dateTime);
	  		}
	  		
	  		if(cmd.equals("AutoWrap"))
	  		{
	  			if(theArea.getLineWrap()==true) 
	  			{
	  			    theArea.setLineWrap(false);
	  		        inputArea.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
	  			}
	  			else 
	  			{
	  				theArea.setLineWrap(true);
	  				theArea.setWrapStyleWord(true);
	  				inputArea.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	  			}
	  		}
	  		
	  		if(cmd.equals("Font"))
	  		{
	  			new FontChooser();
	  			
	  		}
	  		
	  		if(cmd.equals("ShowToolBar"))
	  		{
	  			if (toolBar.isVisible()) toolBar.setVisible(false);
	  			else toolBar.setVisible(true);
	  			
	  		}
	  		
	  		if(cmd.equals("LockToolBar"))
	  		{
	  			if (toolBar.isFloatable()) toolBar.setFloatable(false);
	  			else toolBar.setFloatable(true);
	  			
	  		}
	  			  		
	  		if(cmd.equals("MainHelp"))
	  		{
	  			JOptionPane.showMessageDialog(null,"Coming soon...");
	  		}
	  		
	  		if(cmd.equals("About"))
	  		{
	  			showAboutBox();
	  		}
	  		
	  		if(cmd.equals("Bold"))
	  		{
	  			int newAttributeIndex=0,oldFontIndex=getOldFontIndex(),oldAttributeIndex=getOldAttributeIndex(),oldFontSizeIndex=getOldSizeIndex();
	  			switch(oldAttributeIndex)
			      {
			                 case 0:
			                	newAttributeIndex=1;
			                    break;
			                 case 1:
			                	newAttributeIndex=0;
			                    break;
			                 case 2:
			                	newAttributeIndex=3;
			                    break;
			                 case 3:
			                	newAttributeIndex=2;
			                    break;
			                 default:
			                	 theArea.append(Integer.toString(oldAttributeIndex));
			                 
			      }
	  						    
	  			
	  			theArea.setFont(new Font(fontStr[oldFontIndex],newAttributeIndex,Integer.parseInt(fontSizeStr[oldFontSizeIndex])));
	  		}
	  		
	  		if(cmd.equals("Italic"))
	  		{
	  			int newAttributeIndex=0,oldFontIndex=getOldFontIndex(),oldAttributeIndex=getOldAttributeIndex(),oldFontSizeIndex=getOldSizeIndex();
	  			switch(oldAttributeIndex)
			      {
			                 case 0:
			                	newAttributeIndex=2;
			                    break;
			                 case 1:
			                	newAttributeIndex=3;
			                    break;
			                 case 2:
			                	newAttributeIndex=0;
			                    break;
			                 case 3:
			                	newAttributeIndex=1;
			                    break;
			                 
			      }
	  						    
	  			
	  			theArea.setFont(new Font(fontStr[oldFontIndex],newAttributeIndex,Integer.parseInt(fontSizeStr[oldFontSizeIndex])));
	  		}
	  		
	  		
	  		
	  		if(cmd.equals("ChangeColor"))
	  		{
	  			tColor=JColorChooser.showDialog(colorf,"改变文字颜色",Color.black);
	  			theArea.setForeground(tColor);
	  		}
	  		
	  		if(cmd.equals("ChangeBackColor"))
	  		{

⌨️ 快捷键说明

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