📄 finddialog.java
字号:
/*
* @author yeshaowei ysw19831983@163.com
*/
package com.cownew.JDBMonitor.listenerImpl.sckListenerClient;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import com.cownew.JDBMonitor.listenerImpl.uicommon.JCownewDialog;
public class FindDialog extends JCownewDialog
{
private static final long serialVersionUID = -5174218961507366106L;
private static final int COMPONENTDEFAULTHEIGHT = 20;
private static FindDialog instance;
private JTextArea textAreaToFind;
private JLabel findLabel;
private JTextField txtFind;
private JButton btnFindNext, btnCancel;
private JCheckBox checkboxCaseSensitive;
private JPanel directionPanel;
private JRadioButton rbBackward, rbForward;
private ButtonGroup btnGroupDirection;
private StringBuffer findString;
public static FindDialog getFindDialog(JFrame owner, JTextArea textArea)
{
if(instance==null)
{
instance = new FindDialog(owner,textArea);
}
return instance;
}
protected void initUI()
{
super.initUI();
container.setLayout(null);
findLabel = new JLabel( "Text to find:" );
findLabel.setBounds(10, 20, 80, COMPONENTDEFAULTHEIGHT);
container.add( findLabel );
txtFind = new JTextField();
txtFind.setBounds(130, 20, 130, COMPONENTDEFAULTHEIGHT);
txtFind.getDocument().addDocumentListener(
new DocumentListener()
{
public void changedUpdate( DocumentEvent e )
{
}
public void insertUpdate( DocumentEvent e )
{
btnFindNext.setEnabled( true );
}
public void removeUpdate( DocumentEvent e )
{
if( txtFind.getText().equals( "" ) )
btnFindNext.setEnabled( false );
}
}
);
container.add( txtFind );
btnFindNext = new JButton( "Next" );
getRootPane().setDefaultButton(btnFindNext);
btnFindNext.setBounds(280, 20, 100, COMPONENTDEFAULTHEIGHT);
btnFindNext.setEnabled( !txtFind.getText().equals( "" ) );
btnFindNext.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
findString.delete( 0, findString.length() );
findString.append( txtFind.getText() );
find();
}
}
);
container.add( btnFindNext );
checkboxCaseSensitive = new JCheckBox( "Case Sensitive" );
checkboxCaseSensitive.setBounds(10, 70, 120, COMPONENTDEFAULTHEIGHT);
container.add( checkboxCaseSensitive );
directionPanel = new JPanel();
directionPanel.setLayout(new GridLayout(2,1));
directionPanel.setBounds(130, 50, 130, 60);
directionPanel.setBorder( new TitledBorder( BorderFactory.createLineBorder( Color.BLACK ), "Direction" ) );
rbBackward = new JRadioButton( "Backward" );
directionPanel.add( rbBackward );
rbForward = new JRadioButton( "Forward" );
rbForward.setSelected( true );
directionPanel.add( rbForward );
btnGroupDirection = new ButtonGroup();
btnGroupDirection.add( rbBackward );
btnGroupDirection.add( rbForward );
container.add( directionPanel );
btnCancel = new JButton( "Cancel" );
btnCancel.setBounds(280, 60, 100, COMPONENTDEFAULTHEIGHT);
btnCancel.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
cancel();
}
}
);
container.add( btnCancel );
setSize( 400, 150 );
setResizable( false );
}
private FindDialog( JFrame owner, JTextArea textArea)
{
super( owner,false);
setTitle("Find");
textAreaToFind = textArea;
findString = new StringBuffer(textArea.getText());
}
public void find()
{
int x = textAreaToFind.getCaretPosition();
if( textAreaToFind.getSelectedText() != null &&
textAreaToFind.getSelectedText().toLowerCase().equals( findString.toString().toLowerCase() )
&& rbBackward.isSelected() )
x--;
String tString = textAreaToFind.getText();
String fString = findString.toString();
if( ! checkboxCaseSensitive.isSelected() )
{
tString = tString.toLowerCase();
fString = fString.toLowerCase();
}
if( rbForward.isSelected() )
{
x = tString.indexOf( fString, x );
}
else
{
String str = tString.substring( 0, x );
x = str.lastIndexOf( fString, x - fString.length() );
}
if( x == -1 )
{
JOptionPane.showMessageDialog( getOwner(), "\"" + fString + "\" not found!" );
}
else
{
textAreaToFind.select( x, x + fString.length() );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -