求助,Java写的记事本打开保存复制粘贴功能无法实现
用Java编的记事本,打开保存复制剪切粘贴功能都有相关代码,可是运行时候无法实现,不知怎么回事,求大神解答,在线等…… 代码如下import java.awt.datatransfer.*;
import java.lang.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class TestMenu2 {
public static void main(String args[]){
MyMenuFrame mf=new MyMenuFrame();
}
}
class MyMenuFrame extends Frame //implements ActionListener //定义窗口类
{
String s,s1;
MenuBar m_MenuBar;
Menu menuFile,menuEdit,menuForm, sub_Form_Style,sub_Form_Size;
MenuItem mi_File_Open,mi_File_Save,mi_File_Exit;
MenuItem mi_Edit_Cut,mi_Edit_Copy,mi_Edit_Paste;
CheckboxMenuItem mi_Form_Style,mi_Form_Style1;
CheckboxMenuItem mi_Form_Size,mi_Form_Size1;
Clipboard clipboard=null;
PopupMenu popM;
MenuItem pi_Left,pi_Center,pi_Right;
TextArea ta;
String ta_Font_Name;
int ta_Font_Style,new_Style,ta_Font_Size,new_Size;
FileDialog fd;
MyMenuFrame()
{
super("我的记事本");
ta=new TextArea(5,30);
ta.setFont(new Font("TimesRoman",Font.PLAIN,18));
ta.addMouseListener(new HandleMouse());
add(ta,BorderLayout.CENTER);
m_MenuBar=new MenuBar();
menuFile=new Menu("文件");
mi_File_Open=new MenuItem("打开(O)");
mi_File_Open.setShortcut(new MenuShortcut(KeyEvent.VK_O));
mi_File_Save=new MenuItem("保存(S)");
mi_File_Save.setShortcut(new MenuShortcut(KeyEvent.VK_S));
mi_File_Exit=new MenuItem("退出");
mi_File_Exit.addActionListener(new HandleAct());
mi_File_Save.addActionListener(new HandleAct());
mi_File_Open.addActionListener(new HandleAct());
menuFile.add(mi_File_Open);
menuFile.add(mi_File_Save);
menuFile.addSeparator();
menuFile.add(mi_File_Exit);
m_MenuBar.add(menuFile);
menuEdit=new Menu("编辑");
mi_Edit_Cut=new MenuItem("剪切(X)");
mi_File_Cut.setShortcut(new MenuShortcut(KeyEvent.VK_X));
mi_Edit_Copy=new MenuItem("复制(C)");
mi_File_Copy.setShortcut(new MenuShortcut(KeyEvent.VK_C));
mi_Edit_Paste=new MenuItem("粘贴(V)");
mi_File_Paste.setShortcut(new MenuShortcut(KeyEvent.VK_V));
mi_Edit_Cut.addActionListener(new HandleAct());
mi_Edit_Copy.addActionListener(new HandleAct());
mi_Edit_Paste.addActionListener(new HandleAct());
menuEdit.add(mi_Edit_Cut);
menuEdit.add(mi_Edit_Copy);
menuEdit.add(mi_Edit_Paste);
menuEdit.addSeparator();
m_MenuBar.add(menuEdit);
menuForm=new Menu("格式");
sub_Form_Style=new Menu("字体");
sub_Form_Size=new Menu("字号");
mi_Form_Style=new CheckboxMenuItem("粗体");
mi_Form_Style1=new CheckboxMenuItem("斜体");
mi_Form_Size=new CheckboxMenuItem("大字体");
mi_Form_Size1=new CheckboxMenuItem("一般");
sub_Form_Style.add(mi_Form_Style);
sub_Form_Style.add(mi_Form_Style1);
sub_Form_Size.add(mi_Form_Size);
sub_Form_Size.add(mi_Form_Size1);
menuForm.add(sub_Form_Style);
menuForm.add(sub_Form_Size);
m_MenuBar.add(menuForm);
mi_Form_Style.addItemListener(new HandleItem1());
mi_Form_Style1.addItemListener(new HandleItem2());
mi_Form_Size.addItemListener(new HandleItem3());
mi_Form_Size1.addItemListener(new HandleItem4());
this.setMenuBar(m_MenuBar);
popM=new PopupMenu();
pi_Left=new MenuItem("左对齐");
pi_Left.addActionListener(new HandleAct());
popM.add(pi_Left);
pi_Center=new MenuItem("居中");
pi_Center.addActionListener(new HandleAct());
popM.add(pi_Center);
pi_Right=new MenuItem("右对齐");
pi_Right.addActionListener(new HandleAct());
popM.add(pi_Right);
ta.add(popM);
addWindowListener(new HandleClose());
setSize(700,500);
setVisible(true);
ta_Font_Name=(ta.getFont()).getName();
ta_Font_Style=(ta.getFont()).getStyle();
new_Style=ta_Font_Style;
ta_Font_Size=(ta.getFont()).getSize();
new_Size=ta_Font_Size;
clipboard=getToolkit().getSystemClipboard();
}
class HandleAct implements ActionListener{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="退出"){
dispose();
System.exit(0);
}
else if(e.getActionCommand()=="打开"){
fd=new FileDialog(new MyMenuFrame(),"open",FileDialog.LOAD);
fd.setDirectory("d:\\temp");
fd.setVisible(true);
try{
File myfile=new File(fd.getDirectory(),fd.getFile());
RandomAccessFile raf=new RandomAccessFile(myfile,"r");
while(raf.getFilePointer()<raf.length()){
ta.append(raf.readLine()+"\n"); //阅读文件并加载到ta中
}
}catch(IOException ioe)
{
System.err.println(ioe.toString());
}
}
else if (e.getActionCommand()=="保存"){
FileDialog fd=new FileDialog(new MyMenuFrame(),"另存为",FileDialog.SAVE);
fd.setFile("*.txt");
fd.setVisible(true);
String filePath=fd.getDirectory();
String fileName=fd.getFile();
try{
FileWriter fw=new FileWriter(filePath+fileName);
BufferedWriter bw=new BufferedWriter(fw);
PrintWriter pw=new PrintWriter(bw);
pw.println(ta.getText());
pw.flush();
pw.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
else if(e.getActionCommand()=="剪切"){
String temp=ta.getSelectedText();
StringSelection text=new StringSelection(temp);
clipboard.setContents(text,null);
int start=ta.getSelectionStart();
int end=ta.getSelectionEnd();
ta.replaceRange("",start,end);
}
else if(e.getActionCommand()=="复制"){
//s=ta.getSelectedText();
String temp=ta.getSelectedText();
StringSelection text=new StringSelection(temp);
clipboard.setContents(text,null);
}
else if(e.getActionCommand()=="粘贴"){
// ta.append(s1);
Transferable contents=clipboard.getContents(this);
DataFlavor flavor=DataFlavor.stringFlavor;
if(contents.isDataFlavorSupported(flavor)){
try{
String str;
str=(String)contents.getTransferData(flavor);
ta.append(str);
}catch(Exception ee){}
}
}
else if(e.getActionCommand()=="字号"){
ta.setFont(new Font("宋体",Font.PLAIN,24));
}
else if(e.getActionCommand()=="字体"){
ta.setFont(new Font("宋体",Font.BOLD,18));
}
}
}
class HandleMouse extends MouseAdapter
{
public void mouseReleased(MouseEvent e){
if(e.isPopupTrigger())
popM.show((Component)e.getSource(),e.getX(),e.getY());
}
}
class HandleItem1 implements ItemListener
{
public void itemStateChanged(ItemEvent e){
if(((CheckboxMenuItem)e.getSource()).getState())
{
ta.setFont(new Font("Courier",Font.BOLD,18));
}
}
}
class HandleItem2 implements ItemListener
{
public void itemStateChanged(ItemEvent e){
if(((CheckboxMenuItem)e.getSource()).getState())
{
ta.setFont(new Font("Arial",Font.ITALIC,18));
}
}
}
class HandleItem3 implements ItemListener
{
public void itemStateChanged(ItemEvent e){
new_Size=ta_Font_Size*2;
ta.setFont(new Font(ta_Font_Name,new_Style,new_Size));
}
}
class HandleItem4 implements ItemListener
{
public void itemStateChanged(ItemEvent e){
new_Size=ta_Font_Size;
ta.setFont(new Font(ta_Font_Name,new_Style,new_Size));
}
}
class HandleClose extends WindowAdapter
{
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
}
}
--------------------编程问答-------------------- mi_File_Cut
mi_File_Copy
mi_File_Paste
这几个的定义在哪里? --------------------编程问答-------------------- 调试一下了,太长了,怎么看啊。 --------------------编程问答-------------------- 怎么还是错误代码。整理了下,能执行,但是你的提问稍后再看。
import java.awt.BorderLayout;--------------------编程问答-------------------- if (e.getActionCommand() == "打开") 错误在这里
import java.awt.CheckboxMenuItem;
import java.awt.Component;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.PopupMenu;
import java.awt.TextArea;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
public class TestMenu2 {
public static void main(String[] args) {
new MyMenuFrame();
}
}
class MyMenuFrame extends Frame // implements ActionListener //定义窗口类
{
private static final long serialVersionUID = 1L;
String s, s1;
MenuBar m_MenuBar;
Menu menuFile, menuEdit, menuForm, sub_Form_Style, sub_Form_Size;
MenuItem mi_File_Open, mi_File_Save, mi_File_Exit;
MenuItem mi_Edit_Cut, mi_Edit_Copy, mi_Edit_Paste;
CheckboxMenuItem mi_Form_Style, mi_Form_Style1;
CheckboxMenuItem mi_Form_Size, mi_Form_Size1;
Clipboard clipboard = null;
PopupMenu popM;
MenuItem pi_Left, pi_Center, pi_Right;
TextArea ta;
String ta_Font_Name;
int ta_Font_Style, new_Style, ta_Font_Size, new_Size;
FileDialog fd;
MyMenuFrame() {
super("我的记事本");
ta = new TextArea(5, 30);
ta.setFont(new Font("TimesRoman", Font.PLAIN, 18));
ta.addMouseListener(new HandleMouse());
add(ta, BorderLayout.CENTER);
m_MenuBar = new MenuBar();
menuFile = new Menu("文件");
mi_File_Open = new MenuItem("打开(O)");
mi_File_Open.setShortcut(new MenuShortcut(KeyEvent.VK_O));
mi_File_Save = new MenuItem("保存(S)");
mi_File_Save.setShortcut(new MenuShortcut(KeyEvent.VK_S));
mi_File_Exit = new MenuItem("退出");
mi_File_Exit.addActionListener(new HandleAct());
mi_File_Save.addActionListener(new HandleAct());
mi_File_Open.addActionListener(new HandleAct());
menuFile.add(mi_File_Open);
menuFile.add(mi_File_Save);
menuFile.addSeparator();
menuFile.add(mi_File_Exit);
m_MenuBar.add(menuFile);
menuEdit = new Menu("编辑");
mi_Edit_Cut = new MenuItem("剪切(X)");
mi_Edit_Cut.setShortcut(new MenuShortcut(KeyEvent.VK_X));
mi_Edit_Copy = new MenuItem("复制(C)");
mi_Edit_Copy.setShortcut(new MenuShortcut(KeyEvent.VK_C));
mi_Edit_Paste = new MenuItem("粘贴(V)");
mi_Edit_Paste.setShortcut(new MenuShortcut(KeyEvent.VK_V));
mi_Edit_Cut.addActionListener(new HandleAct());
mi_Edit_Copy.addActionListener(new HandleAct());
mi_Edit_Paste.addActionListener(new HandleAct());
menuEdit.add(mi_Edit_Cut);
menuEdit.add(mi_Edit_Copy);
menuEdit.add(mi_Edit_Paste);
menuEdit.addSeparator();
m_MenuBar.add(menuEdit);
menuForm = new Menu("格式");
sub_Form_Style = new Menu("字体");
sub_Form_Size = new Menu("字号");
mi_Form_Style = new CheckboxMenuItem("粗体");
mi_Form_Style1 = new CheckboxMenuItem("斜体");
mi_Form_Size = new CheckboxMenuItem("大字体");
mi_Form_Size1 = new CheckboxMenuItem("一般");
sub_Form_Style.add(mi_Form_Style);
sub_Form_Style.add(mi_Form_Style1);
sub_Form_Size.add(mi_Form_Size);
sub_Form_Size.add(mi_Form_Size1);
menuForm.add(sub_Form_Style);
menuForm.add(sub_Form_Size);
m_MenuBar.add(menuForm);
mi_Form_Style.addItemListener(new HandleItem1());
mi_Form_Style1.addItemListener(new HandleItem2());
mi_Form_Size.addItemListener(new HandleItem3());
mi_Form_Size1.addItemListener(new HandleItem4());
this.setMenuBar(m_MenuBar);
popM = new PopupMenu();
pi_Left = new MenuItem("左对齐");
pi_Left.addActionListener(new HandleAct());
popM.add(pi_Left);
pi_Center = new MenuItem("居中");
pi_Center.addActionListener(new HandleAct());
popM.add(pi_Center);
pi_Right = new MenuItem("右对齐");
pi_Right.addActionListener(new HandleAct());
popM.add(pi_Right);
ta.add(popM);
addWindowListener(new HandleClose());
setSize(700, 500);
setVisible(true);
ta_Font_Name = (ta.getFont()).getName();
ta_Font_Style = (ta.getFont()).getStyle();
new_Style = ta_Font_Style;
ta_Font_Size = (ta.getFont()).getSize();
new_Size = ta_Font_Size;
clipboard = getToolkit().getSystemClipboard();
}
class HandleAct implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "退出") {
dispose();
System.exit(0);
}
else if (e.getActionCommand() == "打开") {
fd = new FileDialog(new MyMenuFrame(), "open", FileDialog.LOAD);
fd.setDirectory("d:\\temp");
fd.setVisible(true);
try {
File myfile = new File(fd.getDirectory(), fd.getFile());
RandomAccessFile raf = new RandomAccessFile(myfile, "r");
while (raf.getFilePointer() < raf.length()) {
ta.append(raf.readLine() + "\n"); // 阅读文件并加载到ta中
}
} catch (IOException ioe) {
System.err.println(ioe.toString());
}
} else if (e.getActionCommand() == "保存") {
FileDialog fd = new FileDialog(new MyMenuFrame(), "另存为",
FileDialog.SAVE);
fd.setFile("*.txt");
fd.setVisible(true);
String filePath = fd.getDirectory();
String fileName = fd.getFile();
try {
FileWriter fw = new FileWriter(filePath + fileName);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
pw.println(ta.getText());
pw.flush();
pw.close();
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (e.getActionCommand() == "剪切") {
String temp = ta.getSelectedText();
StringSelection text = new StringSelection(temp);
clipboard.setContents(text, null);
int start = ta.getSelectionStart();
int end = ta.getSelectionEnd();
ta.replaceRange("", start, end);
} else if (e.getActionCommand() == "复制") {
// s=ta.getSelectedText();
String temp = ta.getSelectedText();
StringSelection text = new StringSelection(temp);
clipboard.setContents(text, null);
} else if (e.getActionCommand() == "粘贴") {
// ta.append(s1);
Transferable contents = clipboard.getContents(this);
DataFlavor flavor = DataFlavor.stringFlavor;
if (contents.isDataFlavorSupported(flavor)) {
try {
String str;
str = (String) contents.getTransferData(flavor);
ta.append(str);
} catch (Exception ee) {
}
}
} else if (e.getActionCommand() == "字号") {
ta.setFont(new Font("宋体", Font.PLAIN, 24));
} else if (e.getActionCommand() == "字体") {
ta.setFont(new Font("宋体", Font.BOLD, 18));
}
}
}
class HandleMouse extends MouseAdapter {
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger())
popM.show((Component) e.getSource(), e.getX(), e.getY());
}
}
class HandleItem1 implements ItemListener {
public void itemStateChanged(ItemEvent e) {
if (((CheckboxMenuItem) e.getSource()).getState()) {
ta.setFont(new Font("Courier", Font.BOLD, 18));
}
}
}
class HandleItem2 implements ItemListener {
public void itemStateChanged(ItemEvent e) {
if (((CheckboxMenuItem) e.getSource()).getState()) {
ta.setFont(new Font("Arial", Font.ITALIC, 18));
}
}
}
class HandleItem3 implements ItemListener {
public void itemStateChanged(ItemEvent e) {
new_Size = ta_Font_Size * 2;
ta.setFont(new Font(ta_Font_Name, new_Style, new_Size));
}
}
class HandleItem4 implements ItemListener {
public void itemStateChanged(ItemEvent e) {
new_Size = ta_Font_Size;
ta.setFont(new Font(ta_Font_Name, new_Style, new_Size));
}
}
class HandleClose extends WindowAdapter {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
}
}
mi_File_Open = new MenuItem("打开(O)"); 你的名称是打开(O),而不是打开
换成mi_File_Open = new MenuItem("打开"); 试试,能执行的。
补充:Java , Java SE