📄 notepad备份.java
字号:
else
{ if(replaceCount==0)
{ JOptionPane.showMessageDialog(replaceDialog, "找不到您查找的内容!", "记事本",JOptionPane.INFORMATION_MESSAGE);
}
else
{ JOptionPane.showMessageDialog(replaceDialog,"成功替换"+replaceCount+"个匹配内容!","替换成功",JOptionPane.INFORMATION_MESSAGE);
}
}
}
else if(downButton.isSelected())
{ if(editArea.getSelectedText()==null)
k=strA.indexOf(strB,editArea.getCaretPosition()+1);
else
k=strA.indexOf(strB, editArea.getCaretPosition()-findText.getText().length()+1);
if(k>-1)
{ editArea.setCaretPosition(k);
editArea.select(k,k+strB.length());
}
else
{ if(replaceCount==0)
{ JOptionPane.showMessageDialog(replaceDialog, "找不到您查找的内容!", "记事本",JOptionPane.INFORMATION_MESSAGE);
}
else
{ JOptionPane.showMessageDialog(replaceDialog,"成功替换"+replaceCount+"个匹配内容!","替换成功",JOptionPane.INFORMATION_MESSAGE);
}
}
}
if(replaceText.getText().length()==0 && editArea.getSelectedText()!= null)
{ editArea.replaceSelection("");
replaceCount++;
}
if(replaceText.getText().length()>0 && editArea.getSelectedText()!= null)
{ editArea.replaceSelection(replaceText.getText());
replaceCount++;
}
}//while循环结束
}
});//"替换全部"方法结束
//创建"替换"对话框的界面
JPanel directionPanel=new JPanel();
directionPanel.setBorder(BorderFactory.createTitledBorder("方向"));
//设置directionPanel组件的边框;
//BorderFactory.createTitledBorder(String title)创建一个新标题边框,使用默认边框(浮雕化)、默认文本位置(位于顶线上)、默认调整 (leading) 以及由当前外观确定的默认字体和文本颜色,并指定了标题文本。
directionPanel.add(upButton);
directionPanel.add(downButton);
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
JPanel panel3=new JPanel();
JPanel panel4=new JPanel();
panel4.setLayout(new GridLayout(2,1));
panel1.add(findContentLabel);
panel1.add(findText);
panel1.add(findNextButton);
panel4.add(replaceButton);
panel4.add(replaceAllButton);
panel2.add(replaceLabel);
panel2.add(replaceText);
panel2.add(panel4);
panel3.add(matchCheckBox);
panel3.add(directionPanel);
panel3.add(cancel);
con.add(panel1);
con.add(panel2);
con.add(panel3);
replaceDialog.setSize(420,220);
replaceDialog.setResizable(false);//不可调整大小
replaceDialog.setLocation(230,280);
replaceDialog.setVisible(true);
}//"全部替换"按钮监听结束
//"字体"方法
public void font()
{ final JDialog fontDialog=new JDialog(this,"字体设置",false);
Container con=fontDialog.getContentPane();
con.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel fontLabel=new JLabel("字体(F):");
fontLabel.setPreferredSize(new Dimension(100,20));//构造一个Dimension,并将其初始化为指定宽度和高度
JLabel styleLabel=new JLabel("字形(Y):");
styleLabel.setPreferredSize(new Dimension(100,20));
JLabel sizeLabel=new JLabel("大小(S):");
sizeLabel.setPreferredSize(new Dimension(100,20));
final JLabel sample=new JLabel("张选仲的记事本-ZXZ's Notepad");
final JTextField fontText=new JTextField(9);
fontText.setPreferredSize(new Dimension(200,20));
final JTextField styleText=new JTextField(8);
styleText.setPreferredSize(new Dimension(200,20));
final int style[]={Font.PLAIN,Font.BOLD,Font.ITALIC,Font.BOLD+Font.ITALIC};
final JTextField sizeText=new JTextField(5);
sizeText.setPreferredSize(new Dimension(200,20));
JButton okButton=new JButton("确定");
JButton cancel=new JButton("取消");
cancel.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e)
{ fontDialog.dispose();
}
});
Font currentFont=editArea.getFont();
fontText.setText(currentFont.getFontName());
fontText.selectAll();
if(currentFont.getStyle()==Font.PLAIN)
styleText.setText("常规");
else if(currentFont.getStyle()==Font.BOLD)
styleText.setText("粗体");
else if(currentFont.getStyle()==Font.ITALIC)
styleText.setText("斜体");
else if(currentFont.getStyle()==(Font.BOLD+Font.ITALIC))
styleText.setText("粗斜体");
styleText.selectAll();
String str=String.valueOf(currentFont.getSize());
sizeText.setText(str);
sizeText.selectAll();
final JList fontList,styleList,sizeList;
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
final String fontName[]=ge.getAvailableFontFamilyNames();
fontList=new JList(fontName);
fontList.setFixedCellWidth(86);
fontList.setFixedCellHeight(20);
fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
final String fontStyle[]={"常规","粗体","斜体","粗斜体"};
styleList=new JList(fontStyle);
styleList.setFixedCellWidth(86);
styleList.setFixedCellHeight(20);
styleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
if(currentFont.getStyle()==Font.PLAIN)
styleList.setSelectedIndex(0);
else if(currentFont.getStyle()==Font.BOLD)
styleList.setSelectedIndex(1);
else if(currentFont.getStyle()==Font.ITALIC)
styleList.setSelectedIndex(2);
else if(currentFont.getStyle()==(Font.BOLD+Font.ITALIC))
styleList.setSelectedIndex(3);
final String fontSize[]={"8","9","10","11","12","14","16","18","20","22","24","26","28","36","48","72"};
sizeList=new JList(fontSize);
sizeList.setFixedCellWidth(43);
sizeList.setFixedCellHeight(20);
sizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fontList.addListSelectionListener(new ListSelectionListener()
{ public void valueChanged(ListSelectionEvent event)
{ fontText.setText(fontName[fontList.getSelectedIndex()]);
fontText.selectAll();
Font sampleFont1=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText()));
sample.setFont(sampleFont1);
}
});
styleList.addListSelectionListener(new ListSelectionListener()
{ public void valueChanged(ListSelectionEvent event)
{ int s=style[styleList.getSelectedIndex()];
styleText.setText(fontStyle[s]);
styleText.selectAll();
Font sampleFont2=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText()));
sample.setFont(sampleFont2);
}
});
sizeList.addListSelectionListener(new ListSelectionListener()
{ public void valueChanged(ListSelectionEvent event)
{ sizeText.setText(fontSize[sizeList.getSelectedIndex()]);
sizeText.selectAll();
Font sampleFont3=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText()));
sample.setFont(sampleFont3);
}
});
okButton.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e)
{ Font okFont=new Font(fontText.getText(),style[styleList.getSelectedIndex()],Integer.parseInt(sizeText.getText()));
editArea.setFont(okFont);
fontDialog.dispose();
}
});
JPanel samplePanel=new JPanel();
samplePanel.setBorder(BorderFactory.createTitledBorder("示例"));
samplePanel.add(sample);
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
JPanel panel3=new JPanel();
panel2.add(fontText);
panel2.add(styleText);
panel2.add(sizeText);
panel2.add(okButton);
panel3.add(new JScrollPane(fontList));//JList不支持直接滚动,所以要让JList作为JScrollPane的视口视图
panel3.add(new JScrollPane(styleList));
panel3.add(new JScrollPane(sizeList));
panel3.add(cancel);
con.add(panel1);
con.add(panel2);
con.add(panel3);
con.add(samplePanel);
fontDialog.setSize(350,340);
fontDialog.setLocation(200,200);
fontDialog.setResizable(false);
fontDialog.setVisible(true);
}//"字体"方法结束
public void actionPerformed(ActionEvent e)
{ //新建
if(e.getSource()==fileMenu_New)
{ editArea.requestFocus();
String currentValue=editArea.getText();
boolean isTextChange=(currentValue.equals(oldValue))?false:true;
if(isTextChange)
{ int saveChoose=JOptionPane.showConfirmDialog(this,"您的文件尚未保存,是否保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION);
if(saveChoose==JOptionPane.YES_OPTION)
{ String str=null;
JFileChooser fileChooser=new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setDialogTitle("另存为");
int result=fileChooser.showSaveDialog(this);
if(result==JFileChooser.CANCEL_OPTION)
{ statusLabel.setText("您没有选择任何文件");
return;
}
File saveFileName=fileChooser.getSelectedFile();
if(saveFileName==null || saveFileName.getName().equals(""))
{ JOptionPane.showMessageDialog(this,"不合法的文件名","不合法的文件名",JOptionPane.ERROR_MESSAGE);
}
else
{ try
{ FileWriter fw=new FileWriter(saveFileName);
BufferedWriter bfw=new BufferedWriter(fw);
bfw.write(editArea.getText(),0,editArea.getText().length());
bfw.flush();//刷新该流的缓冲
bfw.close();
isNewFile=false;
currentFile=saveFileName;
oldValue=editArea.getText();
this.setTitle(saveFileName.getName()+" - 记事本");
statusLabel.setText("当前打开文件:"+saveFileName.getAbsoluteFile());
}
catch (IOException ioException)
{
}
}
}
else if(saveChoose==JOptionPane.NO_OPTION)
{ editArea.replaceRange("",0,editArea.getText().length());
statusLabel.setText(" 新建文件");
this.setTitle("无标题 - 记事本");
isNewFile=true;
undo.discardAllEdits(); //撤消所有的"撤消"操作
editMenu_Undo.setEnabled(false);
oldValue=editArea.getText();
}
else if(saveChoose==JOptionPane.CANCEL_OPTION)
{ return;
}
}
else
{ editArea.replaceRange("",0,editArea.getText().length());
statusLabel.setText(" 新建文件");
this.setTitle("无标题 - 记事本");
isNewFile=true;
undo.discardAllEdits();//撤消所有的"撤消"操作
editMenu_Undo.setEnabled(false);
oldValue=editArea.getText();
}
}//新建结束
//打开
else if(e.getSource()==fileMenu_Open)
{ editArea.requestFocus();
String currentValue=editArea.getText();
boolean isTextChange=(currentValue.equals(oldValue))?false:true;
if(isTextChange)
{ int saveChoose=JOptionPane.showConfirmDialog(this,"您的文件尚未保存,是否保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION);
if(saveChoose==JOptionPane.YES_OPTION)
{ String str=null;
JFileChooser fileChooser=new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setDialogTitle("另存为");
int result=fileChooser.showSaveDialog(this);
if(result==JFileChooser.CANCEL_OPTION)
{ statusLabel.setText("您没有选择任何文件");
return;
}
File saveFileName=fileChooser.getSelectedFile();
if(saveFileName==null || saveFileName.getName().equals(""))
{ JOptionPane.showMessageDialog(this,"不合法的文件名","不合法的文件名",JOptionPane.ERROR_MESSAGE);
}
else
{ try
{ FileWriter fw=new FileWriter(saveFileName);
BufferedWriter bfw=new BufferedWriter(fw);
bfw.write(editArea.getText(),0,editArea.getText().length());
bfw.flush();//刷新该流的缓冲
bfw.close();
isNewFile=false;
currentFile=saveFileName;
oldValue=editArea.getText();
this.setTitle(saveFileName.getName()+" - 记事本");
statusLabel.setText("当前打开文件:"+saveFileName.getAbsoluteFile());
}
catch (IOException ioException)
{
}
}
}
else if(saveChoose==JOptionPane.NO_OPTION)
{ String str=null;
JFileChooser fileChooser=new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setDialogTitle("打开文件");
int result=fileChooser.showOpenDialog(this);
if(result==JFileChooser.CANCEL_OPTION)
{ statusLabel.setText("您没有选择任何文件");
return;
}
File fileName=fileChooser.getSelectedFile();
if(fileName==null || fileName.getName().equals(""))
{ JOptionPane.showMessageDialog(this,"不合法的文件名","不合法的文件名",JOptionPane.ERROR_MESSAGE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -