📄 jfinddialog.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package jnotepad;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.TitledBorder;/** * * @author Administrator */class JFindDialog extends JDialog implements ActionListener { public JFindDialog(Frame owner, JTextArea area) { super(owner, "查找", false); this.area = area; //this.setLayout(new FlowLayout()); this.setLayout(null); label.setBounds(10, 10, 80, 25); txtfld.setBounds(90, 10, 250, 25); upper_lower.setBounds(10, 70, 140, 25); okbn.setBounds(345, 10, 120, 25); cancelbn.setBounds(345, 50, 120, 25); radiopanel.setBounds(150,45,150,90); group.add(up); group.add(down); radiopanel.add(up); radiopanel.add(down); radiopanel.setBorder(new TitledBorder("方向")); this.add(label); this.add(txtfld); this.add(okbn); this.add(upper_lower); this.add(radiopanel); this.add(cancelbn); this.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { JFindDialog.this.dispose();//!!!!!!!! } }); //txtfld.addTextListener(); txtfld.addKeyListener(new KeyAdapter() { public void keyReleased(KeyEvent e) { String str = txtfld.getText(); if(!str.equals("")) okbn.setEnabled(true); else okbn.setEnabled(false); } }); okbn.addActionListener(this); okbn.setSelected(true); okbn.setEnabled(false); cancelbn.addActionListener(this); up.addActionListener(this); down.addActionListener(this); down.setSelected(true); upper_lower.addActionListener(this); this.setResizable(false); this.setBounds(500, 300, 500, 180); this.setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == okbn) { findNext(); } if (e.getSource() == cancelbn) { JFindDialog.this.dispose(); } if (e.getSource() == up) { finddown = false; } if (e.getSource() == down) { finddown = true; } if (e.getSource() == upper_lower) { if (teller) { teller = false; } else { teller = true; } } } private void findNext() { String obj = txtfld.getText(); String all = area.getText(); int len = obj.length(); int end = all.length(); int start = area.getSelectionEnd();//从光标位置开始查找字符串 if (finddown) { nUp = false; for (; start <= end - len; start++) { if (teller) { if (all.substring(start, start + len).equals(obj)) {//若找到查找字符串 area.setSelectionStart(start); //选中并显示字符串 area.setSelectionEnd(start + len); return; //退出方法 } } else if ( all.substring(start, start + len).toLowerCase(). equals (obj.toLowerCase())) {//若找到查找字符串 area.setSelectionStart(start); //选中并显示字符串 area.setSelectionEnd(start + len); return; //退出方法 } } } else { //area.setSelectionEnd(start-); if (nUp) { start -= len; } for (; start >= len; start--) { if (teller) { if (all.substring(start - len, start).equals(obj)) {//若找到查找字符串 area.setSelectionStart(start - len); //选中并显示字符串 area.setSelectionEnd(start); nUp = true; return; //退出方法 } } else if ( all.substring(start - len, start).toLowerCase(). equals(obj.toLowerCase())) {//若找到查找字符串 area.setSelectionStart(start - len); //选中并显示字符串 area.setSelectionEnd(start); nUp = true; return; //退出方法 } } } JOptionPane.showMessageDialog(this, " 找不到“" + obj + "”", "记事本", JOptionPane.INFORMATION_MESSAGE); } private JLabel label = new JLabel("查找内容(N):"); private JTextField txtfld = new JTextField(24); private JButton okbn = new JButton("查找下一个(F)"); private JButton cancelbn = new JButton("取 消"); private JRadioButton up = new JRadioButton("向上(U)"); private JRadioButton down = new JRadioButton("向下(D)"); private JCheckBox upper_lower = new JCheckBox("是否区分大小写(C)"); private ButtonGroup group = new ButtonGroup(); private JPanel radiopanel = new JPanel(); JTextArea area = new JTextArea(); boolean finddown = true; boolean nUp = false; boolean teller = false;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -