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

📄 notepad.java

📁 多功能编辑器(java)(1) 能够实现基本的文字录入及发布; (2) 能够对录入文字进行字体
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      copyItem.setMnemonic( 'C' );
      editMenu.add( copyItem );
      copyItem.addActionListener(
       new ActionListener(){
        public void actionPerformed( ActionEvent event ){
         selectText = displayText.getSelectedText();//获得选中的内容,并保存在selectText里
        }
       }
      );
      
      //粘贴的实现
      JMenuItem pasteItem = new JMenuItem( "粘贴(P)" );
      pasteItem.setMnemonic( 'P' );
      editMenu.add( pasteItem );
      pasteItem.addActionListener(
       new ActionListener(){
        public void actionPerformed( ActionEvent event ){
         //int position = displayText.getCaretPosition();//获得鼠标当前位置
         
         SimpleAttributeSet attrset = new SimpleAttributeSet();
 		
        insert(selectText, attrset,doc);
         
        }
       }
      );
      
      
     
      JMenuItem swapItem = new JMenuItem( "替换(R)..." );
      swapItem.setMnemonic( 'R' );
      editMenu.add( swapItem );
      swapItem.addActionListener(
       new ActionListener(){
        public void actionPerformed( ActionEvent event ){
         JPanel swapPanel = new JPanel();
         JLabel lookupLabel = new JLabel("要替换的内容");
         JTextField inputText = new JTextField(10);
         JLabel swapLabel = new JLabel("替换为:");
         JTextField changetoText = new JTextField(10);
         swapPanel.add( lookupLabel );
         swapPanel.add( inputText );
         swapPanel.add( swapLabel );
         swapPanel.add( changetoText );
         JOptionPane.showMessageDialog(null,swapPanel);
         String text = displayText.getText();//获得整个文本内容
         String changeText = text.replace(inputText.getText(),changetoText.getText());//获得替换后的内容
         displayText.setText(changeText);
         
        }
       }
      );
      
      bar.add( editMenu );//add editMenu
      displayText = new JTextPane();

      // create Format menu, its submenus and menu items
      JMenu formatMenu = new JMenu( "格式(R)" );  
      formatMenu.setMnemonic( 'R' );
   
  

      // create Color submenu
      

      JMenu colorMenu = new JMenu( "Color" );
      colorMenu.setMnemonic( 'C' );

      colorItems = new JRadioButtonMenuItem[ colors.length ];
      colorGroup = new ButtonGroup();
      ItemHandler itemHandler = new ItemHandler();

      // create color radio button menu items
      for ( int count = 0; count < colors.length; count++ ) {
         colorItems[ count ] = 
            new JRadioButtonMenuItem( colors[ count ] );
         colorMenu.add( colorItems[ count ] );
         colorGroup.add( colorItems[ count ] );
         colorItems[ count ].addActionListener( itemHandler );
      }

      // select first Color menu item
      colorItems[ 0 ].setSelected( true );  

      // add format menu to menu bar
      formatMenu.add( colorMenu );
      formatMenu.addSeparator();

      // create Font submenu
      JMenu fontMenu = new JMenu( "Font" );
      fontMenu.setMnemonic( 'n' );

      fonts = new JRadioButtonMenuItem[ fontNames.length ];
      fontGroup = new ButtonGroup();

      // create Font radio button menu items
      for ( int count = 0; count < fonts.length; count++ ) {
         fonts[ count ] = new JRadioButtonMenuItem( fontNames[ count ] );
         fontMenu.add( fonts[ count ] );
         fontGroup.add( fonts[ count ] );
         fonts[ count ].addActionListener( itemHandler );
      }

      // select first Font menu item
      fonts[ 0 ].setSelected( true );

      fontMenu.addSeparator();

      // set up style menu items
      

      styleItems = new JCheckBoxMenuItem[ styleNames.length ];


      // create style checkbox menu items
      for ( int count = 0; count < styleNames.length; count++ ) {
         styleItems[ count ] = 
            new JCheckBoxMenuItem( styleNames[ count ] );
         fontMenu.add( styleItems[ count ] );
         StyleHandler styleHandler = new StyleHandler();
         styleItems[ count ].addItemListener( styleHandler );
      }

      // put Font menu in Format menu
      formatMenu.add( fontMenu );

      // add Format menu to menu bar
      bar.add( formatMenu );

      JMenu helpMenu = new JMenu( "帮助(H)" );  
      helpMenu.setMnemonic( 'H' );
   
   //帮助菜单项
      JMenuItem helpItem = new JMenuItem( "帮助主题(H)..." );
      helpItem.setMnemonic( 'H' );
      helpMenu.add( helpItem );
      helpItem.addActionListener(
       new ActionListener(){
        public void actionPerformed( ActionEvent event ){
         JTextArea helpText = new JTextArea(
           "格式里字体等的设置\n"+
           "复制,粘贴可以实现插入到鼠标当前位置\n");
         JScrollPane scroller = new JScrollPane(helpText);
         JOptionPane.showMessageDialog(null,scroller);
        }
       }
      );
      bar.add( helpMenu ); //添加
     
      // set up label to display text
     
      displayText.setForeground( colorvalues[ 0 ] );
      displayText.setFont( new Font( "Serif", Font.PLAIN, 24 ) );//设置默认字体
     
      scroll = new JScrollPane( box,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS );
     doc = displayText.getStyledDocument(); // 获得JTextPane的Document
      
     box_1.add(new JLabel("字体:")); // 加入标签
	  box_1.add(fontBox); // 加入组件
	  box_1.add(Box.createHorizontalStrut(8)); // 间距
	  box_1.add(new JLabel("样式:"));
	  box_1.add(styleBox);
	  box_1.add(Box.createHorizontalStrut(8));
	  box_1.add(new JLabel("字号:"));
	  box_1.add(sizeBox);
	  box_1.add(Box.createHorizontalStrut(8));
	  box_1.add(new JLabel("颜色:"));
	  box_1.add(fontColor);
	  box_1.add(Box.createHorizontalStrut(8));
	  box_1.add(new JLabel("背景:"));
	  box_1.add(fontBackColor);
	  box_1.add(Box.createHorizontalStrut(8));
	  box_1.add(b_icon);
	  box_2.add(displayText);
	  box_1.add(Box.createHorizontalStrut(8));
	  box_1.add(b_insert);
	  
	  box_1.add(Box.createHorizontalStrut(8));
	  box_1.add(b_link);

	 this.getContentPane().add(scrollPane,BorderLayout.CENTER);
	  this.getContentPane().add(box, BorderLayout.SOUTH);
	
	  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
	  this.setLocationRelativeTo(null);
	 
     
      setSize( 800, 500 );
      setVisible( true );

   } 


   public static void main( String args[] )
   {
      Notepad application = new Notepad();
      
   
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   }
   
   
   public void insert(String str, AttributeSet attrset,Document doc){

 		
 		str = str + "\n";
 		try{
 		  doc.insertString(doc.getLength(), str, attrset);
 		}catch(BadLocationException ble){
 		  System.out.println("BadLocationException: "+ble);
 		}	
 	}//end of insert()
   /**
    * 插入图片
    * 
    * 
    * @param icon
    */
 
   
   private void insertIcon(File file) {
 	  displayText.setCaretPosition(doc.getLength()); // 设置插入位置
 	  

displayText.insertIcon(new ImageIcon(file.getPath())); // 插入图片
    
 	  insert(" ",new SimpleAttributeSet(),doc); // 这样做可以换行
   }

   

   // inner class to handle action events from menu items
   private class ItemHandler implements ActionListener {

      // process color and font selections
      public void actionPerformed( ActionEvent event )
      {
         // process color selection
         for ( int count = 0; count < colorItems.length; count++ )
  
            if ( colorItems[ count ].isSelected() ) {
               displayText.setForeground( colorvalues[ count ] );
               break;
            }
 
         // process font selection
         for ( int count = 0; count < fonts.length; count++ )

            if ( event.getSource() == fonts[ count ] ) {
               displayText.setFont( 
                  new Font( fonts[ count ].getText(), style, 72 ) );
               break;
            }

         repaint();  

      } // end method actionPerformed

   } // end class ItemHandler

   // inner class to handle item events from check box menu ite
   private class StyleHandler implements ItemListener {

      // process font style selections
      public void itemStateChanged( ItemEvent e )
      {
         style = 0;

         // check for bold selection
         if ( styleItems[ 0 ].isSelected() )
            style += Font.BOLD;

         // check for italic selection
         if ( styleItems[ 1 ].isSelected() )
            style += Font.ITALIC;

         displayText.setFont( 
            new Font( displayText.getFont().getName(), style, 72 ) );

         repaint();
      }

   } // end class StyleHandler
   


} // end class Notepad 
 
 


 

  
 

⌨️ 快捷键说明

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