📄 notepad.java
字号:
else
{
redoItem.setEnabled(false);
redopopupItem.setEnabled(false);
redoButton.setEnabled(false);
}
}
});
c.addMouseListener(new MouseAdapter() {
public void mouseExited(MouseEvent e) {
statuelabel.setText("状态栏");
}
});
c.add(tool, BorderLayout.NORTH);
c.add(scroller, BorderLayout.CENTER);
c.add(statuelabel, BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setSize(800,600);// 760540
this.setVisible(true);
}
// 事件处理
public void actionPerformed(ActionEvent e) {
// 新建
if (e.getSource() == newItem || e.getSource() == newButton) {
Object[] options = { "确定", "取消" };
int s = JOptionPane.showOptionDialog(null, "请注意保存文档!按“确定”新建文件",
"警告!", JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE, null, options, options[0]);
if (s == JOptionPane.YES_OPTION) {
text.setText("");
}
file = null;
}
// 打开
if (e.getSource() == openItem || e.getSource() == openButton) {
text.requestFocusInWindow();
filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
if (file != null)
filechooser.setSelectedFile(file);
// 文件过滤,见JAVAFileFilter类
filechooser.addChoosableFileFilter(new JAVAFileFilter("txt"));
filechooser.addChoosableFileFilter(new JAVAFileFilter("c"));
filechooser.addChoosableFileFilter(new JAVAFileFilter("cpp"));
filechooser.addChoosableFileFilter(new JAVAFileFilter("sql"));
filechooser.addChoosableFileFilter(new JAVAFileFilter("doc"));
filechooser.addChoosableFileFilter(new JAVAFileFilter("class"));
filechooser.addChoosableFileFilter(new JAVAFileFilter("java"));
filechooser.addChoosableFileFilter(new JAVAFileFilter("asm"));
int returnVal = filechooser.showOpenDialog(notepad.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = filechooser.getSelectedFile();
if (file == null || file.getName().equals(""))
JOptionPane.showMessageDialog(this, "文件名错误", "错误",
JOptionPane.ERROR_MESSAGE);
else
openFile(text);
newfileFlag = false; // 设置为保存标志
text.setCaretPosition(0);// 光标位置
}
}
// 保存
if (e.getSource() == saveItem || e.getSource() == saveButton) {
text.requestFocusInWindow();
saveFile(text);
}
// 另存为
if (e.getSource() == saveasItem) {
// 和保存的差别就在这个标志
newfileFlag = true;
saveFile(text);
}
// 页面设置,方便,JAVA提供的print方法
if (e.getSource() == uniformItem) {
text.requestFocusInWindow();
try {
text.print();
} catch (PrinterException e1) {
e1.printStackTrace();
}
}
// 打印,这个原本想了半天,后来发现JTextPane可以用printDialog来调用系统的打印机
if (e.getSource() == printItem) {
prtMe.printDialog();
}
// 退出
if (e.getSource() == exitItem) {
text.requestFocusInWindow();
quitProg();
}
// 撤销
if (e.getSource() == undoItem || e.getSource() == undoButton
|| e.getSource() == undopopupItem) {
if (undo.canUndo())
undo.undo();
}
// 重做
if (e.getSource() == redoItem || e.getSource() == redoButton
|| e.getSource() == redopopupItem) {
if (undo.canUndo())
undo.redo();
}
// 复制
if (e.getSource() == copyItem || e.getSource() == copyButton
|| e.getSource() == copypopupItem)
text.copy();
// 剪切
if (e.getSource() == cutItem || e.getSource() == cutButton
|| e.getSource() == cutpopupItem)
text.cut();
// 粘贴
if (e.getSource() == pasteItem || e.getSource() == pasteButton
|| e.getSource() == pastepopupItem)
text.paste();
// 删除
if (e.getSource() == deleteItem || e.getSource() == deleteButton
|| e.getSource() == deletepopupItem)
text.replaceSelection("");
// 查找
if (e.getSource() == searchItem)
search();
// 替换
if (e.getSource() == replaceItem)
replace();
// 全选
if (e.getSource() == seleteAllItem
|| e.getSource() == seleteAllpopupItem)
text.selectAll();
// 日期/时间
if (e.getSource() == dateItem) {
SimpleDateFormat TimeFormat = new SimpleDateFormat(
" yyyy-MM-dd HH:mm:ss ");
Calendar date = Calendar.getInstance();
date = Calendar.getInstance(Locale.CHINESE); // 以上为获取系统当前的日期和时间
text.setText(text.getText() + TimeFormat.format(date.getTime()));
}
// 背景颜色
if (e.getSource() == colorItem || e.getSource() == colorButton
|| e.getSource() == colorpopupItem) {
Color ccc = JColorChooser.showDialog(this, "背景颜色", text
.getBackground());
if(ccc!=null)
text.setBackground(ccc);
}
// 字体设置
if (e.getSource() == fontItem || e.getSource() == fontButton
|| e.getSource() == fontpopupItem) {
// 调用fontChooser
fontChooser();
}
// 关于
if (e.getSource() == aboutItem || e.getSource() == helpButton) {// 调用initabout()
initabout();
}
//字数统计
if (e.getSource() == countItem) {
int bytecount, count,allbytecount,allcount;
allbytecount = text.getText().getBytes().length;
allcount = text.getText().length();
if (text.getSelectedText() == null) {
JOptionPane.showMessageDialog(this, "字数统计 :\n全文:" + "\n" + "字数: "
+ allcount + " 个" + "\n" + "字节数:" + allbytecount + "个",
"字数统计", JOptionPane.INFORMATION_MESSAGE);
}
else {
bytecount = text.getSelectedText().getBytes().length;
count = text.getSelectedText().length();
JOptionPane.showMessageDialog(this, "字数统计 :\n全文:"+"\n" + "字数: "
+ allcount + " 个" + "\n" + "字节数:" + allbytecount + "个"+"\n您所选择的字段:" + "\n"
+ "字数: " + count + " 个" + "\n" + "字节数:" + bytecount
+ "个", "字数统计", JOptionPane.INFORMATION_MESSAGE);
}
}
}
/**
* 退出菜单的响应
*/
void quitProg() {
int a = JOptionPane.showConfirmDialog(this, "文件已被改变,是否要保存?", "提示",
JOptionPane.YES_NO_CANCEL_OPTION);
if (a == 1) {
this.dispose();
} else if (a == 0) {
// 保存文件的方法
saveFile(text);
}
if (a == 2) {
}
}
/**
* 保存文件的方法
*
* @param text
*/
void saveFile(JTextPane text) {
text.requestFocusInWindow();
// 未保存的文件
if (newfileFlag) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.addChoosableFileFilter(new JAVAFileFilter("txt"));
fileChooser.addChoosableFileFilter(new JAVAFileFilter("c"));
fileChooser.addChoosableFileFilter(new JAVAFileFilter("cpp"));
fileChooser.addChoosableFileFilter(new JAVAFileFilter("sql"));
fileChooser.addChoosableFileFilter(new JAVAFileFilter("doc"));
fileChooser.addChoosableFileFilter(new JAVAFileFilter("class"));
fileChooser.addChoosableFileFilter(new JAVAFileFilter("java"));
fileChooser.addChoosableFileFilter(new JAVAFileFilter("asm"));
fileChooser.setApproveButtonText("确定");
fileChooser.setDialogTitle("另存为");
int result = fileChooser.showSaveDialog(this);
if (result == JFileChooser.CANCEL_OPTION) {
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(text.getText(), 0, text.getText().length());
bfw.flush();
fw.close();
newfileFlag = false;
file = saveFileName;
oldValue = text.getText();
} catch (IOException ioException) {
}
}
}
// 文件已经保存
else {
try {
FileWriter fw = new FileWriter(file);
BufferedWriter bfw = new BufferedWriter(fw);
bfw.write(text.getText(), 0, text.getText().length());
bfw.flush();
fw.close();
} catch (IOException ioException) {
}
}
}
/**
* 打开文件的方法
*
* @param text
*/
void openFile(JTextPane text) {
try {
FileReader fr = new FileReader(file);
int len = (int) file.length();
char[] buffer = new char[len];
fr.read(buffer, 0, len);
fr.close();
text.setText(new String(buffer));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 初始化帮助的关于记事本,并且显示,包含记事本的信息
*/
public void initabout() {
// 关于对话框标题图片
// 帮助选项关于弹出对话框
JDialog about = new JDialog(this);
about.setLocationRelativeTo(this);
Icon abouticon = new ImageIcon("image/about.gif");
about.setTitle("关于记事本");
about.setSize(410, 250);
Container ca = about.getContentPane();
ca.setBackground(Color.GRAY);
ca.add(new JLabel(abouticon), BorderLayout.NORTH);
JPanel panel = new JPanel();
panel.setBackground(new Color(200, 200, 255));
panel.setFont(new Font("", Font.BOLD, 18));
JLabel label1 = new JLabel("记事本程序 Version2.01", SwingConstants.CENTER);
JLabel label2 = new JLabel("制作者:许子彦", SwingConstants.CENTER);
JLabel label3 = new JLabel("厦门大学软件学院软件工程系 06级", SwingConstants.CENTER);
JLabel label4 = new JLabel("学号:24320062204504", SwingConstants.CENTER);
JLabel label5 = new JLabel("个人制作,版权所有");
// 设置BOX
Box box1 = Box.createVerticalBox();
box1.add(label5, SwingConstants.CENTER);
box1.add(Box.createVerticalStrut(15)); // 两行的间距
box1.add(label4, SwingConstants.CENTER);
box1.add(Box.createVerticalStrut(15));
box1.add(label3, SwingConstants.CENTER);
box1.add(Box.createVerticalStrut(15));
box1.add(label2, SwingConstants.CENTER);
box1.add(Box.createVerticalStrut(15));
box1.add(label1, SwingConstants.CENTER);
panel.add(box1);
ca.add(panel, BorderLayout.CENTER);
about.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
// 显示
about.show();
}
// 自动换行事件
public void itemStateChanged(ItemEvent e2) {
if (e2.getItemSelectable() == lineItem) {
if (lineItem.getState()) {
scroller
.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
} else
scroller
.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
}
if (e2.getItemSelectable() == statueItem) {
if (statueItem.getState())
statuelabel.setVisible(true);
else
statuelabel.setVisible(false);
}
}
/**
* 处理查找的方法
*/
public void search() {
final JDialog searchDia = new JDialog(this, "查找", true);
searchDia.setLocationRelativeTo(this);
searchDia.setSize(450, 200);
// 这个设置可以在弹出对话框之后能回到文本编辑
searchDia.setModalityType(Dialog.ModalityType.MODELESS);
Container c = searchDia.getContentPane();
GridLayout gridlayout = new GridLayout(2, 3);
gridlayout.setHgap(10);
gridlayout.setVgap(10);
c.setLayout(gridlayout);
JPanel labelpanel = new JPanel();
JLabel label1 = new JLabel("查找内容(N)");
labelpanel.add(label1);
final JTextField field1 = new JTextField(20);
JPanel fieldpanel = new JPanel();
fieldpanel.add(field1);
JPanel searchnextpanel = new JPanel();
JButton searchnextButton = new JButton("查找下一个(F)");
searchnextpanel.add(searchnextButton);
JPanel checkpanel = new JPanel();
final JCheckBox checkcase = new JCheckBox("区分大小写(C)");
checkpanel.add(checkcase);
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);
JPanel cancelpanel = new JPanel();
// 下面"取消"前后有空格是使界面更好看,按钮对齐,下同
JButton cancelButton = new JButton(" 取消 ");
cancelpanel.add(cancelButton);
c.add(labelpanel);
c.add(fieldpanel);
c.add(searchnextpanel);
c.add(checkpanel);
c.add(directionpanel);
c.add(cancelpanel);
// 查找下一个按钮的事件
searchnextButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int a = 0, b = 0;
// 设置文本的初始位置
int FindStartPos = text.getCaretPosition();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -