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

📄 jnotepadui.java

📁 一个java记事本无错源代码 一个java记事本无错源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
               public void actionPerformed(ActionEvent e){
                  closeFile();
                   }
                }
        );
        menuColor.addActionListener(
             new ActionListener(){
               public void actionPerformed(ActionEvent e){
                  color();
                   }
                }
        );
        menuCut.addActionListener(
             new ActionListener(){
               public void actionPerformed(ActionEvent e){
                  cut();
                   }
                }
        );
        
        menuCopy.addActionListener(
             new ActionListener(){
               public void actionPerformed(ActionEvent e){
                  copy();
                   }
                }
        );
         menuSelectAll.addActionListener(
             new ActionListener(){
               public void actionPerformed(ActionEvent e){
                  selectAll();
                   }
                }
        );
       menuFind.addActionListener(
             new ActionListener(){
               public void actionPerformed(ActionEvent e){
                  find();
                   }
                }
        );  
                 menuReplace.addActionListener(
             new ActionListener(){
               public void actionPerformed(ActionEvent e){
                  replace();
                   }
                }
        ); 
                 menuFindNext.addActionListener(
             new ActionListener(){
               public void actionPerformed(ActionEvent e){
                  findNext();
                   }
                }
        ); 
        menuCancel.addActionListener(
             new ActionListener(){
               public void actionPerformed(ActionEvent e){
                  setVisible(false);
                   }
                }
        ); 
        menuAbout.addActionListener(
             new ActionListener(){
               public void actionPerformed(ActionEvent e){
                   JOptionPane.showOptionDialog(null,
              "课程名称:\n    JNotePad\n",
              "关于JNotePad",
              JOptionPane.DEFAULT_OPTION,
              JOptionPane.INFORMATION_MESSAGE,
              null,null,null);

                   }
                }
        );
     textArea.addKeyListener(
         new KeyAdapter(){
         public void keyTyped(KeyEvent e){
          processTextArea();

         }
           }
       );
     textArea.addMouseListener(
         new MouseAdapter(){
         public void mouseReleased(MouseEvent e){
         
          if(e.getButton()==MouseEvent.BUTTON3)
            popUpMenu.show(editMenu,e.getX(),e.getY());}
        public void mouseClicked(MouseEvent e){ 
          if(e.getButton()==MouseEvent.BUTTON1)
              
              
            popUpMenu.setVisible(false);
}
 }
       );
       
    }
    private void openFile() {
  if(isCurrentFileSaved()){
  open();
  }
  else{
  int option = JOptionPane.showConfirmDialog(
          null,"文件已修改,是否保存?",
          "保存文件?",JOptionPane.YES_NO_OPTION,
          JOptionPane.WARNING_MESSAGE,null);
  switch(option){
      case JOptionPane.YES_OPTION:
      saveFile();
      break;
      case JOptionPane.NO_OPTION:
      open();
      break;
  }
  }
}
    private void open(){
    File file = null;
    GraphicsEnvironment getFont = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Font []fonts = getFont.getAllFonts();
   
    JFileChooser filechooser = new JFileChooser();
    JDialog about = new JDialog(this);
    JMenuBar menubar = new JMenuBar();
    JLabel stateBar = new JLabel();

int option = filechooser.showDialog(null,null);
if(option == JFileChooser.APPROVE_OPTION){
      try{
          BufferedReader buf =
                  new BufferedReader(
                  new FileReader(
                  filechooser.getSelectedFile()));
          setTitle(filechooser.getSelectedFile().toString());
          textArea.setText("");
          stateBar.setText("未修改");
          String LineSeparator = System.getProperty("line.separator");
          String text;
          while((text = buf.readLine()) != null){
              textArea.append(text);
              textArea.append(lineSeparator);
          }
          buf.close();
      }
      catch(IOException e){
          JOptionPane.showMessageDialog(null,e.toString(),
              "开启文件失败",JOptionPane.ERROR_MESSAGE);
          }
      }
}
    private boolean isCurrentFileSaved() {
        JLabel stateBar = new JLabel();
        if(stateBar.getText().equals("未修改")){
            return false;
        }
        else{
            return true;
        }
    }
    private void saveFile() {
File file = new File(getTitle());
if(!file.exists()){
    saveFileAs();
}
    else{
    try{
        BufferedWriter buf =
                new BufferedWriter(
                new FileWriter(file));
        buf.write(textArea.getText());
        buf.close();
        JLabel stateBar = new JLabel();
        stateBar.setText("未修改");
    }
          catch(IOException e){
              JOptionPane.showMessageDialog(null,e.toString(),
              "写入文件失败",JOptionPane.ERROR_MESSAGE);
          }
    
    }
}
    private void saveFileAs() {
    
    JFileChooser filechooser = new JFileChooser();
    int option = filechooser.showDialog(null,null);
    if(option == JFileChooser.APPROVE_OPTION){
    File file = filechooser.getSelectedFile();
    setTitle(file.toString());
    try{
        file.createNewFile();
        saveFile();
    }
              catch(IOException e){
              JOptionPane.showMessageDialog(null,e.toString(),
              "无法建立新文件",JOptionPane.ERROR_MESSAGE);
          }
}
}
    private void closeFile() {
if(isCurrentFileSaved()){
dispose();
}
else{
int option = JOptionPane.showConfirmDialog(
        null,"文件已经修改,是否保存?","保存文件?",JOptionPane.YES_NO_OPTION,
        JOptionPane.WARNING_MESSAGE,null);
switch (option){
    case JOptionPane.YES_OPTION:
         saveFile();
         break;
    case JOptionPane.NO_OPTION:
           dispose();
}
}
}
        private void color() {
        color=JColorChooser.showDialog(JNotePadUI.this,"",color);
        textArea.setForeground(color); 
        
}
    private void cut() {
    textArea.cut();
    JLabel stateBar = new JLabel();
    stateBar.setText("已修改");
    popUpMenu.setVisible(false);
        
}
private void copy() {
textArea.copy();
popUpMenu.setVisible(false);

}
private void paste() {
textArea.paste();
    JLabel stateBar = new JLabel();
stateBar.setText("已修改");
popUpMenu.setVisible(false);
 }
private void selectAll() {
textArea.selectAll();
    JLabel stateBar = new JLabel();
stateBar.setText("已修改");
popUpMenu.setVisible(false);
 }
private void replace() {
       strReplace = treplace.getText();
        textArea.replaceRange(strReplace, txtPlace, txtPlace + strFind.length());
  // JLabel stateBar = new JLabel();
//stateBar.setText("已修改");
//popUpMenu.setVisible(false);
}
private void find() {
        strFind = tfind.getText();
        strNext = 0; //开始位置
        txtPlace = textArea.getText().indexOf(strFind, strNext);
        txtLen = txtPlace + strFind.length();
        textArea.select(txtPlace, txtLen);
        strNext = textArea.getSelectionEnd(); //选中内容的最后位置
        if (txtPlace == -1) /*没有找到的处理*/ 
        {
            JOptionPane.showMessageDialog(null, "没你找到你需要的内容", "查找",
                    JOptionPane.INFORMATION_MESSAGE);
        }
        menuFindNext.setEnabled(true);
         //  JLabel stateBar = new JLabel();
//stateBar.setText("已修改");
//popUpMenu.setVisible(false);
 }
private void findNext() {
        menuFind.setEnabled(false);
        txtPlace = textArea.getText().indexOf(strFind, strNext);
        txtLen = txtPlace + strFind.length();
        textArea.select(txtPlace, txtLen);
        strNext = textArea.getSelectionEnd(); //获取查找下一个内容的最后位置
        if (txtPlace == -1) /*没有找到的处理*/ 
{
            JOptionPane.showMessageDialog(null, "没你找到你需要的内容", "查找下一个",
                    JOptionPane.INFORMATION_MESSAGE);
            strNext = 0; //没有找到初始化位置,以变重新查找
            menuFind.setText("");
            menuFindNext.setEnabled(false);
            menuReplace.setEnabled(false);
        }
 }
 

private void processTextArea() {
 JLabel stateBar = new JLabel();    
stateBar.setText("已修改");
}
    

    public static void main(String[] args) {
             new JNotePadUI();  
    }
}

⌨️ 快捷键说明

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