📄 notebook.java
字号:
else if(e.getActionCommand().equals("0")){ //监听到新建按钮的事件处理
ta.selectAll(); //清除当前记事本内容
ta.replaceRange("",ta.getSelectionStart(),ta.getSelectionEnd());
}
else if(e.getActionCommand().equals("3")) //监听到全选按钮的事件处理
ta.selectAll();
else if(e.getActionCommand().equals("4")) //监听到剪切按钮的事件处理
cutMethod();
else if(e.getActionCommand().equals("5")) //监听到复制按钮的事件处理
copyMethod();
else if(e.getActionCommand().equals("6")) //监听到粘贴按钮的事件处理
pasteMethod();
else if(e.getActionCommand().equals("8")){ //实现闹钟功能
alarmDialog.setVisible(true); //显示闹钟对话框
Integer alarmOption=(Integer)alarmPane.getValue(); //取得选项按钮的选项
if(alarmOption!=null) //使按叉钮关闭对话框时正常
if(alarmOption.intValue()==JOptionPane.OK_OPTION) //当按下赞成钮
alarmString=(String)alarmPane.getInputValue(); //取得用户输入时间
hourString=alarmString.substring(0,2); //对用户输入时间进行操作
minuteString=alarmString.substring(3,5);
secondString=alarmString.substring(6,8);
h1=Integer.parseInt(hourString); //将字符串转换为整型数据
m1=Integer.parseInt(minuteString);
s1=Integer.parseInt(secondString);
musicDialog.setVisible(true); //显示音乐选择对话框
Integer musicOption=(Integer)musicPane.getValue(); //取得选项区的选项
if(musicOption!=null)
if(musicOption.intValue()==JOptionPane.OK_OPTION){ //按下OK钮
musicString=(String)musicPane.getInputValue(); //取得选项区的资料
alarmMethod(); //调用报警功能
}
}
else if(e.getActionCommand().equals("7")){ //实现调用系统计算器
Runtime calc=java.lang.Runtime.getRuntime(); //创建Runtime类对象,提供所在系统的接口
try{
calc.exec("c:/windows/system32/calc.exe"); //运行计算器
}catch(IOException e1){
}
}
else if(e.getActionCommand().equals("9")){
helpDialog.setVisible(true); //显示帮助对话框
}
else if(e.getActionCommand().equals("10"))
System.exit(0); //实现退出记事本
}
class TestFileFilter extends FileFilter{ //实现在存取对话框中显示扩展名为"txt"的文件
public boolean accept(File f){
return(f.isDirectory()||f.getName().endsWith(".txt"));
}
public String getDescription(){
return("Text files(*.txt)");
}
}
public void readerMethod(){ //实现读取文件
try{
FileReader cin=new FileReader(fc.getSelectedFile().getAbsolutePath());//创建文件字符流对象
BufferedReader in=new BufferedReader(cin); //创建缓冲字符流对象
ta.selectAll(); //清除当前记事本内容
ta.replaceRange("",ta.getSelectionStart(),ta.getSelectionEnd());
while(true){
String str=in.readLine(); //将文件分行读入记事本
if(str==null)
break;
ta.append(str+"\n");
}
cin.close(); //关闭流 (?)
}catch(FileNotFoundException e){ //读取文件时的例外处理
ta.append("文件不存在");
}catch(IOException e){
ta.append("读取错误");
}
}
public void writerMethod(){ //实现存储记事本中的信息
try{
FileWriter cout=new FileWriter(fc.getSelectedFile().getAbsolutePath());//创建字符输出流对象
BufferedWriter out=new BufferedWriter(cout); //创建缓冲字符输出流对象
out.write(ta.getText());
out.close(); //关闭缓冲字符输出流 (?)
}catch(IOException e){
ta.append("Exception");
}
}
public void cutMethod(){ //实现剪切功能
try{
FileWriter cout=new FileWriter("jiantieban.txt"); //创建一个剪贴板,其实现过程与保存一个文件相同
BufferedWriter out=new BufferedWriter(cout);
out.write(ta.getSelectedText()); //选择记事本上的信息
out.close();
ta.replaceRange("",ta.getSelectionStart(),ta.getSelectionEnd()); //并将选择的信息替换为空
}catch(IOException e){
ta.append("Exception");
}
}
public void copyMethod(){ //实现复制功能,其实现过程与上剪切方法类似
try{
FileWriter cout=new FileWriter("jiantieban.txt");
BufferedWriter out=new BufferedWriter(cout);
out.write(ta.getSelectedText()); //将选择的内容写到剪贴板
out.close();
}catch(IOException e){
ta.append("Exception");
}
}
public void pasteMethod(){ //实现粘贴功能
try{
FileReader cin=new FileReader("jiantieban.txt");
BufferedReader in=new BufferedReader(cin);
while(true){
String str=in.readLine(); //从剪贴板中读取数据
if(str==null)
break;
ta.insert(str,ta.getSelectionStart()); //在文本框中当前光标下插入数据
}
cin.close();
}catch(FileNotFoundException e){
ta.append("文件不存在");
}catch(IOException e){
ta.append("读取错误");
}
}
public void alarmMethod(){ //实现闹钟功能
Thread t=new Thread(this); //创建一个线程
t.start(); //启动线程
}
public void run(){ //在run()方法中存放线程执行代码
try{
for(;;){
c=Calendar.getInstance(); //创建一个时钟对象
h2=c.get(Calendar.HOUR_OF_DAY); //从系统中获取小时
m2=c.get(Calendar.MINUTE); //从系统中获取分钟
s2=c.get(Calendar.SECOND); //从系统中获取秒钟
if(h1==h2&&m1==m2&&s1==s2){ //铃声启动条件
try{
sound=new URL("file:/sound/"+musicString+".wav"); //创建一个URL组件
music=Applet.newAudioClip(sound); //实现在应用程序中播放声音
music.loop(); //循环播放铃声
}catch(MalformedURLException e){ //捕获URL异常
}
wakeDialog.setVisible(true);
Integer wakeOption=(Integer)wakePane.getValue();
if(wakeOption.intValue()==JOptionPane.OK_OPTION){ //按下OK钮
music.stop(); //停止响铃
t.stop(); //停止计时线程
}
}
t.sleep(1000); //计时间隔一秒
}
}catch(InterruptedException e){ //捕获中断异常
}
}
public static void main(String args[]){ //主方法
JFrame frame=new NoteBook(); //创建JFrame对象,初始不可见
frame.setSize(400,200); //记事本的大小
frame.setDefaultCloseOperation(EXIT_ON_CLOSE); //实现记事本关闭功能
frame.pack(); //压缩框架的显示区域
frame.setVisible(true); //显示框架的主窗口
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -