一、实验内容
实验题目:记事本应用程序设计
要求:1.分析WINDOWS“记事本程序”各主要菜单项的功能,画出详细的程序流程图
2.设计完整的UI,并实现“文件”菜单中“新建”“打开”“保存”“另存为”“退出”等 主要菜单项功能
3.将各主要菜单项的程序流程图及主要代码写入实验报告
二、实验过程
1、写出程序的程序流程图,主要是各个功能的程序流程图
新建 打开
N
Y
N YY N
保存 另存
N Y
退出
Y N
2、根据程序流程图写出各个方法,新建,打开,保存,另存,退出。
三、实验结果
(应先列出测试数据,要写明实验的现象,实验数据的处理等。对于实验结果的表述,一般有三种方法:文字叙述(根据实验目的将实验结果系统化、条理化,用准确的专业术语客观地描述实验现象和结果,要有时间顺序以及各项指标在时间上的关系),图表(用表格或坐标图的方式使实验结果突出、清晰,便于相互比较,尤其适合于分组较多,且各组观察指标一致的实验,使组间异同一目了然;每一图表应有表目和计量单位,应说明一定的中心问题),屏幕截图(实验结果也可以是屏幕截图,充分表明实验的实际情况)。在实验报告中,可任选其中一种或几种方法并用,以获得最佳效果。)
1、记事本界面布局
2、功能描述
a、新建:若记事本内容经过修改则提示保存;否则直接初始化记事本
b、打开:选择打开的文件,将文件内容读出,写入记事本
c、保存:若为第一次保存,调用另存函数;否则将文件内容存为新的记事本内容
d、另存:选择另存路径,将记事本内容写入文件
e、退出:若未保存或经过修改,则提示保存,调用保存函数,再退出;否则直接退出
四、讨论与分析
1、实验纠错:
a、实验中,出现打开对话框出现两次的情况,即文件选择之后又弹出对话框,需要再选择一次,但是程序中并未出现两次调用。重新建立工程之后才解决这个错误,不过仍然不知道错误出现在哪里。
实验中,大括号的对应非常重要,应该一一对应,不然很多地方都会报错。应该注意括号的范围。
b、文件都是保存在当前目录下。之前的程序没有保存在当前目录,有些功能就有错,如保存功能不能实现等等。所以后来都换成当前目录打开,保存。
2、实验结果:
实验结果与预期基本一致能实现新建,打开,保存,另存,退出功能。但是功能很混乱,还有很多功能没有实现或者没有考虑到。如,打开就没有再判断是否修改过文件,提示保存。
单个运行各功能问题不大,但连续运行就会出现很多不够严谨的地方。
五、附录:关键代码
以下是记事本程序的主要功能代码:
String str = new String //设了一个全局变量str保存每次功能结束后记事本的内容,便于比较记事本内容是否经过修改。
新建功能:
void New_actionPerformed(ActionEvent e) {
if (! (jTextArea1.getText().equals(str))) {
int i = JOptionPane.showConfirmDialog(null, "文件已经更改,想保存文件吗?", "记事本",
JOptionPane.YES_NO_OPTION);
if (i == JOptionPane.YES_OPTION) {
exit_actionPerformed(e);
}
else if (i == JOptionPane.NO_OPTION) {
jTextArea1.setText("");
this.setTitle("无标题--记事本");
}
}
else {
jTextArea1.setText("");
this.setTitle("无标题--记事本");
}
}
打开功能 :
void open_actionPerformed(ActionEvent e) {
JFileChooser jfc = new JFileChooser(".");
// JFileChooser jfc = null;
File curFile;
int ret = jfc.showOpenDialog(null);
if (ret == jfc.APPROVE_OPTION) {
curFile = jfc.getSelectedFile();
String name = curFile.getName();
this.setTitle(name);
try {
FileReader fr = new FileReader(curFile);
BufferedReader bf = new BufferedReader(fr);
String s = bf.readLine();
jTextArea1.setText(s + "\n");
while (s != null) {
s = bf.readLine();
jTextArea1.getText();
if (s != null) {
jTextArea1.setText(jTextArea1.getText() + "\n" + s);
}
}
}
catch (Exception ex1) {
}
}
str = jTextArea1.getText();
}
保存功能:
void save_actionPerformed(ActionEvent e) {
JFileChooser jfc = new JFileChooser(".");
File file = new File(this.getTitle());
if (! (file.exists())) {
save_as_actionPerformed(e);
}
else {
FileWriter fw = null;
try {
fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(jTextArea1.getText());
if (bw != null) {
bw.close();
}
}
catch (Exception ex) {
}
}
str = jTextArea1.getText();
}
另存功能:
void save_as_actionPerformed(ActionEvent e) {
JFileChooser jfc = new JFileChooser(".");
int ret = jfc.showSaveDialog(this);
if (ret == jfc.APPROVE_OPTION) {
File file = jfc.getSelectedFile();
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(file));
String buff = jTextArea1.getText();
bw.write(buff);
if (bw != null) {
bw.close();
}
}
catch (Exception ex2) {
}
String name = file.getName();
this.setTitle(name);
}
else {
jTextArea1.setText(jTextArea1.getText());
}
str = jTextArea1.getText();
}
退出功能:
//File | Exit action performed
public void exit_actionPerformed(ActionEvent e) {
String s = new String();
s = jTextArea1.getText();
if (s.equals(str)) {
System.exit(0);
}
else {
int i = JOptionPane.showConfirmDialog(null, "文件已经更改,想保存文件吗?", "记事本",
JOptionPane.YES_NO_OPTION);
if (i == 0) {
save_actionPerformed(e);
}
else {
System.exit(0);
}
}
}
第二篇:记事本java代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Object.*;
import java.lang.*;
import java.io.File.*;
import java.io.*;
import java.util.Date;
import java.text.*;
import java.awt.FileDialog;
public class myNote extends JFrame implements
ActionListener,ItemListener,MouseListener{
private JFrame frm = new JFrame();//创建窗体
static JTextArea tx = new JTextArea(100,100);//创建文本范围
static Label lasthang = new Label();//创建最后一行标签
static JMenuBar mb = new JMenuBar(); //创建菜单条
static JDialog jd ;//创建对话框
static JScrollPane jsp = new JScrollPane(tx);//创建滚动面板
static JTextField jtf = new JTextField();//创建单行文本
static JTextField jtft = new JTextField();
static Checkbox cb1 = new Checkbox("区分大小写");//查找的框内容
static Checkbox ckb1_1 = new Checkbox("向上",true);//创建复选框以及选中状态 static Checkbox ckb1_2 = new Checkbox("向下",true);
static Button jb1 = new Button("查找下一个");//创建按钮
static Button jb2 = new Button(" 取消 ");
static Button jb3 = new Button(" 替换 ");
static Button jb4 = new Button(" 全部替换 ");
static CheckboxGroup grp = new CheckboxGroup();//复选框组,只能有一个被选中 static ImageIcon icon=new ImageIcon("resource/myNote.jpg");//创建图片 static String dress = " ";//初始化为空的
static String Data = "";
static String fuzhistr = "";//复制的字符串
static String czstr = "";
static String strall = "";//所有字符串
static int czD = 0;//是否已经弹出查找对话框 0 否
static int thD = 0;//是否已经弹出替换对话框 0 否
int xjSign = 0;//新建标记
int jian = 0;//剪切标记为了区别复制
int set = 0; //查找位置
int fangxiang = 0;//方向 0:向下 1:向上
int qufen = 1;//区分大小写 0:区分 1:不区分
int czSign = 0;
static boolean ishh=false;//是否换行的一个全局变量
public myNote()//构造方法初始化
{
frm.setTitle("记事本");//窗体名称
frm.add(jsp);//将滚动面板添加到窗体上
tx.setEditable(false);//文本不可编辑
tx.addMouseListener(this);//添加鼠标点击的监听器
tx.setSelectionColor(new Color(100,150,250));//对颜色进行设置,获取用于呈现选定的当前颜色
jsp.setViewportView(tx);//滚动窗格的视口子级
frm.getContentPane().add(jsp,BorderLayout.CENTER);//添加,处在中间位置 JMenuBar jmb = new JMenuBar();//设置菜单条
JMenu jm1 = new JMenu("文件(F)");//创建菜单
String[] label1 = { "新建", "打开", "保存", "另存为", "退出" };//数组存放菜单目录
JMenuItem[] jmi1 = new JMenuItem[label1.length];//创建一个有label1长度的菜单项数组
for (int i = 0; i < jmi1.length; i++) {//将菜单目录赋值给菜单项 jmi1[i] = new JMenuItem(label1[i]);
jm1.add(jmi1[i]);//将菜单项添加到菜单上
jmi1[i].addActionListener(this);//添加监听器
}
jm1.insertSeparator(1);//添加中间的分割线
jm1.insertSeparator(3);
jm1.insertSeparator(6);
JMenu jm2 = new JMenu("编辑(E)");
String[] label2 = { "撤销", "剪切", "复制", "粘贴", "删除", "查找", "替换","全选" ,"时间/日期"};
JMenuItem[] jmi2 = new JMenuItem[label2.length];
for (int i = 0; i < jmi2.length; i++) {
jmi2[i] = new JMenuItem(label2[i]);
jm2.add(jmi2[i]);
jmi2[i].addActionListener(this);
}
jm2.insertSeparator(1);
jm2.insertSeparator(5);
jm2.insertSeparator(7);
jm2.insertSeparator(10);
JMenu jm3 = new JMenu("格式(O)");
JMenuItem jmi3 = new JMenuItem("自动换行");
jmi3.addActionListener(this);
JMenu jm3_1 = new JMenu("字体大小");
String[] label3_1 = { "10", "20", "30", "40", "50" }; JMenuItem[] jmi3_1 = new JMenuItem[label3_1.length];
for (int i = 0; i < jmi3_1.length; i++) {
jmi3_1[i] = new JMenuItem(label3_1[i]);
jm3_1.add(jmi3_1[i]);
jmi3_1[i].addActionListener(this);
}
JMenu jm3_2 = new JMenu("字体颜色");
String[] label3_2 = { "红色", "绿色", "蓝色", "品红", "桔黄" }; JMenuItem[] jmi3_2 = new JMenuItem[label3_2.length];
for (int i = 0; i < jmi3_2.length; i++) {
jmi3_2[i] = new JMenuItem(label3_2[i]);
jm3_2.add(jmi3_2[i]);
jmi3_2[i].addActionListener(this);
}
JMenu jm4 = new JMenu("帮助(H)");
JMenuItem jmi4 = new JMenuItem("关于");
jm4.add(jmi4);//将菜单项添加到菜单上
jmi4.addActionListener(this);
jm3.add(jmi3);
jm3.add(jm3_1);
jm3.add(jm3_2);
jmb.add(jm1);
jmb.add(jm2);
jmb.add(jm3);
jmb.add(jm4);
//添加监听器
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
cb1.addItemListener(this);
ckb1_1.addItemListener(this);
ckb1_2.addItemListener(this);
//设置窗体属性
frm.setJMenuBar(jmb);//将菜单条添加到窗体上
frm.pack();//自动计算其大小
frm.setSize(950,540);//设置其大小
frm.setLocation(160,120);//设置初始位置
frm.setVisible(true);//设置可见为真
frm.setIconImage(icon.getImage());//设置添加图片
frm.addWindowListener(new WindowAdapter() {//添加窗体监听器以及其实现的行为
public void windowClosing(WindowEvent e) {
frm.removeAll();//删除所有
frm.dispose();
}
});
}
public static void main(String args[]) throws IOException
{
new myNote();//通过构造方法来创建窗体
}
//进行文件选项下的操作
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e)//实现ActionListener接口方法 {
String comm = e.getActionCommand();//获取命令
Font f = tx.getFont();//字体=获取的字体
String name = f.getName();//得到名字
int style = f.getStyle();//字体格式
int size = f.getSize();//字体大小
if(comm.equals("新建"))//如果事件名称=“新建”
{
dress = " ";//文件名=空
tx.setText("");//全文初始内容为空
xjSign = 1;//新建标志为1,真
tx.setEditable(true);//可编辑为真
frm.setTitle("无标题-记事本"); //此时窗体名称显示
}
if(comm.equals("打开")) //如果选择打开
{
xjSign = 1;//新建为真
openFile();//打开文件方法
}
if(comm.equals("保存"))//选择保存
{
if(dress != " ")//如果不为空
{
try{//捕获异常
FileOutputStream fo = new FileOutputStream(dress);//文件名 String str = tx.getText();//获取文本内容
fo.write(str.getBytes()); //输入文件名
fo.close();//关闭文件流
}
catch(Exception h){}
}
else if(dress == " "&&xjSign == 1)//文件名为空且新建为真时 {
FileDialog fd = new FileDialog(frm,"保存",FileDialog.SAVE);//显示对话框,保存
fd.show();//进行文件项下的操作
saveFile(fd);//保存文件
}
else
{
JOptionPane.showMessageDialog(null,"你还未新建文本文档!");//显示对话框进行提示
}
}
if(comm.equals("另存为"))//选择另存为
{
if(xjSign == 1)//如果新建为真
{
FileDialog fd = new FileDialog(frm,"另存为",FileDialog.SAVE);//显示对话框,保存
fd.show();
saveFile(fd);//保存文件
}
else
{
JOptionPane.showMessageDialog(null,"你还未新建文本文档!");//显示对话框进行提示
}
}
if(comm.equals("退出")) //选择退出
{
frm.removeAll();//删除所有
frm.dispose();//关闭窗体
}
if(comm.equals("撤销")) //选择撤销
{
String change = tx.getText();//获取文本内容
tx.setText(Data); //设置文本内容
int len1 = 0,len2 = 0;//定义变量,用于比较
len1 = change.length();//将获取的文本长度赋值
len2 = Data.length();//选取的文本内容的长度
if(len2>len1)//如果选择的长度比初始的长的话
{
tx.select(len1,len2);
}
else
{
tx.select(len2,len1);
}
Data = change;//将选取的赋值给文本
}
if(comm.equals("剪切")) //选择剪切
{
jian = 1;//标记为真
fuzhistr = tx.getSelectedText();//将选择的文本赋值给fuzhistr tx.replaceSelection("");//将选中的文本用空白代替
}
if(comm.equals("复制")) //选择复制
{
jian = 0;//剪切为假
fuzhistr = "";//fuzhistr为空
fuzhistr = tx.getSelectedText();//fuzhistr=选中的文本 tx.copy();//copy方法实现
}
if(comm.equals("粘贴")) //选择粘贴
{
int start = tx.getCaretPosition();//获取初始位置
tx.insert(fuzhistr,start);//插入需要粘贴的文本
if(jian == 1)//如果剪切为真的话
fuzhistr = "";//fuzhistr为空
}
if(comm.equals("删除")) //选择删除
{
tx.replaceSelection("");//将选中的部分用空
}
if(comm.equals("查找")) //选择查找
{
if(thD == 0)//未弹出对话框
{
set = 0;//从初始开始查找
qufen = 1;//是否区分大小写为真
czD = 1;//查找
search();//查找方法
}
}
if(e.getSource() == jb1)//查找
{
String czstr1 = "";//查找字符串为空
String strall1 = "";//全部为空
int n = 0;
czstr = tx.getText();//查找的字符串为输入的文本
strall = jtf.getText();//查找范围是全文
czstr1 = czstr.toLowerCase();//不区分大小写,这将字母转换为小写 strall1 = strall.toLowerCase();//将查找范围的文本也设为小写 if(fangxiang == 0)//查找方向向上
{
if(qufen == 0)//不区分大小写
{
if(czSign == 0)//查找为假
{
n = czstr.indexOf(strall, set);//进行查找
}
else
{
n = czstr.indexOf(strall, set + strall.length());//长度
}
}
else//区分大小写
{
if(czSign == 0)
{
n = czstr1.indexOf(strall1, set);
}
else
{
n = czstr1.indexOf(strall1, set + strall.length());
}
}
}
else//查找方向向下,进行同样的思想进行编写
{
czSign = 1;
if(qufen == 0)
{
n = czstr.lastIndexOf(strall, set - strall.length());//字符长度 }
else
{
n = czstr1.lastIndexOf(strall1, set - strall.length());
}
}
if (n != -1)
{
if(fangxiang == 0)
{
set = n + 1;
}
else
{
set = n - 1;
}
tx.setSelectionStart(n);
tx.setSelectionEnd(n + strall.length());
}
else
{
JOptionPane.showMessageDialog(null,"找不到" + strall);//显示没有找到的对话框
}
}
if(e.getSource() == jb2)//取消
{
jd.removeAll();
jd.dispose();//退出
czD = 0;
thD = 0;
}
if(comm.equals("替换"))//选择替换
{
if(czD == 0)//未弹出查找对话框
{
set = 0;
qufen = 1;
thD = 1;//显示替换对话框
tihuan();//进行tihuan()方法
}
}
if(e.getSource() == jb3)//替换部分
{
String str = jtft.getText();
int start = set - 1;
tx.replaceSelection(str);//实现替换
tx.select(start,start + str.length());
}
if(e.getSource() == jb4)//全部替换
{
String czstr1 = "";//查找的字符串为空
String strall1 = "";//查找范围也为空
String str1 = "";//替换为
int n1 = 0,n2 = 0;//长度
int set1 = 0;
czstr = tx.getText();//获取查找字符串
strall = jtf.getText();//查找范围
str1 = jtft.getText();//替换的文本
czstr1 = czstr.toLowerCase();//转换为小写
strall1 = strall.toLowerCase();//转换为小写
if(qufen == 0)//不区分
{
n2 = czstr.lastIndexOf(strall);//从后往前查找
}
else
{
n2 = czstr1.lastIndexOf(strall1);
}
while(set1 <= n2)
{
if(qufen == 0)
{
n1 = czstr.indexOf(strall, set1);//从开始进行
}
else
{
n1 = czstr1.indexOf(strall1, set1);
}
if (n1 != -1)
{
set1 = n1 + 1;
tx.setSelectionStart(n1);//选中开始
tx.setSelectionEnd(n1 + strall.length());//选中结束 tx.replaceSelection(str1);//替换字符串
}
}
}
if(comm.equals("全选")) //全选
{
tx.selectAll();//选中全部
}
if(comm.equals("时间/日期")) //选中时间/日期
{
int start = tx.getCaretPosition();//获取的位置
Date d = new Date();//创建日期
SimpleDateFormat sdf = new SimpleDateFormat("kk:mm yyyy/MM/dd");//设置日期格式
String str = sdf.format(d); // tx.insert(str,start);//插入日期 }
if(comm.equals("自动换行")) //选中自动换行 {
if (ishh==false){
ishh=true;
tx.setLineWrap(ishh);//自动换行为真
}
else{
ishh=false;
tx.setLineWrap(ishh);//自动换行为假
}
}
if(comm.equals("10")) //对字体大小进行设置 {
tx.setFont(new Font(name,style,10));//实现 }
if(comm.equals("20")) //同上
{
tx.setFont(new Font(name,style,20));
}
if(comm.equals("30")) //同上
{
tx.setFont(new Font(name,style,30));
}
if(comm.equals("40")) //同上
{
tx.setFont(new Font(name,style,40));
}
if(comm.equals("50")) //同上
{
tx.setFont(new Font(name,style,50));
}
if(comm.equals("红色")) //设置字体颜色的方法 {
tx.setForeground(Color.red);//实现
}
if(comm.equals("绿色")) //同上
{
tx.setForeground(Color.green);
}
if(comm.equals("蓝色")) //同上
{
tx.setForeground(Color.blue);
}
if(comm.equals("品红")) //同上
{
tx.setForeground(Color.pink);
}
if(comm.equals("桔黄")) //同上
{
tx.setForeground(Color.orange);
}
if(comm.equals("关于")) //选中关于
{
jd = new JDialog(frm,"关于");//显示对话框;
JTextArea text = new JTextArea();//输入文本
try{
FileInputStream io = new FileInputStream("resource/guanyu.txt");//得到路径
byte a[] = new byte[io.available()];//获取数组
io.read(a);//读
io.close();//关闭
String str = new String(a);//字符串
text.setFont(new Font(name,style,15));//设置字体
text.setText(str);//将字符串写入文本中
}catch(Exception g){}//捕获异常
jd.add(text);//添加文本内容
text.setEditable(false);//不可编辑
jd.setLocation(400, 240);//设置位置
jd.setSize(300,140);//设置大小
jd.show();
}
}
public void openFile()//打开文件的方法
{
dress = " ";//文件名为空
Font f = tx.getFont();//字体为文件字体
String name = f.getName();//获取名字
int style = f.getStyle();//获取字体
FileDialog fd = new FileDialog(frm, "打开");//显示对话框
fd.show();
if(fd.getFile() == null) //如果没有选择文件,则返回
{
return;
}
frm.setTitle(fd.getFile() + "-记事本");//将文件按此格式命名
try{
dress = fd.getDirectory() + File.separator + fd.getFile();//取得文件名[包括路径];
FileInputStream io = new FileInputStream(dress);//得到路径 byte a[] = new byte[io.available()];
io.read(a);
io.close();//关闭文件流
String str = new String(a);
tx.setFont(new Font(name,style,15));
tx.setEditable(true);
Data = str;
tx.setText(str);
}
catch(Exception g){}
}
//同上
public void saveFile(FileDialog fd)//保存||另存为
{
if(fd.getFile() == null)
{
return;
}
String filepath = fd.getDirectory() + File.separator + fd.getFile(); //获取保存或另存为
try{
FileOutputStream fo = new FileOutputStream(filepath);
String str = tx.getText();
frm.setTitle(fd.getFile() + "-记事本");
fo.write(str.getBytes());
fo.close();
}
catch(Exception h){}
}
public void search()//查找方法
{
jd = new JDialog(frm,"查找");//显示对话框
Label lb1 = new Label("查找内容:");//标签显示查找内容
jd.setLayout(null);//布局管理器
jd.add(lb1);//添加标签
jd.add(jtf);//添加
jd.add(jb1);//同上
jd.add(jb2);//同上
jd.add(cb1);//同上
jd.add(ckb1_1);//同上
jd.add(ckb1_2);//同上
ckb1_1.setCheckboxGroup(grp);//复选框组,表示只能选中一个 ckb1_2.setCheckboxGroup(grp);//同上
grp.setSelectedCheckbox(ckb1_2);//同上
jd.pack();//自动计算其大小
jd.setResizable(false);//不可调节
jd.setSize(400, 110);//设置大小
lb1.setBounds(20,10,70,25);//设置窗体位置和大小 jtf.setBounds(90,10,160,25);//同上
jb1.setBounds(270,10,100,25);//同上
jb2.setBounds(270,45,100,25);//同上
cb1.setBounds(20,45,90,25);//同上
ckb1_1.setBounds(120,45,60,25);//同上
ckb1_2.setBounds(200,45,60,25);//同上
jd.addWindowListener(new WinLis());//添加监听器 jtf.paste();//粘贴方法
jd.setLocation(400, 240);//设置位置
jd.show();
}
// 同上
public void tihuan()//替换方法
{
jd = new JDialog(frm,"替换");//显示对话框
Label lb1 = new Label("查找内容:");//文本内容
Label lb2 = new Label("替换为: ");//同上
jd.setLayout(null);
jd.add(lb1);
jd.add(lb2);
jd.add(jtf);
jd.add(jtft);
jd.add(cb1);
jd.add(jb1);
jd.add(jb2);
jd.add(jb3);
jd.add(jb4);
jd.pack();
jd.setResizable(false);
jd.setSize(400, 145);
cb1.setBounds(20,80,90,20);
lb1.setBounds(20,10,70,20);
lb2.setBounds(20,35,70,20);
jtf.setBounds(90,10,160,20);
jtft.setBounds(90,35,160,20);
jb1.setBounds(270,10,100,20);
jb3.setBounds(270,35,100,20);
jb2.setBounds(270,85,100,20);
jb4.setBounds(270,60,100,20);
jd.setLocation(400, 240);
jtf.paste();
jd.addWindowListener(new WinLis());
jd.show();
}
// 实现监听器事件
public void itemStateChanged(ItemEvent e)
{
if(ckb1_1.getState() == true)
{
fangxiang = 1;//向上
}
if(ckb1_2.getState() == true)
{
fangxiang = 0;//向下
}
if(cb1.getState() == true)
{
qufen = 0;//区分大小写
}
if(cb1.getState() == false)
{
qufen = 1;//不区分大小写
}
}
static class WinLis extends WindowAdapter//适配器实现关闭方法 {
public void windowClosing(WindowEvent e)
{
czD = 0;
thD = 0;
}
}
//鼠标右键功能
public void mouseClicked(MouseEvent e)
{
JPopupMenu pp = new JPopupMenu();//弹出对话框
if (e.getButton() == 3)
{
JMenuItem jmi1,jmi2,jmi3,jmi4;//添加菜单项
pp.add(jmi1 = new JMenuItem("剪切"));
pp.add(jmi2 = new JMenuItem("复制"));
pp.add(jmi3 = new JMenuItem("粘贴"));
pp.addSeparator();
pp.add(jmi4 = new JMenuItem("全选"));
jmi1.addActionListener(this);
jmi2.addActionListener(this);
jmi3.addActionListener(this);
jmi4.addActionListener(this);
pp.pack();
frm.add(pp);
pp.setVisible(true);
pp.show(e.getComponent(), e.getX(), e.getY());//显示 }
}
//实现没有用到的抽象方法
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}