📄 finddlg.java
字号:
package notepad;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FindDlg extends JDialog implements ActionListener, ItemListener {
// GUI components
JLabel label;
JTextField key;
JCheckBox isIgnoreCase;
JRadioButton up, down;
ButtonGroup group;
JButton btn_findNext, btn_cancel;
JPanel panel1, panel2, panel3;
// 成员变量
boolean ignoreCase = true; // 查找时是否忽略大小写,默认 忽略
String findKey = ""; // 输入的要查找的字符串
boolean isUp; // 查找方向是否向上,否则就是向下,默认 向下查找
// "父窗口"
NotePad notePad;
public FindDlg(NotePad initNotePad) {
setTitle("查找");
notePad = initNotePad;
notePad.mileStone = 0;
notePad.isFirstCall = true;
Container con = getContentPane();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
label = new JLabel("查找内容(N):");
key = new JTextField(20);
panel1.setLayout(new FlowLayout());
panel1.add(label);
panel1.add(key);
isIgnoreCase = new JCheckBox("区分大小写(C)");
up = new JRadioButton("向上查找(U)", false);
down = new JRadioButton("向下查找(D)", true);
group = new ButtonGroup();
group.add(up);
group.add(down);
isIgnoreCase.addItemListener(this);
up.addItemListener(this);
down.addItemListener(this);
panel2.add(isIgnoreCase);
panel2.add(up);
panel2.add(down);
btn_findNext = new JButton("查找下一个(N)");
btn_cancel = new JButton("取消");
btn_findNext.addActionListener(this);
btn_cancel.addActionListener(this);
panel3.add(btn_findNext);
panel3.add(btn_cancel);
setLayout(new BorderLayout());
con.add(panel1, BorderLayout.NORTH);
con.add(panel2, BorderLayout.CENTER);
con.add(panel3, BorderLayout.SOUTH);
// setBounds(100,100,400,300);
setSize(400, 140);
setVisible(false);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == btn_findNext) {
findKey = key.getText();
// 调用搜索函数来搜索整个工作区中的文字
if (!notePad.OnFindMenuItem(findKey, ignoreCase, isUp)) {
JOptionPane.showMessageDialog(null, "找不到关键字 \"" + findKey
+ "\"");
}
}
if (e.getSource() == btn_cancel) {
this.setVisible(false);
this.dispose();
}
}
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == isIgnoreCase) {
ignoreCase = isIgnoreCase.isSelected() ? false : true;
}
if (e.getSource() == up) {
isUp = up.isSelected() ? true : false;
}
if (e.getSource() == down) {
isUp = down.isSelected() ? false : true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -