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

📄 find.java

📁 简单的java文本编辑器
💻 JAVA
字号:
/*************************************************************************************
* 类名: Find                                                               
*                                                                    
* 功能: 此类为“查找对话框”实现类
* 
* 注意事项:此类没有实现“查找对话框”中“统计总数”按钮的功能
*                                                                
**************************************************************************************/
package edit.com;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class Find extends JPanel
{
	private JPanel centerPanel;
	private JLabel findLabel;
	private JLabel titleLabel;
	private JLabel matchLabel;
	private JLabel loopLabel;
	private JComboBox findText;//用户需要查找的文字
	private JCheckBox matchBox;//“完全匹配模式”复选按钮
	private JCheckBox loopBox;//“循环查找”复选按钮
	private JButton[] buttons = new JButton[3];//“确认”/“取消”/“统计”按钮
	private JDialog dialog;
	private String sourceString;//编辑器中“用户输入区”中所有的文字内容
	private FindSet findSet = new FindSet();//用来存储关于查找位置的数据
	private FindSet saveSet = new FindSet();//用来存储上一次查找位置的数据
    private FindState findState = new FindState();//用来存储此类的一些状态
    private FindTools tool = new FindTool();//工具类

	public Find()
	{
		setLayout( new BorderLayout() );

		centerPanel = new JPanel();
		addToCenterPanel( centerPanel );

	}

    public void setString( String s )
    {
    	sourceString = s;
    }

    public boolean showDialog( Component c , String s )
    {
    	findState.setFlag( false );

    	Frame owner = null;

    	if( c instanceof Frame )
    	{
    		owner = ( Frame )c;
    	}
    	else
    	{
    		owner = ( Frame )SwingUtilities.getAncestorOfClass( Frame.class , c );
    	}

    	if( dialog == null || dialog.getOwner() != owner )
    	{
    		owner = null;
    		dialog = new JDialog( owner , true );
    		dialog.getContentPane().add( this );
			dialog.setResizable( false );
			dialog.setSize( 400 , 180 );
    	}

    	dialog.setTitle( s );
		dialog.show();

		return findState.getFlag();
	}

	public FindSet getFindSet()  // this is a potential error , may be return a cloned object !!!!
	{
		return findSet;
	}

    //查找用户所定义的文字
    public void find()
    {
    	int cartPosition = 0;
    	int aLength = 0;
        String newString = null;

        tool.addItems( findText );
    	//得到用户所定义的需要查找的文字
    	String text = ( String )findText.getSelectedItem();

    	//“完全匹配模式”没有被选中时,忽略大小写
    	if( !matchBox.isSelected() )
    	{
    		newString = sourceString.toLowerCase();
    		text = text.toLowerCase();
    	}
    	
    	//找到待查文本内容中与查找文本匹配的位置
    	cartPosition = sourceString.indexOf( text , findState.getFindPosition() );

    	if( cartPosition > -1 )
    	{
    		aLength = ( ( String )findText.getSelectedItem() ).length();

    		//存储本次查找的位置
    		if( findState.getFindPosition() == 0 )
    		{
    			saveSet.setcartPoint( cartPosition );
    			saveSet.setLength( aLength );
    		}

    		findSet.setcartPoint( cartPosition );
    		findSet.setLength( aLength );

	        findState.setFindPosition( cartPosition + aLength );

	        findState.setIsFind( true );
		}
		else
		{
			//“循环查找“复选框被选中,设置初始位置为”0“,进行下一次查找
			if( loopBox.isSelected() && findState.getIsFind() )
			{
				findState.setFindPosition( 0 );

			    findSet.setcartPoint( saveSet.getcartPoint() );
    			findSet.setLength( saveSet.getLength() );
			}
			else
			{
				JOptionPane.showMessageDialog( Find.this , "End of file" , "no file found" ,
							JOptionPane.INFORMATION_MESSAGE );
                findSet.setcartPoint( -1 );
            }
		}
    }

//private function add private class defined
    private void addToCenterPanel( JPanel aPanel )
    {
    	aPanel.setLayout( new GridBagLayout() );

    	GridBagConstraints constraint = new GridBagConstraints();

    	findLabel = tool.createLabel( "Find : " ,
                                      new Font( null , Font.BOLD , 15 ) ,
                                      Color.blue );
    	titleLabel = tool.createLabel( "see help for meaning of special character" );
    	matchLabel = tool.createLabel( "Match Case" );
    	loopLabel = tool.createLabel( "Loop" );

    	ItemChange itemListener = new ItemChange();
    	findText = tool.createComboBox( itemListener );

    	matchBox = new JCheckBox();
    	loopBox = new JCheckBox();

    	OkListener okListener = new OkListener();
    	CancelListener cancelListener = new CancelListener();
    	buttons[0] = tool.createButton( "   O k   " , Color.orange , okListener );
    	buttons[1] = tool.createButton( " Cancel " , Color.orange , cancelListener );
    	buttons[2] = tool.createButton( " Count " , Color.orange , null );

    	Box temBox = tool.createVerticalBox( 6 );
    	Box temBox_2 = tool.createHorizontalBox( 12 );
    	Box buttonBox = tool.createVerticalBox( buttons );

    	tool.addToCenterWithConstraint( findLabel , constraint , aPanel, 0 , 0 , 1 , 1 , GridBagConstraints.NONE ,
    			GridBagConstraints.WEST );
    	tool.addToCenterWithConstraint( temBox , constraint , aPanel, 0 , 1 , 2 , 1 , GridBagConstraints.NONE ,
    			GridBagConstraints.WEST );
    	tool.addToCenterWithConstraint( titleLabel , constraint , aPanel, 0 , 2 , 2 , 1 , GridBagConstraints.NONE ,
    			GridBagConstraints.WEST );
    	tool.addToCenterWithConstraint( matchBox , constraint , aPanel, 0 , 3 , 1 , 1 , GridBagConstraints.NONE ,
    			GridBagConstraints.WEST );
    	tool.addToCenterWithConstraint( loopBox , constraint , aPanel, 0 , 4 , 1 , 1 , GridBagConstraints.NONE ,
    			GridBagConstraints.WEST );
    	tool.addToCenterWithConstraint( findText , constraint , aPanel, 1 , 0 , 1 , 1 , GridBagConstraints.HORIZONTAL ,
    			GridBagConstraints.CENTER );
    	tool.addToCenterWithConstraint( matchLabel ,constraint , aPanel, 1 , 3 , 1 , 1 , GridBagConstraints.NONE ,
    			GridBagConstraints.WEST );
    	tool.addToCenterWithConstraint( loopLabel , constraint , aPanel, 1 , 4 , 1 , 1 , GridBagConstraints.NONE ,
    			GridBagConstraints.WEST );
        tool.addToCenterWithConstraint( temBox_2 , constraint , aPanel, 2 , 0 , 1 , 1 , GridBagConstraints.NONE ,
    			GridBagConstraints.WEST );
   		tool.addToCenterWithConstraint( buttonBox , constraint , aPanel, 3 , 0 , 1 , 4 , GridBagConstraints.NONE ,
    			GridBagConstraints.WEST );

   		add( aPanel , BorderLayout.CENTER );

   }

	//private class defined 	
	private class ItemChange implements ItemListener
	{
		public void itemStateChanged( ItemEvent i )
		{
			findState.setFindPosition( 0 );
			findState.setIsFind( false );
		}
	}

    private class OkListener implements ActionListener
    {
    	public void actionPerformed( ActionEvent a )
    	{
    			findState.setFlag( true );
    			find();

    			dialog.setVisible( false );
    	}
    }

	private class CancelListener implements ActionListener
	{
		public void actionPerformed( ActionEvent a )
		{
			findState.setFindPosition( 0 );
			findState.setIsFind( false );
			tool.addItems( findText );

		    dialog.setVisible( false );
		}
	}


}


⌨️ 快捷键说明

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