1个回答
2013-07-25
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.awt.datatransfer.*;public class NewEdit
{
public static void main(String args[])
{
MyFrame EditFrame=new MyFrame();
EditFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
EditFrame.setVisible(true);
}
}class MyFrame extends JFrame
{
public MyFrame()
{ Dimension screenSize=toolKit.getScreenSize();
int screenHeight=screenSize.height;
int screenWidth=screenSize.width;
setSize(screenWidth/2,screenHeight/2);
setLocation(screenWidth/4,screenHeight/4);
Image img=toolKit.getImage("icon.gif");
setIconImage(img);
setTitle("MyEdit");
JMenuBar menuBar=new JMenuBar();
setJMenuBar(menuBar);
JMenu fileMenu=new JMenu("文件");
JMenu editMenu=new JMenu("编辑");
JMenu helpMenu=new JMenu("帮助");
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
JMenuItem newItem=new JMenuItem("新建");
newItem.addActionListener(new NewListener());
JMenuItem openItem=new JMenuItem("打开");
openItem.addActionListener(new OpenListener());
JMenuItem saveItem=new JMenuItem("保存");
saveItem.addActionListener(new SaveListener());
JMenuItem escItem=new JMenuItem("退出");
escItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent myE)
{
System.exit(0);
}
});
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(escItem);
JMenuItem allItem=new JMenuItem("全选");
//JMenuItem copyItem=new JMenuItem("复制");
//CopyAction copyAction=new CopyAction();
//copyItem.addActionListener(copyAction);
JMenuItem cutItem=new JMenuItem("剪切");
//JMenuItem pasteItem=new JMenuItem("粘贴");
//Action pasteAction=new PasteAction();
//pasteItem.addActionListener(pasteAction);
editMenu.add(allItem);
allItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent myE)
{
textArea.selectAll();
}
});
CopyAction copyAction=new CopyAction("剪切");
editMenu.add(copyAction);
CutAction cutAction=new CutAction("复制");
editMenu.add(cutAction);
Action pasteAction=new PasteAction("粘贴");
editMenu.add(pasteAction);
popup=new JPopupMenu();
popup.add(copyAction);
popup.add(pasteAction);
popup.add(cutAction);
textArea = new JTextArea();
textArea.add(popup);
textArea.addMouseListener(new
MouseListener(){
public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3)
{
popup.show(textArea, e.getX(), e.getY());
}
} public void mouseReleased(MouseEvent e) {} });//可尝试用MouseAdapter,代码可能更简单
JScrollPane scroller = new JScrollPane(textArea);
add(scroller);
//OR--this.getContentPane().add(scroller)
//scroller.setComponentPopupMenu(popup);
//textArea.setInheritsPopupMenu(true);
//scroller.addMouseListener(new MouseAdapter(){});
}
public void writeFile(String fileName)
{
try
{
File file = new File(fileName);
FileWriter write = new FileWriter(file);
write.write(textArea.getText());
write.close();
}
catch(Exception e){
System.out.println("Error closing file!");
}
}
public void openFile(String fileName)
{
try
{
File file = new File(fileName);
FileReader readIn = new FileReader(file);
int size = (int)file.length();
int charsRead = 0;
char[] content = new char[size];
while(readIn.ready())
charsRead += readIn.read(content,charsRead,size-charsRead);
readIn.close();
textArea.setText(new String(content,0,charsRead));
}
catch(IOException e)
{
System.out.println("Error opening file!");
}
}
private class NewListener implements ActionListener
{
public void actionPerformed(ActionEvent myE)
{
textArea.setText("");
}
}
private class OpenListener implements ActionListener
{
public void actionPerformed(ActionEvent myE)
{
openFileDialog.setVisible(true);
fileName = openFileDialog.getDirectory()+openFileDialog.getFile();
if(fileName != null)
{
openFile(fileName);
}
}
}
private class SaveListener implements ActionListener
{
public void actionPerformed(ActionEvent myE)
{
saveFileDialog.setVisible(true);
fileName = saveFileDialog.getDirectory()+saveFileDialog.getFile();
if(fileName !=null)
{
writeFile(fileName);
}
}
}
private class CutAction extends AbstractAction
{
public CutAction(String name)
{
super(name);
}
public void actionPerformed(ActionEvent event)
{
String text = textArea.getSelectedText();
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection,null);
textArea.replaceRange("",textArea.getSelectionStart(),
textArea.getSelectionEnd());
}
}
private class CopyAction extends AbstractAction
{
public CopyAction(String name)
{
super(name);
}
public void actionPerformed(ActionEvent event)
{
String text = textArea.getSelectedText();
StringSelection selection= new StringSelection(text);
clipboard.setContents(selection,null);
}
}
private class PasteAction extends AbstractAction
{
public PasteAction(String name)
{
super(name);
}
public void actionPerformed(ActionEvent event)
{
Transferable contents = clipboard.getContents(this);
if(contents==null)
return;
String text;
text="";
try
{
text = (String)contents.getTransferData(DataFlavor.stringFlavor);
}
catch(Exception ex){}
textArea.replaceRange(text,
textArea.getSelectionStart(),textArea.getSelectionEnd());
}
}
private JTextArea textArea;
private JPopupMenu popup;
private String fileName="";
private FileDialog openFileDialog=new FileDialog(this,"OpenFile",FileDialog.LOAD); private FileDialog saveFileDialog=new FileDialog(this,"SaveFile",FileDialog.SAVE);
Toolkit toolKit = Toolkit.getDefaultToolkit();
private Clipboard clipboard = toolKit.getSystemClipboard();}
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.awt.datatransfer.*;public class NewEdit
{
public static void main(String args[])
{
MyFrame EditFrame=new MyFrame();
EditFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
EditFrame.setVisible(true);
}
}class MyFrame extends JFrame
{
public MyFrame()
{ Dimension screenSize=toolKit.getScreenSize();
int screenHeight=screenSize.height;
int screenWidth=screenSize.width;
setSize(screenWidth/2,screenHeight/2);
setLocation(screenWidth/4,screenHeight/4);
Image img=toolKit.getImage("icon.gif");
setIconImage(img);
setTitle("MyEdit");
JMenuBar menuBar=new JMenuBar();
setJMenuBar(menuBar);
JMenu fileMenu=new JMenu("文件");
JMenu editMenu=new JMenu("编辑");
JMenu helpMenu=new JMenu("帮助");
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
JMenuItem newItem=new JMenuItem("新建");
newItem.addActionListener(new NewListener());
JMenuItem openItem=new JMenuItem("打开");
openItem.addActionListener(new OpenListener());
JMenuItem saveItem=new JMenuItem("保存");
saveItem.addActionListener(new SaveListener());
JMenuItem escItem=new JMenuItem("退出");
escItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent myE)
{
System.exit(0);
}
});
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(escItem);
JMenuItem allItem=new JMenuItem("全选");
//JMenuItem copyItem=new JMenuItem("复制");
//CopyAction copyAction=new CopyAction();
//copyItem.addActionListener(copyAction);
JMenuItem cutItem=new JMenuItem("剪切");
//JMenuItem pasteItem=new JMenuItem("粘贴");
//Action pasteAction=new PasteAction();
//pasteItem.addActionListener(pasteAction);
editMenu.add(allItem);
allItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent myE)
{
textArea.selectAll();
}
});
CopyAction copyAction=new CopyAction("剪切");
editMenu.add(copyAction);
CutAction cutAction=new CutAction("复制");
editMenu.add(cutAction);
Action pasteAction=new PasteAction("粘贴");
editMenu.add(pasteAction);
popup=new JPopupMenu();
popup.add(copyAction);
popup.add(pasteAction);
popup.add(cutAction);
textArea = new JTextArea();
textArea.add(popup);
textArea.addMouseListener(new
MouseListener(){
public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3)
{
popup.show(textArea, e.getX(), e.getY());
}
} public void mouseReleased(MouseEvent e) {} });//可尝试用MouseAdapter,代码可能更简单
JScrollPane scroller = new JScrollPane(textArea);
add(scroller);
//OR--this.getContentPane().add(scroller)
//scroller.setComponentPopupMenu(popup);
//textArea.setInheritsPopupMenu(true);
//scroller.addMouseListener(new MouseAdapter(){});
}
public void writeFile(String fileName)
{
try
{
File file = new File(fileName);
FileWriter write = new FileWriter(file);
write.write(textArea.getText());
write.close();
}
catch(Exception e){
System.out.println("Error closing file!");
}
}
public void openFile(String fileName)
{
try
{
File file = new File(fileName);
FileReader readIn = new FileReader(file);
int size = (int)file.length();
int charsRead = 0;
char[] content = new char[size];
while(readIn.ready())
charsRead += readIn.read(content,charsRead,size-charsRead);
readIn.close();
textArea.setText(new String(content,0,charsRead));
}
catch(IOException e)
{
System.out.println("Error opening file!");
}
}
private class NewListener implements ActionListener
{
public void actionPerformed(ActionEvent myE)
{
textArea.setText("");
}
}
private class OpenListener implements ActionListener
{
public void actionPerformed(ActionEvent myE)
{
openFileDialog.setVisible(true);
fileName = openFileDialog.getDirectory()+openFileDialog.getFile();
if(fileName != null)
{
openFile(fileName);
}
}
}
private class SaveListener implements ActionListener
{
public void actionPerformed(ActionEvent myE)
{
saveFileDialog.setVisible(true);
fileName = saveFileDialog.getDirectory()+saveFileDialog.getFile();
if(fileName !=null)
{
writeFile(fileName);
}
}
}
private class CutAction extends AbstractAction
{
public CutAction(String name)
{
super(name);
}
public void actionPerformed(ActionEvent event)
{
String text = textArea.getSelectedText();
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection,null);
textArea.replaceRange("",textArea.getSelectionStart(),
textArea.getSelectionEnd());
}
}
private class CopyAction extends AbstractAction
{
public CopyAction(String name)
{
super(name);
}
public void actionPerformed(ActionEvent event)
{
String text = textArea.getSelectedText();
StringSelection selection= new StringSelection(text);
clipboard.setContents(selection,null);
}
}
private class PasteAction extends AbstractAction
{
public PasteAction(String name)
{
super(name);
}
public void actionPerformed(ActionEvent event)
{
Transferable contents = clipboard.getContents(this);
if(contents==null)
return;
String text;
text="";
try
{
text = (String)contents.getTransferData(DataFlavor.stringFlavor);
}
catch(Exception ex){}
textArea.replaceRange(text,
textArea.getSelectionStart(),textArea.getSelectionEnd());
}
}
private JTextArea textArea;
private JPopupMenu popup;
private String fileName="";
private FileDialog openFileDialog=new FileDialog(this,"OpenFile",FileDialog.LOAD); private FileDialog saveFileDialog=new FileDialog(this,"SaveFile",FileDialog.SAVE);
Toolkit toolKit = Toolkit.getDefaultToolkit();
private Clipboard clipboard = toolKit.getSystemClipboard();}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询