关于记事本的右键快捷菜单
本人菜鸟,刚写个记事本,想添加右键菜单,但是右键菜单不显示,而且状态栏也有问题,帮忙看下!!!
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();
if(fileName!=null){
writeFile(fileName);
}
}
if(e.getSource()==jcopy){
String text = jtarea.getSelectedText();
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection,null);
}
if(e.getSource()==jzhant){
Transferable content = clipboard.getContents(this);
if(content == null) return;
String text;
text = " ";
try{
text = (String)content.getTransferData(DataFlavor.stringFlavor);
}catch(Exception event){
}
jtarea.replaceRange(text,jtarea.getSelectionStart(),jtarea.getSelectionEnd());
}
}
public void itemStateChanged(ItemEvent e){
if(e.getItemSelectable()==jchuan){
jtarea.setLineWrap(!jtarea.getLineWrap());
}
//if(e.getItemSelectable()==jchuan){
// if (e.getStateChange()==ItemEvent.SELECTED) jtarea.setLineWrap(true);
// else jtarea.setLineWrap(false);
//}
}
public void mouseReleased(MouseEvent e)
{
if(e.isPopupTrigger())
{
pm.show(this,e.getX(),e.getY());
}
}
public void mouseExited(MouseEvent e){
}
public void mouseClicked(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
public void readFile(String fileName){
try{
File file = new File(fileName);
FileReader fileread = new FileReader(file);
int size = (int)file.length();
int chars = 0;
char[] content = new char[size];
while(fileread.ready()){
chars += fileread.read(content,chars,size-chars);
jtarea.setText(new String(content,0,chars));
}
}catch(IOException e){
JOptionPane.showMessageDialog(this,"Error opening file!");
}
}
public void writeFile(String fileName){
try{
File file = new File(fileName);
FileWriter filewrite = new FileWriter(file);
filewrite.write(jtarea.getText());
filewrite.close();
}catch(IOException e){
JOptionPane.showMessageDialog(this,"Error opening file!");
}
}
public static void main(String args[]){
new NotePad();
}
}
[最优解释]
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 NotePad1 extends JFrame implements ActionListener,ItemListener,MouseListener{
JMenuBar jmenubar;
JMenu jfile,jedit,jgeshi,jlook,jhelp;
JMenuItem jopen,jsave,jexit,jcopy,jzhant,jsaveas,jhuan,jsave1,jcopy1,jzhant1;
JTextArea jtarea;
FileDialog openFileDialog,saveAsFileDialog,saveFileDialog;
String fileName;
JCheckBox jchuan;
Toolkit toolkit;
Clipboard clipboard;
JPopupMenu pm;
public NotePad1(){
setTitle("记事本");
setSize(600,400);
setVisible(true);
jmenubar = new JMenuBar();
jtarea = new JTextArea();
add(jtarea,BorderLayout.CENTER);
jtarea.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("粘贴");
jsave1 = new JMenuItem("保存");
jcopy1 = new JMenuItem("复制");
jzhant1 = 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(jsave1);
pm.add(jcopy1);
pm.add(jzhant1);
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);
jcopy1.addActionListener(this);
jsave1.addActionListener(this);
jzhant1.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();
if(fileName!=null){
writeFile(fileName);
}
}
if(e.getSource()==jcopy){
String text = jtarea.getSelectedText();
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection,null);
}
if(e.getSource()==jzhant){
Transferable content = clipboard.getContents(this);
if(content == null) return;
String text;
text = " ";
try{
text = (String)content.getTransferData(DataFlavor.stringFlavor);
}catch(Exception event){
}
jtarea.replaceRange(text,jtarea.getSelectionStart(),jtarea.getSelectionEnd());
}
}
public void itemStateChanged(ItemEvent e){
if(e.getItemSelectable()==jchuan){
jtarea.setLineWrap(!jtarea.getLineWrap());
}
// if(e.getItemSelectable()==jchuan){
// if (e.getStateChange()==ItemEvent.SELECTED) jtarea.setLineWrap(true);
// else jtarea.setLineWrap(false);
// }
}
public void mouseReleased(MouseEvent e)
{
if(e.isPopupTrigger())
{
pm.show(this,e.getX(),e.getY());
}
}
public void mouseExited(MouseEvent e){
}
public void mouseClicked(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
public void readFile(String fileName){
try{
File file = new File(fileName);
FileReader fileread = new FileReader(file);
int size = (int)file.length();
int chars = 0;
char[] content = new char[size];
while(fileread.ready()){
chars += fileread.read(content,chars,size-chars);
jtarea.setText(new String(content,0,chars));
}
}catch(IOException e){
JOptionPane.showMessageDialog(this,"Error opening file!");
}
}
public void writeFile(String fileName){
try{
File file = new File(fileName);
FileWriter filewrite = new FileWriter(file);
filewrite.write(jtarea.getText());
filewrite.close();
}catch(IOException e){
JOptionPane.showMessageDialog(this,"Error opening file!");
}
}
public static void main(String args[]){
new NotePad1();
}
}