资源描述
《Java程序设计》课程设计指导书
一、课程设计的目的
第二部分 小型软件的开发(选做一题,且由个人独立开发完成)
课程设计题目
1、编写一个记事本程序。
要求:使用图形用户界面实现。能实现编辑、保存、另存为、查询替换等功能。
import java.awt.CheckboxMenuItem;
import java.awt.Color;
import java.awt.Container;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class notebook {
// 记事本的具体实现类
private static final long serialVersionUID = 1L;
private TextArea content;
private String filePath = "";//先让路径为空
Color color=Color.red;
Toolkit toolKit = Toolkit.getDefaultToolkit();
Clipboard clipboard = toolKit.getSystemClipboard();
public notebook(){
//创建一个JFrame对象;并设置相关属性
final JFrame jf = new JFrame("我的记事本");
jf.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
jf.setBounds(100,100,500,500);
jf.setResizable(true);
jf.setVisible(true);
//创建菜单栏
MenuBar menu = new MenuBar();
jf.setMenuBar(menu);
//创建并添加文本框
content = new TextArea("",50,50,TextArea.SCROLLBARS_VERTICAL_ONLY);
jf.add(content);
content.setVisible(true);
content.requestFocusInWindow();
//菜单栏添加内容
Menu filemenu = new Menu("文件(F)");
Menu editmenu = new Menu("编辑(E)");
Menu formatmenu = new Menu("格式(O)");
Menu viewmenu = new Menu("查看(V)");
Menu helpmenu = new Menu("帮助(H)");
menu.add(filemenu);
menu.add(editmenu);
menu.add(formatmenu);
menu.add(viewmenu);
menu.add(helpmenu);
//创建文件菜单上的各个菜单项并添加到菜单上
MenuItem newitem = new MenuItem("新建(N)");
newitem.setShortcut(new MenuShortcut(KeyEvent.VK_N,false));
filemenu.add(newitem);
MenuItem openitem = new MenuItem("打开(O)");
openitem.setShortcut(new MenuShortcut(KeyEvent.VK_O,false));
filemenu.add(openitem);
MenuItem saveitem = new MenuItem("保存(S)");
saveitem.setShortcut(new MenuShortcut(KeyEvent.VK_S,false));
filemenu.add(saveitem);
MenuItem saveasitem = new MenuItem("另存为(A)");
saveasitem.setShortcut(new MenuShortcut(KeyEvent.VK_A,false));
filemenu.add(saveasitem);
MenuItem setitem = new MenuItem("页面设置(U)");
setitem.setShortcut(new MenuShortcut(KeyEvent.VK_U,false));
filemenu.add(setitem);
setitem.setEnabled(false);
MenuItem printitem = new MenuItem("打印(P)");
printitem.setShortcut(new MenuShortcut(KeyEvent.VK_P,false));
filemenu.add(printitem);
printitem.setEnabled(false);
filemenu.addSeparator();
MenuItem exititem = new MenuItem("退出(X)");
exititem.setShortcut(new MenuShortcut(KeyEvent.VK_X,false));
filemenu.add(exititem);
//添加监听器来实现文件菜单上的各个菜单项的功能
//新建菜单项的功能实现
newitem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String con = content.getText();
if(!con.equals("")){//文本域里文本不为空
int result = JOptionPane.showConfirmDialog(
null, ("是否要保存?"),("保存文件..."),JOptionPane.YES_NO_CANCEL_OPTION);
if(result == JOptionPane.NO_OPTION){//不保存
content.setText("");
}
else if(result == JOptionPane.CANCEL_OPTION){//取消新建
}
else if(result == JOptionPane.YES_OPTION)//选择保存
{
JFileChooser jfc = new JFileChooser();//用于选择保存路径的文件名
int bcf = jfc.showSaveDialog(jf);
if(bcf == JFileChooser.APPROVE_OPTION){
try {
//保存文件
BufferedWriter bfw = new BufferedWriter(
new FileWriter(new File(jfc.getSelectedFile().getAbsolutePath()+".txt")));
filePath = jfc.getSelectedFile().getAbsolutePath()+".txt";//获取文件保存的路径
bfw.write(con);//向文件写出数据
bfw.flush();
bfw.close();//关闭输出流
} catch (IOException ex) {
Logger.getLogger(notebook.class.getName()).log(Level.SEVERE, null, ex);
}
new notebook();//新建文本文件
}
}
}
}
});
//打开菜单项的功能实现
openitem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
FileDialog dialog = new FileDialog(new JFrame(),"打开....",FileDialog.LOAD);
dialog.setVisible(true);
filePath = dialog.getDirectory() + dialog.getFile();
System.out.println(filePath);
File file = new File(filePath);
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
try{
br = new BufferedReader (new FileReader(file));
String str = null;
while ((str = br.readLine()) != null){
sb.append(str).append("\n");
}
content.setText(sb.toString());
}
catch(FileNotFoundException e1){
e1.printStackTrace();
}
catch(IOException e1){
e1.printStackTrace();
}
finally{
if(br != null){
try{
br.close();
}
catch(IOException e1){
e1.printStackTrace();
}
}
}
}
});
//保存菜单项的功能实现
saveitem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
FileDialog dialog = new FileDialog(new JFrame(),"保存....",FileDialog.SAVE);
dialog.setVisible(true);
filePath = dialog.getDirectory() + dialog.getFile();
if(filePath.equals("")){//没有路径时,就另存为
JFileChooser jfc = new JFileChooser();//用于选择保存路径的文件名
int bcf = jfc.showSaveDialog(jf);//弹出保存窗口
if(bcf == JFileChooser.APPROVE_OPTION){
try {
//保存文件
BufferedWriter bfw = new BufferedWriter(
new FileWriter(new File(jfc.getSelectedFile().getAbsolutePath()+".txt")));
filePath = jfc.getSelectedFile().getAbsolutePath();
bfw.write(content.getText());//向文件写出数据
bfw.flush();
bfw.close();//关闭输出流
} catch (IOException ex) {
Logger.getLogger(notebook.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
else{//路径不为空时,保存在原来的路径下
try {
//保存文件
BufferedWriter bfw = new BufferedWriter(
new FileWriter(
new File(filePath)));
bfw.write(content.getText());//向文件写出数据
bfw.flush();
bfw.close();//关闭输出流
} catch (IOException ex) {
Logger.getLogger(notebook.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
//另存为菜单项的功能实现
saveasitem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JFileChooser jfc = new JFileChooser();//用于选择保存路径的文件名
int bcf = jfc.showSaveDialog(jf);//弹出保存窗口
if(bcf == JFileChooser.APPROVE_OPTION){
try {
//保存文件
BufferedWriter bfw = new BufferedWriter(
new FileWriter(new File(jfc.getSelectedFile().getAbsolutePath()+".txt")));
filePath = jfc.getSelectedFile().getAbsolutePath();
bfw.write(content.getText());//向文件写出数据
bfw.flush();
bfw.close();//关闭输出流
} catch (IOException ex) {
Logger.getLogger(notebook.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
//页面设置菜单项的功能实现
setitem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
}
});
//打印菜单项的功能实现
printitem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
}
});
//退出菜单项的功能实现
exititem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Object[] options = { "是的,我要退出", "不好意思,点错了" };
int option = JOptionPane.showOptionDialog(null, "您确定要退出吗?",
"退出提示....",JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE,
null,options, options[0]);
if(option == JOptionPane.OK_OPTION){
System.exit(0);
}
}
});
//创建编辑菜单上的各个菜单项并添加到菜单上
MenuItem undoitem = new MenuItem("撤销(U)");
undoitem.setShortcut(new MenuShortcut(KeyEvent.VK_Z,false));
editmenu.add(undoitem);
MenuItem cutitem = new MenuItem("剪切(T)");
cutitem.setShortcut(new MenuShortcut(KeyEvent.VK_X,false));
editmenu.add(cutitem);
MenuItem copyitem = new MenuItem("复制(C)");
copyitem.setShortcut(new MenuShortcut(KeyEvent.VK_C,false));
editmenu.add(copyitem);
MenuItem pasteitem = new MenuItem("粘贴(P)");
pasteitem.setShortcut(new MenuShortcut(KeyEvent.VK_V,false));
editmenu.add(pasteitem);
MenuItem deleteitem = new MenuItem("删除(L)");
deleteitem.setShortcut(new MenuShortcut(KeyEvent.VK_DELETE,false));
editmenu.add(deleteitem);
editmenu.addSeparator();
MenuItem finditem = new MenuItem("查找(F)");
finditem.setShortcut(new MenuShortcut(KeyEvent.VK_F,false));
editmenu.add(finditem);
MenuItem nextitem = new MenuItem("查找下一个(N)");
nextitem.setShortcut(new MenuShortcut(KeyEvent.VK_3,false));
editmenu.add(nextitem);
MenuItem replaceitem = new MenuItem("替换(R)");
replaceitem.setShortcut(new MenuShortcut(KeyEvent.VK_H,false));
editmenu.add(replaceitem);
MenuItem turntoitem = new MenuItem("转到(G)");
turntoitem.setShortcut(new MenuShortcut(KeyEvent.VK_G,false));
editmenu.add(turntoitem);
editmenu.addSeparator();
//复选菜单项
Menu choicemenu = new Menu("选择(C)");
MenuItem allitem = new MenuItem("全选(A)");
allitem.setShortcut(new MenuShortcut(KeyEvent.VK_A,false));
choicemenu.add(allitem);
MenuItem fanxiangitem = new MenuItem("反向选择(B)");
fanxiangitem.setShortcut(new MenuShortcut(KeyEvent.VK_B,false));
choicemenu.add(fanxiangitem);
MenuItem chieseitem = new MenuItem("选择汉字(C)");
chieseitem.setShortcut(new MenuShortcut(KeyEvent.VK_C,false));
choicemenu.add(chieseitem);
editmenu.add(choicemenu);
//编辑菜单项的时间/日期项
MenuItem dateitem = new MenuItem("时间/日期(D)");
dateitem.setShortcut(new MenuShortcut(KeyEvent.VK_5,false));
editmenu.add(dateitem);
//添加监听器来实现编辑菜单上的各个菜单项的功能
//撤销菜单项的功能实现
undoitem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
}
});
//剪切菜单项的功能实现
cutitem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String text = content.getSelectedText();
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection, null);
if(text.length() == 0){
return;
}
else{
content.replaceRange("", content.getSelectionStart(),content.getSelectionEnd());
}
}
});
//复制菜单项的功能实现
copyitem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String text = content.getSelectedText();
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection, null);
}
});
//粘贴菜单项的功能实现
pasteitem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Transferable contents = clipboard.getContents(this);
String str =null;
try {
str = (String) contents.getTransferData(DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
if (str == null)
return;
try {
content.replaceRange(str,content.getSelectionStart(),content.getSelectionEnd());
}
catch (Exception e1) {
e1.printStackTrace();
}
}
});
//删除菜单项的功能实现
deleteitem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
content.replaceRange("",content.getSelectionStart(),content.getSelectionEnd());
}
});
//查找菜单项的功能实现
finditem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
final JDialog dialog = new JDialog(jf,"查找字符串...",true);
dialog.setBounds(560,250,310,130);
JLabel find = new JLabel("请输入字符串 :");
final JTextField findtext = new JTextField(1);
JButton jbu = new JButton("查找");
dialog.setLayout(null);
find.setBounds(10,30,90,20);
findtext.setBounds(100,30,90,20);
jbu.setBounds(200,30,80,20);
dialog.add(find);
dialog.add(findtext);
dialog.add(jbu);
jbu.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String text = content.getText();
String str = findtext.getText();
int end = text.length();
int len = str.length();
int start = content.getSelectionEnd();
if(start == end){
展开阅读全文