?? finddialog.java
字號:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
public class FindDialog extends JDialog
{
private Container pane;
private JTextArea text;
private JFrame owner;
private JLabel findLabel;
private JTextField findText;
private JButton findNext, cancel;
private JCheckBox upperLowerCase;
private JPanel upDownPanel;
private JRadioButton upButton, downButton;
private ButtonGroup upDownGroup;
private StringBuffer findString;
public FindDialog( JFrame frame, JTextArea textArea, StringBuffer string )
{
super( frame, "查找" );
owner = frame;
text = textArea;
findString = string;
pane = getContentPane();
pane.setLayout( null );
findLabel = new JLabel( "查找內容:" );
findLabel.setBounds(18, 20, 70, 21);
pane.add( findLabel );
findText = new JTextField( findString.toString() );
findText.setBounds(94, 20, 147, 21);
findText.getDocument().addDocumentListener(
new DocumentListener()
{
public void changedUpdate( DocumentEvent e )
{
}
public void insertUpdate( DocumentEvent e )
{
findNext.setEnabled( true );
}
public void removeUpdate( DocumentEvent e )
{
if( findText.getText().equals( "" ) )
findNext.setEnabled( false );
}
}
);
pane.add( findText );
findNext = new JButton( "查找下一個" );
findNext.setBounds(262, 20, 107, 21);
findNext.setEnabled( !findText.getText().equals( "" ) );
findNext.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
findString.delete( 0, findString.length() );
findString.append( findText.getText() );
find();
}
}
);
pane.add( findNext );
upperLowerCase = new JCheckBox( "區分大小寫" );
upperLowerCase.setBounds(18, 82, 93, 21);
pane.add( upperLowerCase );
upDownPanel = new JPanel();
upDownPanel.setBounds(118, 48, 122, 55);
TitledBorder tb = new TitledBorder( BorderFactory.createLineBorder( Color.BLACK ), "方向" );
upDownPanel.setBorder( tb );
upButton = new JRadioButton( "向上" );
upDownPanel.add( upButton );
downButton = new JRadioButton( "向下" );
downButton.setSelected( true );
upDownPanel.add( downButton );
upDownGroup = new ButtonGroup();
upDownGroup.add( upButton );
upDownGroup.add( downButton );
pane.add( upDownPanel );
cancel = new JButton( "取消" );
cancel.setBounds(262, 57, 107, 21);
cancel.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
dispose();
}
}
);
pane.add( cancel );
setSize( 395, 147 );
int x = ( int )( owner.getLocation().getX() + owner.getWidth() / 2 -200 );
x = ( x > 624 ? 624 : x );
x = ( x < 0 ? 0 : x );
int y = ( int )( owner.getLocation().getY() + owner.getHeight() / 2 - 75 );
y = ( y > 620 ? 620 : y );
y = ( y < 0 ? 0 : y );
setLocation( x, y );
setResizable( false );
}
public void find()
{
int x = text.getCaretPosition();
if( text.getSelectedText() != null &&
text.getSelectedText().toLowerCase().equals( findString.toString().toLowerCase() )
&& upButton.isSelected() )
x--;
String tString = text.getText();
String fString = findString.toString();
if( ! upperLowerCase.isSelected() )
{
tString = tString.toLowerCase();
fString = fString.toLowerCase();
}
if( downButton.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( owner, "沒找到 \"" + fString + "\"" );
}
else
{
text.select( x, x + fString.length() );
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -