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

📄 dreamtimesnotepad.java

📁 能实现记事本的基本功能
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	          break;
	        default:
	          break;
	      }
	    }
	    else {
	      switch(option) {
	        case JOptionPane.NO_OPTION :
	          openFile();
	          break;
	        case JOptionPane.YES_OPTION :
	          saveFile();
	          openFile();
	          break;
	        default:
	          break;
	         }
	        }
	      }
	  else {
	     openFile(); 
	     }   
	 }
	  
	void saveNewFile() {                        //保存新文件
	   OutputStreamWriter osw;
	   int pos = 0, t = 0;
	   int rv = fc.showSaveDialog(DreamTimesNotePad.this);//保存文件对话框
	   if (rv == JFileChooser.APPROVE_OPTION) {
	   file = fc.getSelectedFile(); 
	   fns = file.getName();
	   if(file != null) {                     
	   try { 
	    osw = new OutputStreamWriter(
	          new BufferedOutputStream(
	            new FileOutputStream(file)));
	      String str = text.getText();
	      while(true){
	      pos = str.indexOf('\12', pos);
	      if(pos == -1) break;
	      str = str.substring(0, pos) + '\15' + str.substring(pos);
	      pos = pos + 2;
	       }
	      osw.write(str, 0, str.length());
	      osw.close();
	      } catch(IOException e) { }
	   }
	   f.setTitle("记事本 - [" + file.getName() + "]");
	   undo.discardAllEdits();
	   Undo.setEnabled(false); 
	   statusFile.setText("已保存!");
	 }
	}    
	
	void saveFile() {                              //保存已打开的文件
	OutputStreamWriter osw;
	int pos = 0, t = 0;
	if(file != null) {
	try {
	  osw = new OutputStreamWriter(
	          new BufferedOutputStream(
	            new FileOutputStream(file)));
	  String str = text.getText();
	  while(true){
	    pos = str.indexOf('\12', pos);
	    if(pos == -1) break;
	    str = str.substring(0, pos) + '\15' + str.substring(pos);
	    pos = pos + 2;
	  }
	  osw.write(str, 0, str.length());
	  osw.close();
	 } catch(IOException e) { }
	}
	 f.setTitle("梦想年华记事本 - [" + file.getName() + "]");
	 fns = file.getName();
	 undo.discardAllEdits();
	 Undo.setEnabled(false);
	 statusFile.setText("已保存!");
	}   
	
	void exit(){
	if(statusFile.getText().endsWith("    *   ")) {
	     int option = JOptionPane.showConfirmDialog(null,
	     "文件  "+fns+"  文字已经被修改! \n 你是否要保存该文件?", "警告!", 
	     JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
	      switch(option) {
	        case JOptionPane.NO_OPTION :
	          System.exit(0);
	          break;
	        case JOptionPane.YES_OPTION :
	          if(fns == "新文件") {
	            saveNewFile();
	            System.exit(0);
	          }
	          else {
	            saveFile();
	            System.exit(0);
	          }
	          break;
	        default:
	          break;
	         }
	        }
	       else {
	        System.exit(0);
	       } 
	 }       
	                      
	void print(){ 	                       //打印
	  //获取打印服务对象
	  PrintJob pjob = tk.getPrintJob(DreamTimesNotePad.this,
	  file.getName(),null,null);
	  Graphics p = pjob.getGraphics();    //获取打印内容      
	  text.printAll(p);                   //调用printAll()方法实现打印
	  p.dispose();                        //关闭打印窗口
	  pjob.end();                         //打印结束
	}
	
	void setundo(){                         //撤消
	  if(undo.canUndo()) {
	        try {
	         undo.undo();
	         } catch(CannotUndoException cue) {
	        System.out.println("Unable to undo: " + cue);
	        cue.printStackTrace();
	         }
	        if(!undo.canUndo())
	         Undo.setEnabled(false);
	       }
	 }
	
	void delete(){                          //删除
	//用空格代替选定的文本实现删除
	 text.replaceRange("", text.getSelectionStart(), text.getSelectionEnd());
	 }
	
	void find(){
	JDialog ds = new JDialog(this, "查找", true);
	  ds.getContentPane().setLayout(new FlowLayout());
	  ds.setResizable(false);
	  final JLabel dsMessage1 = new JLabel("   总数:  ");
	  final JLabel dsMessage2 = new JLabel(" 0");
	  final Checkbox dsLoop = new Checkbox("循环   ");
	  dsLoop.setState(findingLoop);
	  final Checkbox dsMatchCase = new Checkbox("大小写匹配  ");
	  final TextField tfs = new TextField(15);
	  ds.getContentPane().add(tfs);
	  tfs.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
	      int a = 0, b = 0;
	      String str1, str2, str3, str4, strA, strB;
	      str1 = text.getText();          //得到文本域内容
	      str2 = str1.toLowerCase();      //转化为小写
	      str3 = tfs.getText();           //得到查找文本框的内容
	      str4 = str3.toLowerCase();      //转化为小写
	      if(dsMatchCase.getState()) {
	        strA = str1;
	        strB = str3;
	      }
	      else {
	        strA = str2;
	        strB = str4;
	      }
	      a = strA.indexOf(strB, FindStartPos); //得到起始位置
	      if(a > -1) {
	        text.setCaretPosition(a);
	        b = tfs.getText().length();        //得到查找字符串长度
	        text.select(a, a + b);             //选择查找到的字符串
	        FindStartPos = a + b;
	        foundCount++;
	        dsMessage2.setText(foundCount + "");
	      }
	      else {
	        if(dsLoop.getState()) {
	          JOptionPane.showMessageDialog(null, "已到文件结尾!", 
	          "Find result", JOptionPane.INFORMATION_MESSAGE);
	          FindStartPos = 0;
	        }
	        else {
	          JOptionPane.showMessageDialog(null, "已到文件结尾!", 
	          "Find result", JOptionPane.INFORMATION_MESSAGE);
	        }
	        foundCount = 0;
	       }
	     }
	   });
	   
	  Button bs = new Button("  查找  ");
	  bs.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
	      int a = 0, b = 0;
	      String str1, str2, str3, str4, strA, strB;
	      str1 = text.getText();          //得到文本域内容
	      str2 = str1.toLowerCase();      //转化为小写
	      str3 = tfs.getText();           //得到查找文本框的内容
	      str4 = str3.toLowerCase();      //转化为小写
	      if(dsMatchCase.getState()) {
	        strA = str1;
	        strB = str3;
	      }
	      else {
	        strA = str2;
	        strB = str4;
	      }
	      a = strA.indexOf(strB, FindStartPos); //得到起始位置
	      if(a > -1) {
	        text.setCaretPosition(a);
	        b = tfs.getText().length();        //得到查找字符串长度
	        text.select(a, a + b);             //选择查找到的字符串
	        FindStartPos = a + b;
	        foundCount++;
	        dsMessage2.setText(foundCount + "");
	      }
	      else {
	        if(dsLoop.getState()) {
	          JOptionPane.showMessageDialog(null, "已到文件结尾!", 
	          "Find result", JOptionPane.INFORMATION_MESSAGE);
	          FindStartPos = 0;
	        }
	        else {
	          JOptionPane.showMessageDialog(null, "已到文件结尾!", 
	          "Find result", JOptionPane.INFORMATION_MESSAGE);
	        }
	        foundCount = 0;
	       }
	     }
	   });
	   
	  Button bsc = new Button("Cancel");
	  bsc.addActionListener(new ActionListener() {
	                          public void actionPerformed(ActionEvent e) {
	                            dispose();
	                            foundCount = 0;
	                          }
	                       });
	  ds.getContentPane().add(bs);
	  ds.getContentPane().add(bsc);
	  ds.getContentPane().add(dsLoop);
	  ds.getContentPane().add(dsMatchCase);
	  ds.getContentPane().add(dsMessage1);
	  ds.getContentPane().add(dsMessage2);
	  ds.setLocation(120, 120);
	  ds.addWindowListener(new WindowAdapter() {
	                         public void windowClosing(WindowEvent e) {
	                           dispose();
	                           FindStartPos = 0;
	                         }
	                       });
	  ds.setSize(300,110);
	  Dimension  dsSize  =  ds.getSize();        //使窗体居中
	  int x3 = (sSize.width - dsSize.width)/2;
	  int y3 = (sSize.height - dsSize.height)/2;
	  ds.setLocation(x3, y3);
	  ds.setVisible(true);
	}	
	     
	void fontcolor() {
	  //使用颜色对话框来设置前景色(字体颜色)
	  //用text.getForeground()得到当前文本域中使用的前景色
	  fcolor = JColorChooser.showDialog(this,"设置字体颜色",text.getForeground());
	  if (fcolor != null) {
	    text.setForeground(fcolor);          //设置文本颜色为选定的颜色
	    }
	 this.repaint();                        //重绘界面(该语句可无)
	}
	
	void backcolor() {
	  //使用颜色对话框来设置背景颜色
	  //用text.getBackground()得到当前文本域中使用的背景色
	  bcolor = JColorChooser.showDialog(this,"设置背景颜色",text.getBackground());
	    if (bcolor != null) {
	    text.setBackground(bcolor);        //设置背景颜色为选定的颜色
	     }
	   this.repaint();                     //重绘界面(该语句可无)
	}
	
	void about() {                          //关于对话框
	//创建关于对话框并设定标题
	 JDialog about = new JDialog(this, "欢迎您使用记事本", true);
	  //设定对话框的布局
	  about.getContentPane().setLayout(new BorderLayout());
	  //创建一个按钮用来关闭对话框
	  JButton Ok = new JButton(new ImageIcon("images/ok.gif"));
	  Ok.setMnemonic(KeyEvent.VK_F4);     //设置按钮快捷键
	  Ok.addActionListener(new ActionListener() {        //关闭窗口
	                          public void actionPerformed(ActionEvent Oke) {
	                            dispose();
	                          }
	                        });
	
	  //创建图片标签,显示在关于对话框中
	  JLabel top1 = new JLabel(Icontop1);
	  //在窗口中加入标签和按钮并指定布局
	  about.getContentPane().add("North",top1);
	  about.getContentPane().add("South",Ok);
	  Ok.addActionListener(this);
	  about.addWindowListener(new WindowAdapter() {     //关闭窗口
	                         public void windowClosing(WindowEvent dle) {
	                           dispose();
	                         }
	                       });
	                       
	  about.setSize(400, 400);       //设置窗体大小
	  //以下语句使窗体居中显示
	  Dimension  aboutSize  =  about.getSize();
	  int x2 = (sSize.width - aboutSize.width)/2;
	  int y2 = (sSize.height - aboutSize.height)/2;
	  about.setLocation(x2, y2);
	  about.setResizable(false);     //设置窗体大小不可改变
	  about.setVisible(true);        //显示窗体  
	} 
	
	class UndoHandler implements UndoableEditListener {
	public void undoableEditHappened(UndoableEditEvent uee) {
	  undo.addEdit(uee.getEdit());
	}   
	}
	}

⌨️ 快捷键说明

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