📄 jishiben.java
字号:
copyBtn=new JButton(new ImageIcon("images/copy.gif"));
copyBtn.setToolTipText("复制");
toolBar1.add(copyBtn);
pasteBtn=new JButton(new ImageIcon("images/paste.gif"));
toolBar1.add(pasteBtn);
pasteBtn.setToolTipText("粘贴");
//创建"文字工具"工具栏
JToolBar toolBar2=new JToolBar("文字工具",SwingConstants.HORIZONTAL);
toolBar2.add(comFont);
toolBar2.add(comSize);
toolBar2.addSeparator();
toolBar2.add(bold);
toolBar2.add(italic);
//toolBar2.add(lineBtn);
toolBar2.addSeparator();
toolBar2.add(undoBtn);
toolBar2.add(redoBtn);
//创建日期工具栏
JToolBar toolBar3=new JToolBar("当前日期",SwingConstants.HORIZONTAL);
toolBar3.add(time);
toolBar3.add(time1);
class ClockThread extends Thread {
Date date = new Date();
Calendar calendar = new GregorianCalendar();
public ClockThread() {
calendar.setTime(date);
}
public void run() {
while (true) {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss EEEE");
calendar.set(Calendar.SECOND,
calendar.get(Calendar.SECOND) + 1);
time1.setText(dateFormat.format(calendar.getTime()));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
new ClockThread().start();
//将各工具栏加入面板
JPanel p=new JPanel(new FlowLayout(FlowLayout.LEFT));
p.add(toolBar);
p.add(toolBar1);
p.add(toolBar2);
p.add(toolBar3);
c.add(p,BorderLayout.NORTH);
//将按钮注册给动作事件的监听者
openBtn.addActionListener(this);
newBtn.addActionListener(this);
cutBtn.addActionListener(this);
copyBtn.addActionListener(this);
pasteBtn.addActionListener(this);
saveBtn.addActionListener(this);
undoBtn.addActionListener(this);
redoBtn.addActionListener(this);
// lineBtn.addActionListener(this);
//设置事件监听器,监听游标,动态显示行数和列数
text.addCaretListener(new CaretListener()
{
public void caretUpdate(CaretEvent e)
{
try
{
int pos = text.getCaretPosition();
//Gets the position of the text insertion caret(游标) for this text component
//获取行数
int lineOfC = text.getLineOfOffset(pos) + 1;
//Translates an offset into the components text to a line number.
//获取列数
int col = pos - text.getLineStartOffset(lineOfC - 1) + 1;
// Determines the offset of the start of the given line.
statusBar.setText("当前光标位置 " + lineOfC + " 行 , " + col + " 列 ");
}
catch(Exception ex)
{
statusBar.setText(" 无法获得当前光标位置");
}
}
});
//监听文档内容是否发生变化
text.getDocument().addDocumentListener(
new DocumentListener() {
public void changedUpdate(DocumentEvent de) {
isChange = true;
}
public void insertUpdate(DocumentEvent de) {
isChange = true;
}
public void removeUpdate(DocumentEvent de) {
isChange = true;
}
}
);
}
//辅助方法:测试JTextArea中文本是否有变化
private boolean hasChange() {
return isChange;
}
//内部方法:响应菜单事件
private void newPerformed() {
int value = -1;
if(hasChange()) //若文件已更改
if(fileName != null && fileName.exists()) {//文件名不为空并且文件名存在
String[] buttonTexts = {"保存", "不保存", "取消"};
value = JOptionPane.showOptionDialog(this, "源程序已经改变,请确认是否保存!", "保存更改?", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, buttonTexts, buttonTexts[0]);
if(value == JOptionPane.YES_OPTION)
savePerformed();
else if(value == JOptionPane.NO_OPTION) {//不保存,初始化当前编辑器
fileName = null;
text.setText("");
setTitle("小小记事本");
return;
}
else if(value == JOptionPane.CANCEL_OPTION)
return;
}
else { //若文件名不存在,则另存,初始化当前编辑器
saveAsPerformed();
// fileName = null;
//text.setText("");
// setTitle("程序编辑器");
// return;
}
fileName = null;//文件未更改ho或已处理,初始化当前编辑器
text.setText("");
setTitle("小小记事本");
isChange = false;
return;
}
private void openPerformed() {
BufferedReader input = null;
String textString;
int ret = fileChooser.showOpenDialog(this);
if(ret != JFileChooser.APPROVE_OPTION)
return;
newPerformed();
fileName = fileChooser.getSelectedFile();
if(fileName.exists() && fileName.canRead()) {
setTitle("小小记事本: " + fileName.getName());
try{
input = new BufferedReader(new FileReader(fileName));
while((textString = input.readLine()) != null)
text.append(textString + "\n");
input.close();
}
catch(IOException e) {
JOptionPane.showMessageDialog(this, "文件打开错误,没有打开");
}
finally {
input = null;
}
}
isChange = false;
return;
}
private void savePerformed() {
if(!hasChange())
return;
FileWriter output = null;
if(fileName != null && fileName.exists()) {
try{
output = new FileWriter(fileName);
output.write(text.getText());
}
catch(IOException e)
{
JOptionPane.showMessageDialog(this, "文件写入错误,没有保存");
}
finally {
try{ output.close();}catch(IOException ee){}
output = null;
}
isChange = false;
}
else
saveAsPerformed();
}
private void saveAsPerformed() {
if(!hasChange())
return;
FileWriter output = null;
int ret = fileChooser.showSaveDialog(this);
if(ret != JFileChooser.APPROVE_OPTION) {
isChange = true;
return;
}
fileName = fileChooser.getSelectedFile();
setTitle("小小记事本: " + fileName.getName());
try{
output = new FileWriter(fileName);
output.write(text.getText());
}
catch(IOException e){
JOptionPane.showMessageDialog(this, "文件写入错误,没有保存");
}
finally{
try{ output.close();}catch(IOException ee){}
output = null;
}
isChange = false;
return;
}
private void exitPerformed() {
String[] buttonTexts = {"保存退出", "不保存退出", "取消"};
int value = -1;
if(hasChange()) {
value = JOptionPane.showOptionDialog(this, "源程序已经改变,请是否选择保存或直接退出!", "退出程序", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, buttonTexts, buttonTexts[0]);
switch(value)
{
case JOptionPane.YES_OPTION:
savePerformed();
break;
case JOptionPane.NO_OPTION:
System.exit(0);
break;
case JOptionPane.CANCEL_OPTION:
isChange = true;
return;
}
}
else
System.exit(0);
}
private void undoPerformed() {
try{
if(undo.canUndo())
undo.undo();
}catch(CannotUndoException e) {
JOptionPane.showMessageDialog(this, "无法执行撤销操作", "撤销异常", JOptionPane.WARNING_MESSAGE);
}
}
private void redoPerformed() {
try{
if(undo.canRedo())
undo.redo();
}catch(CannotRedoException e) {
JOptionPane.showMessageDialog(this, "无法执行恢复操作", "恢复异常", JOptionPane.WARNING_MESSAGE);
}
}
private void cutPerformed() {
//textArea.cut();
placeHolder=text.getSelectedText();
text.replaceRange("",text.getSelectionStart(),text.getSelectionEnd());
statusBar.setText("执行了cut操作");
}
private void copyPerformed() {
// textArea.copy();
placeHolder=text.getSelectedText();
statusBar.setText("执行了copy操作");
}
private void pastePerformed() {
//textArea.paste();
text.insert(placeHolder,text.getCaretPosition());
statusBar.setText("执行了Paste操作");
}
private void deletePerformed() {
text.replaceRange(null, text.getSelectionStart(), text.getSelectionEnd());
}
private void foreColorPerformed(){
color1=color.showDialog(this,"请选择字体颜色",Color.BLACK);
text.setForeground(color1);
}
private void backColorPerformed(){
color2=color.showDialog(this,"请选择背景颜色",Color.WHITE);
text.setBackground(color2);
}
private void selectAllPerformed() {
text.selectAll();
}
private void timePerformed() {
text.insert((new Date()).toString(), text.getSelectionEnd());
}
private void findPerformed() {
FindDialog dFind = new FindDialog(text, JiShiBen.this);
dFind.setVisible(true);
}
private void findNextPerformed() {
FindDialog dFind = new FindDialog(text, JiShiBen.this);
dFind.setVisible(true);
}
private void replacePerformed() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -