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

📄 np.java

📁 java编写的记事本
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
			ji3_4.addActionListener(this);
			ji3_5.addActionListener(this);
			ji3_6.addActionListener(this);	
			jcolor.addActionListener(this);
			style.addActionListener(this);
			
			ji4_1.addActionListener(this);
			
			buttonNew.addActionListener(new Note_jButtonNew_actionAdapter(this));//添加新建按钮的监听
			buttonOpen.addActionListener(new Note_jButtonOpen_actionAdapter(this));//添加新建按钮的监听
			buttonSave.addActionListener(new Note_jButtonSave_actionAdapter(this));//添加新建按钮的监听
						
			//选择文件类型 作用是设置文件过滤者
			javax.swing.filechooser.FileNameExtensionFilter ff[] = {
					new javax.swing.filechooser.FileNameExtensionFilter("文本文件(*.txt)","txt"),
					new javax.swing.filechooser.FileNameExtensionFilter("Java文件(*.java)","java"),
					new javax.swing.filechooser.FileNameExtensionFilter("C文件(*.c)","c")
					};
			for(int i=0;i<ff.length;i++) {
				jfc.setFileFilter(ff[i]);
			}
			
			//调用自定义的notepad函数
			this.notepad();
					
			//设置记事本(JFrame)的外观和感觉***************************
			try {
				UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); //选择外观和感觉
				//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); //根据操作系统选择外观和感觉
			}
			catch (Exception exc) 
			{
				System.err.println("Error loading L&F: " + exc);
			} 
			javax.swing.SwingUtilities.updateComponentTreeUI(this);   //执行外观和感觉,使当前对象根据操作系统设置外观和感觉
			javax.swing.SwingUtilities.updateComponentTreeUI(jfc);    //使jfc对象根据操作系统设置外观和感觉
	
	}//构造函数结束

