📄 notepad.java
字号:
String str1, str2, str3, str4, strA, strB;
str1 = text.getText();
str2 = str1.toLowerCase();
str3 = field1.getText();
str4 = str3.toLowerCase();
// "区分大小写"的CheckBox被选中
if (checkcase.isSelected()) {
strA = str1;
strB = str3;
} else {
strA = str2;
strB = str4;
}
// 如果选择向下查找
if (up.isSelected()) {
// 假如没有选择某一段范围,就是在全部文本里进行查找,下同
if (text.getSelectedText() == null) {
a = strA.lastIndexOf(strB, FindStartPos - 1);
}
// 有选择段范围的情况,选择范围内的最后一个,下同
else {
a = strA.lastIndexOf(strB, FindStartPos
- field1.getText().length() - 1);
}
}
// 如果选择向下查找
else if (down.isSelected()) {
if (text.getSelectedText() == null) {
a = strA.indexOf(strB, FindStartPos);
} else {
a = strA.indexOf(strB, FindStartPos
- field1.getText().length() + 1);
}
}
// 对查找到的字段进行选择
if (a > -1) {
if (up.isSelected()) {
text.setCaretPosition(a);
b = field1.getText().length();
text.select(a, a + b);
} else if (down.isSelected()) {
text.setCaretPosition(a);
b = field1.getText().length();
text.select(a, a + b);
}
} else {
JOptionPane.showMessageDialog(null, "对不起,查找不到指定内容!", "对不起",
JOptionPane.INFORMATION_MESSAGE);
}
}
});
// 取消按钮事件,把查找对话框关闭
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
searchDia.dispose();
}
});
searchDia.show();
}
/**
* 处理替换的方法
*/
public void replace() {
final JDialog replaceDia = new JDialog(this, "替换", true);
replaceDia.setLocationRelativeTo(this);
replaceDia.setSize(450, 250);
replaceDia.setModalityType(Dialog.ModalityType.MODELESS);
GridLayout gridlayout = new GridLayout(1, 2);
replaceDia.setLayout(gridlayout);
JPanel panel1 = new JPanel();
Box box1 = Box.createVerticalBox();
JPanel panel2 = new JPanel();
JLabel label1 = new JLabel("查找内容(N)");
final JTextField field1 = new JTextField(20);
panel2.add(label1);
panel2.add(field1);
JPanel panel3 = new JPanel();
JLabel label2 = new JLabel(" 替换为(P)");
final JTextField field2 = new JTextField(20);
final JCheckBox checkcase = new JCheckBox("区分大小写(C)");
ButtonGroup group = new ButtonGroup();
final JRadioButton up = new JRadioButton("向上(U)");
final JRadioButton down = new JRadioButton("向下(D)");
down.setSelected(true);
group.add(up);
group.add(down);
JPanel directionpanel = new JPanel();
directionpanel.setBorder(BorderFactory.createTitledBorder("方向"));
Box box = Box.createHorizontalBox();
box.add(up);
box.add(down);
directionpanel.add(box);
panel3.add(label2);
panel3.add(field2);
box1.add(panel2);
box1.add(panel3);
box1.add(directionpanel);
box1.add(checkcase);
panel1.add(box1);
JPanel panel4 = new JPanel();
Box box2 = Box.createVerticalBox();
JButton searchnextButton = new JButton("查找下一个(F)");
JButton replaceButton = new JButton(" 替换(R) ");
JButton replaceAllButton = new JButton(" 全部替换(A) ");
JButton cancelButton = new JButton(" 取消 ");
box2.add(searchnextButton);
box2.add(Box.createVerticalStrut(10));
box2.add(replaceButton);
box2.add(Box.createVerticalStrut(10));
box2.add(replaceAllButton);
box2.add(Box.createVerticalStrut(10));
box2.add(cancelButton);
box2.add(Box.createVerticalStrut(10));
panel4.add(box2);
replaceDia.add(box1);
replaceDia.add(box2);
searchnextButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int a = 0, b = 0;
int FindStartPos = text.getCaretPosition();
String str1, str2, str3, str4, strA, strB;
str1 = text.getText();
str2 = str1.toLowerCase();
str3 = field1.getText();
str4 = str3.toLowerCase();
// "区分大小写"的CheckBox被选中
if (checkcase.isSelected()) {
strA = str1;
strB = str3;
} else {
strA = str2;
strB = str4;
}
if (up.isSelected()) {
if (text.getSelectedText() == null) {
a = strA.lastIndexOf(strB, FindStartPos - 1);
} else {
a = strA.lastIndexOf(strB, FindStartPos
- field1.getText().length() - 1);
}
} else if (down.isSelected()) {
if (text.getSelectedText() == null) {
a = strA.indexOf(strB, FindStartPos);
} else {
a = strA.indexOf(strB, FindStartPos
- field1.getText().length() + 1);
}
}
if (a > -1) {
if (up.isSelected()) {
text.setCaretPosition(a);
b = field1.getText().length();
text.select(a, a + b);
} else if (down.isSelected()) {
text.setCaretPosition(a);
b = field1.getText().length();
text.select(a, a + b);
}
} else {
JOptionPane.showMessageDialog(null, "对不起,查找不到指定内容!", "对不起",
JOptionPane.INFORMATION_MESSAGE);
}
}
});
// 替换事件处理
replaceButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (text.getSelectedText() == null) {
int a = 0, b = 0;
int FindStartPos = text.getCaretPosition();
String str1, str2, str3, str4, strA, strB;
str1 = text.getText();
str2 = str1.toLowerCase();
str3 = field1.getText();
str4 = str3.toLowerCase();
// "区分大小写"的CheckBox被选中
if (checkcase.isSelected()) {
strA = str1;
strB = str3;
} else {
strA = str2;
strB = str4;
}
if (up.isSelected()) {
if (text.getSelectedText() == null) {
a = strA.lastIndexOf(strB, FindStartPos - 1);
} else {
a = strA.lastIndexOf(strB, FindStartPos
- field1.getText().length() - 1);
}
} else if (down.isSelected()) {
if (text.getSelectedText() == null) {
a = strA.indexOf(strB, FindStartPos);
} else {
a = strA.indexOf(strB, FindStartPos
- field1.getText().length() + 1);
}
}
if (a > -1) {
if (up.isSelected()) {
text.setCaretPosition(a);
b = field1.getText().length();
text.select(a, a + b);
} else if (down.isSelected()) {
text.setCaretPosition(a);
b = field1.getText().length();
text.select(a, a + b);
// 注意,此步是关键!!!将所选中的字段替换为所需内容
text.replaceSelection(field2.getText());
}
} else {
JOptionPane.showMessageDialog(null, "对不起,查找不到指定内容!",
"对不起", JOptionPane.INFORMATION_MESSAGE);
}
} else {
text.replaceSelection(field2.getText());
}
}
});
// 替换全部事件处理
replaceAllButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text.setCaretPosition(0);
int a = 0, b = 0, replaceCount = 0;
// 这里使用while就是为了将所有满足条件的字段进行替换
while (a > -1) {
int FindStartPos = text.getCaretPosition();
String str1, str2, str3, str4, strA, strB;
str1 = text.getText();
str2 = str1.toLowerCase();
str3 = field1.getText();
str4 = str3.toLowerCase();
if (checkcase.isSelected()) {
strA = str1;
strB = str3;
} else {
strA = str2;
strB = str4;
}
if (up.isSelected()) {
if (text.getSelectedText() == null) {
a = strA.lastIndexOf(strB, FindStartPos - 1);
} else {
a = strA.lastIndexOf(strB, FindStartPos
- field1.getText().length() - 1);
}
} else if (down.isSelected())
if (text.getSelectedText() == null) {
a = strA.indexOf(strB, FindStartPos);
} else {
a = strA.indexOf(strB, FindStartPos
- field1.getText().length() + 1);
}
if (a > -1) {
if (up.isSelected()) {
text.setCaretPosition(a);
b = field1.getText().length();
text.select(a, a + b);
} else if (down.isSelected()) {
text.setCaretPosition(a);
b = field1.getText().length();
text.select(a, a + b);
}
} else {
if (replaceCount == 0) {
JOptionPane.showMessageDialog(null,
"对不起,查找不到指定内容!", "对不起",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "替换成功!共进行"
+ replaceCount + "处替换", "替换成功",
JOptionPane.INFORMATION_MESSAGE);
}
}
if (field2.getText().length() == 0
&& text.getSelectedText() != null) {
text.replaceSelection("");
replaceCount++;
}
if (field2.getText().length() > 0
&& text.getSelectedText() != null) {
text.replaceSelection(field2.getText());
replaceCount++;
}
}// end while
}
});
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
replaceDia.dispose();
}
});
replaceDia.show();
}
/**
* 字体设置方法
*/
public void fontChooser() {
int index = 0;
final JDialog fontDia = new JDialog(this, "字体设置", true);
fontDia.setLocationRelativeTo(this);
fontDia.setModalityType(Dialog.ModalityType.MODELESS);
doc = text.getStyledDocument(); // 获得JTextPane的Document
fontDia.setSize(500, 400);
Container c = fontDia.getContentPane();
c.setLayout(new BorderLayout());
JButton ok = new JButton("确定");
ok.addActionListener(handler);
JButton cancel = new JButton("退出");
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fontDia.dispose();
}
});
// 调用系统支持的所有字体
String[] str_name = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getAvailableFontFamilyNames();
// 字体大小
String[] str_Size = { "8", "10", "12", "14", "16", "18", "20", "22",
"24", "26", "28", "30", "32", "34", "36", "38", "40" };
// 字体样式
String[] str_Style = { "常规", "斜体", "粗体", "粗斜体" };
fontName = new JList(str_name); // 字体名称
fontName.setVisibleRowCount(5);
fontName.setFixedCellHeight(15);
fontName.setFixedCellWidth(120);
// 以下这步一定要做,打开字体对话框的时候选中的是现在文本所用的字体,假如不做这步,会出现异常
for (int i = 0; i < str_name.length; i++) {
if (str_name[i].equals(text.getFont().getFontName())) {
index = i;
break;
}
}
fontName.setSelectedIndex(index);
final JTextField fontnamefield = new JTextField(20);
fontnamefield.setEditable(false);
fontnamefield.setText(fontName.getSelectedValue().toString());
fontName.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
// 为了使用户能更好的看预览的效果,将预览框里面的字设置成这个字体的名字,这样预览中就不会出现显示不了某些字的情况了)
// 比如有些是英文字体,显示不了中文,假如预览框写"预览"二字,但是预览显示出来的会是乱七八糟的东西。用字体名字来预览是效果最好的。
fontPreviewLabel
.setText(fontName.getSelectedValue().toString());
// 选择的结果实时更新,下同
fontnamefield.setText(fontName.getSelectedValue().toString());
// 此方法用来更新预览框的内容,下同
preview();
}
});
// 显示选择结果
fontSize = new JList(str_Size); // 字号
fontSize.setVisibleRowCount(5);
fontSize.setFixedCellHeight(15);
fontSize.setFixedCellWidth(120);
// 以下这步一定要做,打开字体对话框的时候选中的是现在文本所用的字体大小,假如不做这步,会出现异常
for (int i = 0; i < str_Size.length; i++) {
if (str_Size[i].equals(Integer.toString(text.getFont().getSize()))) {
index = i;
break;
}
}
fontSize.setSelectedIndex(index);
final JTextField fontsizefield = new JTextField(20);
fontsizefield.setEditable(false);
fontsizefield.setText(str_Size[index]);
fontSize.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
fontsizefield.setText(fontSize.getSelectedValue().toString());
preview();
}
});
// 显示选择结果
fontStyle = new JList(str_Style); // 样式
fontStyle.setVisibleRowCount(5);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -