日期:2014-05-20  浏览次数:20732 次

关于记事本的右键快捷菜单
本人菜鸟,刚写个记事本,想添加右键菜单,但是右键菜单不显示,而且状态栏也有问题,帮忙看下!!!

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.event.InputEvent.*;
import java.awt.datatransfer.*;
public class NotePad extends JFrame implements ActionListener,ItemListener,MouseListener{
JMenuBar jmenubar;
JMenu jfile,jedit,jgeshi,jlook,jhelp;
JMenuItem jopen,jsave,jexit,jcopy,jzhant,jsaveas,jhuan;
JTextArea jtarea;
FileDialog openFileDialog,saveAsFileDialog,saveFileDialog;
String fileName;
JCheckBox jchuan;
Toolkit toolkit;
Clipboard clipboard;
JPopupMenu pm; 
public NotePad(){
setTitle("记事本");
setSize(600,400);
setVisible(true);
jmenubar = new JMenuBar();
jtarea = new JTextArea();
add(jtarea,BorderLayout.CENTER);
addMouseListener(this); 
jfile = new JMenu("文件(F)");
jedit = new JMenu("编辑(E)");
jgeshi = new JMenu("格式(O)");
jlook = new JMenu("查看(V)");
jhelp = new JMenu("帮助(H)");
jopen = new JMenuItem("打开");

jsave = new JMenuItem("保存");
jsaveas = new JMenuItem("另存为");
jexit = new JMenuItem("退出");
jcopy = new JMenuItem("复制");
jzhant = new JMenuItem("粘贴");
// jhuan = new JMenuItem("自动换行");
jchuan = new JCheckBox("自动换行");
jmenubar.add(jfile);
jmenubar.add(jedit);
jmenubar.add(jgeshi);
jmenubar.add(jlook);
jmenubar.add(jhelp);
jfile.add(jopen);
jfile.add(jsave);
jfile.addSeparator();
jfile.add(jexit);
jedit.add(jcopy);
jedit.add(jzhant);
// jgeshi.add(jhuan);
jgeshi.add(jchuan);



setJMenuBar(jmenubar);
pm=new JPopupMenu(); 

pm.add(jsave); 
pm.add(jcopy); 
pm.add(jzhant); 

toolkit = Toolkit.getDefaultToolkit();
clipboard = toolkit.getSystemClipboard();
JScrollPane scroll = new JScrollPane(jtarea);
getContentPane().add(scroll,BorderLayout.CENTER);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

validate();

jopen.addActionListener(this);
jsave.addActionListener(this);
jexit.addActionListener(this);
// jhuan.addActionListener(this);
jchuan.addItemListener(this);
jcopy.addActionListener(this);
jzhant.addActionListener(this);
jfile.setMnemonic('F');
jedit.setMnemonic('E');
jgeshi.setMnemonic('O');
 jopen.setAccelerator(KeyStroke.getKeyStroke('O',Event.CTRL_MASK));
 jsave.setAccelerator(KeyStroke.getKeyStroke('S',Event.CTRL_MASK));
 jcopy.setAccelerator(KeyStroke.getKeyStroke('C',Event.CTRL_MASK));
 jzhant.setAccelerator(KeyStroke.getKeyStroke('V',Event.CTRL_MASK));
openFileDialog = new FileDialog(this,"打开文件",FileDialog.LOAD);
saveFileDialog = new FileDialog(this,"保存文件",FileDialog.SAVE);
}

public void actionPerformed(ActionEvent e){
if(e.getSource()==jexit){
System.exit(0);
}
if(e.getSource()==jopen){
openFileDialog.show();
fileName = openFileDialog.getDirectory()+openFileDialog.getFile();
if(fileName!=null){
readFile(fileName);
}
}
if(e.getSource()==jsave){
saveFileDialog.show();
fileName = saveFileDialog.getDirectory()+saveFileDialog.getFile();