/***新建各种类************************************************************************************/	
   
	//创建字体监听器的类
	class MyItemListener_font implements ItemListener 
	  { //字体格式监听器
		  public void itemStateChanged(ItemEvent e)
		  {
			   id_font = ((java.awt.List)e.getSource()).getSelectedIndex();
			   switch( id_font)
			   {
					case  0:
					{
						 jta.setFont(new Font("Times New Roman", Font.PLAIN ,jta.getFont().getSize()) );//普通文字
						 break;
					}
					case  1:
					{
						 jta.setFont(new Font("Times New Roman" , Font.BOLD ,jta.getFont().getSize()) );//粗体文字
						 break;
					}
					case  2:
					{
						 jta.setFont(new Font("Times New Roman" , Font.ITALIC ,jta.getFont().getSize()) );//斜体文字
						 break;
					}
			   }
		  }
	  }	
	
	//查找类
	class Frame1 extends JDialog implements ActionListener 
	{ 
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		JButton jButton1 = new JButton(); 
		JButton jButton2 = new JButton(); 
		JButton jButton3 = new JButton(); 
			
		JLabel jLabel1 = new JLabel(); 
		JLabel jLabel2 = new JLabel(); 
		JTextField jTextField1 = new JTextField(); 
		JTextField jTextField2 = new JTextField(); 
		JCheckBox jCheckBox1 = new JCheckBox("区分大小写",true); 
		JRadioButton[] teams = new JRadioButton[2]; 
		ButtonGroup jj=new ButtonGroup(); 
		String findstr; 
			
		public Frame1(JTextArea jTextArea) 
		{ 
			try 
			{ 
				findstr=jTextArea.getText(); 
				jbInit(); 
			} 
			catch(Exception e) 
			{ 
				e.printStackTrace(); 
			} 
		} 
		
		
		@SuppressWarnings("deprecation")
		public void jbInit() throws Exception 
		{ 
			this.getContentPane().setLayout(null); 
			this.setBounds(120,120,380, 150); 
			this.setTitle("查找"); 
			this.setModal(true); 
			
			jButton1.setText("下一个"); 
			jButton1.setBounds(new Rectangle(273, 7, 91, 20)); 
			
			Button1Listener next=new Button1Listener(); 
			jButton1.addActionListener(next); 
			
			jButton3.setText("替换"); 
			jButton3.setBounds(new Rectangle(273, 36, 91, 20)); 
			Button3Listener replace =new Button3Listener(); 
			jButton3.addActionListener(replace); 
			
			jButton2.setText("取消"); 
			jButton2.setBounds(new Rectangle(273, 65, 91, 20)); 
			Button2Listener cancel=new Button2Listener(); 
			jButton2.addActionListener(cancel); 
			
			
			jLabel1.setText("查找内容 :"); 
			jLabel1.setBounds(new Rectangle(7, 10, 72, 19)); 
			jTextField1.setBounds(new Rectangle(78, 12, 182, 19)); 
			
			jLabel2.setText("替换为 :"); 
			jLabel2.setBounds(new Rectangle(7, 36, 72, 19)); 
			jTextField2.setBounds(new Rectangle(78, 36, 182, 19)); 
			
			jCheckBox1.setBounds(new Rectangle(7, 85, 89, 25)); 
			jCheckBox1.addActionListener(this); 
			
			teams[0]=new JRadioButton("向上",false); 
			teams[0].setBounds(new Rectangle(98, 85, 68, 22)); 
			teams[0].addActionListener(this); 
			
			teams[1]=new JRadioButton("向下",true); 
			teams[1].setBounds(new Rectangle(178, 85, 62, 25)); 
			teams[1].addActionListener(this); 
			
			jj.add(teams[0]); 
			jj.add(teams[1]); 
			
			this.getContentPane().add(jButton1, null); 
			this.getContentPane().add(jButton2, null); 
			this.getContentPane().add(jButton3, null); 
			this.getContentPane().add(jLabel1, null); 
			this.getContentPane().add(jLabel2, null); 
			this.getContentPane().add(jTextField1, null); 
			this.getContentPane().add(jTextField2, null); 
			this.getContentPane().add(jCheckBox1, null); 
			this.getContentPane().add(teams[0], null); 
			this.getContentPane().add(teams[1], null); 
			show(); 
		} 
		
		public void actionPerformed(ActionEvent e)
		{ 
			if (e.getSource() == jCheckBox1)
			{ 
				if(check)
				{ 
					findstr=findstr.toLowerCase(); 
				} 
				else 
					findstr=jta.getText(); 
				check=!check; 
			} 
		
		} 
		
		public void processWindowEvent(WindowEvent e)
		{ 
			super.processWindowEvent(e); 
			if(e.getID() == WindowEvent.WINDOW_CLOSED)
			{ 
				System.exit(0); 
			} 
		} 
		
		public void find()
		{ 
			String text1=jTextField1.getText(); 
			if(check)
			{ 
				text1=text1.toLowerCase(); 
			} 
			int index, 
			i=jta.getSelectionStart(); 
			jt=jta.getSelectionEnd(); 
			
			if(teams[0].isSelected())
			{ 
				String ftp=findstr.substring(0,i); 
				index = ftp.lastIndexOf(text1); 
				if(index!=-1)
				{ 
					i=index-1;
					jta.select(index,index+text1.length()); 
				} 
				else 
					JOptionPane.showMessageDialog(this,"找不到"+text1,"提示",JOptionPane.INFORMATION_MESSAGE); 
			} 
			else 
			{ 
				index = findstr.indexOf(text1,jt); 
				if(index!=-1)
				{ 
					jt=index+text1.length(); 
					jta.select(index,index+text1.length()); 
				} 
				else 
					JOptionPane.showMessageDialog(this,"找不到"+text1,"提示",JOptionPane.INFORMATION_MESSAGE); 
			} 
			this.repaint(); 
		} 
		
		//替换 
		public void replace()
		{ 
			String text2=jTextField2.getText(); 
			int star=jta.getSelectionStart(); 
			int end=jta.getSelectionEnd(); 
			if(star!=end)
			{ 
				jta.replaceRange(text2,star,end); 
			} 
			else 
			JOptionPane.showMessageDialog(this,"请确定要查找的内容","提示",JOptionPane.INFORMATION_MESSAGE); 
		} 
		
		class Button1Listener implements ActionListener
		{ 
			public void actionPerformed(ActionEvent e)
			{ 
				find(); 
			} 
		} 
		
		class Button2Listener implements ActionListener 
		{ 
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent e)
			{ 
				hide(); 
			} 
		} 
		
		class Button3Listener implements ActionListener 
		{ 
			public void actionPerformed(ActionEvent e)
			{ 
				replace(); 
			} 
		}
	}	

	//关闭窗口类
	class MyWindowListener implements WindowListener {
	    /**
	     * Invoked the first time a window is made visible.
	     */
	    public void windowOpened(WindowEvent e){}

	    /**
	     * Invoked when the user attempts to close the window
	     * from the window's system menu.
	     */
	    public void windowClosing(WindowEvent e){
	    	String s = jta.getText();
	    	if(bsaved == true){
	    		System.exit(0);
	    	}
	    	else if(s.equals("") && fileName.equals("")){
	    		System.exit(0);
	    	}
	    	else if(s.equals(ss)){
	    		System.exit(0);
	    	}
	    	else{
	    		int i = JOptionPane.showConfirmDialog(null, "您的文本内容已经改变,是否保存?");
	    		if(i == 0){
	    			jfc.setCurrentDirectory(new File(""));
					int hit = jfc.showSaveDialog(null);
					if(hit == 0) {
						try {
							BufferedWriter bw = new BufferedWriter(new FileWriter(jfc.getSelectedFile().getAbsolutePath()));
							String str = jta.getText();
						
							bw.write(str);	
							bw.close();
							
						} catch (FileNotFoundException e1) {
							e1.printStackTrace();
						} catch (IOException e2) {
							e2.printStackTrace();
						}
						System.exit(0);
					}					
		    	}
	    		else if(i == 1){
	    			System.exit(0);
	    		}
	    	}
	    }

	    /**
	     * Invoked when a window has been closed as the result
	     * of calling dispose on the window.
	     */
	    public void windowClosed(WindowEvent e){}

	    /**
	     * Invoked when a window is changed from a normal to a
	     * minimized state. For many platforms, a minimized window 
	     * is displayed as the icon specified in the window's 
	     * iconImage property.
	     * @see java.awt.Frame#setIconImage
	     */
	    public void windowIconified(WindowEvent e){}

	    /**
	     * Invoked when a window is changed from a minimized
	     * to a normal state.
	     */
	    public void windowDeiconified(WindowEvent e){}

	    /**
	     * Invoked when the Window is set to be the active Window. Only a Frame or
	     * a Dialog can be the active Window. The native windowing system may
	     * denote the active Window or its children with special decorations, such
	     * as a highlighted title bar. The active Window is always either the
	     * focused Window, or the first Frame or Dialog that is an owner of the
	     * focused Window.
	     */
	    public void windowActivated(WindowEvent e){}

	    /**
	     * Invoked when a Window is no longer the active Window. Only a Frame or a
	     * Dialog can be the active Window. The native windowing system may denote
	     * the active Window or its children with special decorations, such as a
	     * highlighted title bar. The active Window is always either the focused
	     * Window, or the first Frame or Dialog that is an owner of the focused
	     * Window.
	     */
	    public void windowDeactivated(WindowEvent e){}		
	}
	
	//打印类
	class MyPrintJob extends MouseAdapter implements MouseListener {
		private JFrame jFrame = null;
		
		public MyPrintJob(JFrame jf) {
			jFrame = jf;
		}
	    /**
	     * Invoked when the mouse button has been clicked (pressed
	     * and released) on a component.
	     */
	    public void mouseClicked(MouseEvent e){};

	    /**
	     * Invoked when a mouse button has been pressed on a component.
	     */
	    public void mousePressed(MouseEvent e){};

	    /**
	     * Invoked when a mouse button has been released on a component.
	     */
	    public void mouseReleased(MouseEvent e){
	    	/**
	    	 * 通过Toolkit类取得PrintJob的实例
	    	 */
			Toolkit tool = Toolkit.getDefaultToolkit();
			@SuppressWarnings("unused")
			PrintJob pj = tool.getPrintJob(jFrame, "打印", null);	    	
	    };

	    /**
	     * Invoked when the mouse enters a component.
	     */
	    public void mouseEntered(MouseEvent e){};

	    /**
	     * Invoked when the mouse exits a component.
	     */
	    public void mouseExited(MouseEvent e){};		
	}
	
	//高亮度显示编辑菜单类
	class MyMouseMotionListener extends MouseAdapter implements MouseMotionListener{

		@Override
		public void mouseDragged(MouseEvent e) {
			if(jta.getSelectedText() != null && !jta.getSelectedText().equals("")){
				ji2_1.setEnabled(true);
				ji2_2.setEnabled(true);
				ji2_3.setEnabled(true);
				ji2_5.setEnabled(true);
				ji2_6.setEnabled(true);
				ji2_7.setEnabled(true);
				

⌨️ 快捷键说明

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