我哪里写错了吗?import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.lang.*;
import java.awt.datatransfer.*;
public class EditerDemo extends JFrame implements ActionListener{
//JEditorPane jEditorPane1 = new JEditorPane();
//JScrollPane scrollPane = new JScrollPane();
//JBar bar = new JBar();
JMenuBar menuBar = new JMenuBar();
JToolBar toolBar = new JToolBar();
//JToolBar toolBar1 = new JToolBar();
JTextArea textArea = new JTextArea();
//菜单栏
//JMenuBar menuBar = new JMenuBar();
//菜单项
JMenu fileMenu = new JMenu("文件(F)");
JMenuItem newItem = new JMenuItem("新建(N)");
JMenuItem openItem = new JMenuItem("打开(O)");
JMenuItem saveItem = new JMenuItem("保存(S)");
JMenuItem saveAsItem = new JMenuItem("另存为(A)");
JMenuItem printItem = new JMenuItem("打印(P)");
JMenuItem exitItem = new JMenuItem("退出(X)");
JMenu editMenu = new JMenu("编辑(E)");
JMenuItem copyItem = new JMenuItem("复制(C)");
JMenuItem cutItem = new JMenuItem("剪切(T)");
JMenuItem pasteItem = new JMenuItem("粘贴(P)");
JMenuItem selectItem = new JMenuItem("全选(E)");
JMenu helpMenu = new JMenu("帮助(H)");
JMenuItem aboutItem = new JMenuItem("关于记事本");
//工具栏
JButton toolNewFile = new JButton("新建");
JButton toolOpenFile = new JButton("打开");
JButton toolSaveFile = new JButton("保存");
JButton toolCopy = new JButton("复制");
JButton toolCut = new JButton("剪切");
JButton toolPaste = new JButton("粘贴");
String fileName = "NoName";
Toolkit toolkit = Toolkit.getDefaultToolkit();
Clipboard clipBoard =toolkit.getSystemClipboard();
/**打开文件对话框和保存对话框 */
private FileDialog openFileDialog = new FileDialog(this,"Open File",FileDialog.LOAD);
private FileDialog saveAsFileDialog = new FileDialog(this,"Save File As",FileDialog.SAVE);
public EditerDemo(){
setTitle("记事本Demo版");
setFont(new Font("Times New Roman",Font.PLAIN,12));
// SetBackground(Color.while);
setSize(400,300);
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(saveAsItem);
fileMenu.addSeparator();
fileMenu.add(printItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);
editMenu.add(copyItem);
editMenu.add(cutItem);
editMenu.add(pasteItem);
editMenu.addSeparator();
editMenu.add(selectItem);
helpMenu.add(aboutItem);
toolBar.add(toolNewFile);
toolBar.add(toolOpenFile);
toolBar.add(toolSaveFile);
toolBar.add(toolCopy);
toolBar.add(toolCut);
toolBar.add(toolPaste);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
//Container contentPane = getContentPane();
//contentPane.add(scrollPane,BorderLayout.SOUTH);
//contentPane.add(toolBar,BorderLayout.NORTH);
setJMenuBar(menuBar);
//setJMenuBar(toolBar);
add(textArea);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
//添加事件监听
newItem.addActionListener(this);
openItem.addActionListener(this);
saveItem.addActionListener(this);
printItem.addActionListener(this);
exitItem.addActionListener(this);
copyItem.addActionListener(this);
cutItem.addActionListener(this);
pasteItem.addActionListener(this);
selectItem.addActionListener(this);
aboutItem.addActionListener(this);
toolNewFile.addActionListener(this);
toolOpenFile.addActionListener(this);
toolSaveFile.addActionListener(this);
toolCut.addActionListener(this);
toolCopy.addActionListener(this);
toolPaste.addActionListener(this);
}
//实现监听接口
public void actionPerformed(ActionEvent e){
Object eventSource = e.getSource();
if(eventSource == newItem){
textArea.setText("");
}else if(eventSource == openItem){
openFileDialog.setVisible(true);
fileName = openFileDialog.getDirectory() + openFileDialog.getFile();
if(fileName != null)
readFile(fileName);
}else if(eventSource == saveAsItem){
saveAsFileDialog.setVisible(true);
fileName = saveAsFileDialog.getDirectory() + saveAsFileDialog.getFile();
if(fileName != null)
writeFile(fileName);
}else if(eventSource == selectItem){
textArea.selectAll();
}else if(eventSource == copyItem){
String text = textArea.getSelectedText();
StringSelection selection = new StringSelection(text);
clipBoard.setContents(selection,null);
}else if(eventSource == cutItem){
String text = textArea.getSelectedText();
StringSelection selection = new StringSelection(text);
clipBoard.setContents(selection,null);
//剪切时用空串代替被选的字符
textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd());
}else if(eventSource == pasteItem){
Transferable contents = clipBoard.getContents(this);
if(contents == null)
return;
String text;
text = "";
try{
text = (String)contents.getTransferData(DataFlavor.stringFlavor);
}catch(Exception exception){}
textArea.replaceRange(text,textArea.getSelectionStart(),textArea.getSelectionEnd());
}else if(eventSource == exitItem){
System.exit(0);
}
}
//读文件
public void readFile(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(Exception e){
System.out.println("Error opening file");
}
}
//写文件
public void writeFile(String fileName){
try{
File file = new File(fileName);
FileWriter writerOut = new FileWriter(file);
writerOut.write(textArea.getText());
writerOut.close();
}catch(Exception e){
System.out.println("Error writing file");
}
}
public static void main(String []args){
JFrame frame = new JFrame();
frame.setDefaultLookAndFeelDecorated(true);
//frame.pack();
frame.setVisible(true) ;
}}
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.lang.*;
import java.awt.datatransfer.*;
public class EditerDemo extends JFrame implements ActionListener{
//JEditorPane jEditorPane1 = new JEditorPane();
//JScrollPane scrollPane = new JScrollPane();
//JBar bar = new JBar();
JMenuBar menuBar = new JMenuBar();
JToolBar toolBar = new JToolBar();
//JToolBar toolBar1 = new JToolBar();
JTextArea textArea = new JTextArea();
//菜单栏
//JMenuBar menuBar = new JMenuBar();
//菜单项
JMenu fileMenu = new JMenu("文件(F)");
JMenuItem newItem = new JMenuItem("新建(N)");
JMenuItem openItem = new JMenuItem("打开(O)");
JMenuItem saveItem = new JMenuItem("保存(S)");
JMenuItem saveAsItem = new JMenuItem("另存为(A)");
JMenuItem printItem = new JMenuItem("打印(P)");
JMenuItem exitItem = new JMenuItem("退出(X)");
JMenu editMenu = new JMenu("编辑(E)");
JMenuItem copyItem = new JMenuItem("复制(C)");
JMenuItem cutItem = new JMenuItem("剪切(T)");
JMenuItem pasteItem = new JMenuItem("粘贴(P)");
JMenuItem selectItem = new JMenuItem("全选(E)");
JMenu helpMenu = new JMenu("帮助(H)");
JMenuItem aboutItem = new JMenuItem("关于记事本");
//工具栏
JButton toolNewFile = new JButton("新建");
JButton toolOpenFile = new JButton("打开");
JButton toolSaveFile = new JButton("保存");
JButton toolCopy = new JButton("复制");
JButton toolCut = new JButton("剪切");
JButton toolPaste = new JButton("粘贴");
String fileName = "NoName";
Toolkit toolkit = Toolkit.getDefaultToolkit();
Clipboard clipBoard =toolkit.getSystemClipboard();
/**打开文件对话框和保存对话框 */
private FileDialog openFileDialog = new FileDialog(this,"Open File",FileDialog.LOAD);
private FileDialog saveAsFileDialog = new FileDialog(this,"Save File As",FileDialog.SAVE);
public EditerDemo(){
setTitle("记事本Demo版");
setFont(new Font("Times New Roman",Font.PLAIN,12));
// SetBackground(Color.while);
setSize(400,300);
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(saveAsItem);
fileMenu.addSeparator();
fileMenu.add(printItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);
editMenu.add(copyItem);
editMenu.add(cutItem);
editMenu.add(pasteItem);
editMenu.addSeparator();
editMenu.add(selectItem);
helpMenu.add(aboutItem);
toolBar.add(toolNewFile);
toolBar.add(toolOpenFile);
toolBar.add(toolSaveFile);
toolBar.add(toolCopy);
toolBar.add(toolCut);
toolBar.add(toolPaste);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
//Container contentPane = getContentPane();
//contentPane.add(scrollPane,BorderLayout.SOUTH);
//contentPane.add(toolBar,BorderLayout.NORTH);
setJMenuBar(menuBar);
//setJMenuBar(toolBar);
add(textArea);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
//添加事件监听
newItem.addActionListener(this);
openItem.addActionListener(this);
saveItem.addActionListener(this);
printItem.addActionListener(this);
exitItem.addActionListener(this);
copyItem.addActionListener(this);
cutItem.addActionListener(this);
pasteItem.addActionListener(this);
selectItem.addActionListener(this);
aboutItem.addActionListener(this);
toolNewFile.addActionListener(this);
toolOpenFile.addActionListener(this);
toolSaveFile.addActionListener(this);
toolCut.addActionListener(this);
toolCopy.addActionListener(this);
toolPaste.addActionListener(this);
}
//实现监听接口
public void actionPerformed(ActionEvent e){
Object eventSource = e.getSource();
if(eventSource == newItem){
textArea.setText("");
}else if(eventSource == openItem){
openFileDialog.setVisible(true);
fileName = openFileDialog.getDirectory() + openFileDialog.getFile();
if(fileName != null)
readFile(fileName);
}else if(eventSource == saveAsItem){
saveAsFileDialog.setVisible(true);
fileName = saveAsFileDialog.getDirectory() + saveAsFileDialog.getFile();
if(fileName != null)
writeFile(fileName);
}else if(eventSource == selectItem){
textArea.selectAll();
}else if(eventSource == copyItem){
String text = textArea.getSelectedText();
StringSelection selection = new StringSelection(text);
clipBoard.setContents(selection,null);
}else if(eventSource == cutItem){
String text = textArea.getSelectedText();
StringSelection selection = new StringSelection(text);
clipBoard.setContents(selection,null);
//剪切时用空串代替被选的字符
textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd());
}else if(eventSource == pasteItem){
Transferable contents = clipBoard.getContents(this);
if(contents == null)
return;
String text;
text = "";
try{
text = (String)contents.getTransferData(DataFlavor.stringFlavor);
}catch(Exception exception){}
textArea.replaceRange(text,textArea.getSelectionStart(),textArea.getSelectionEnd());
}else if(eventSource == exitItem){
System.exit(0);
}
}
//读文件
public void readFile(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(Exception e){
System.out.println("Error opening file");
}
}
//写文件
public void writeFile(String fileName){
try{
File file = new File(fileName);
FileWriter writerOut = new FileWriter(file);
writerOut.write(textArea.getText());
writerOut.close();
}catch(Exception e){
System.out.println("Error writing file");
}
}
public static void main(String []args){
JFrame frame = new JFrame();
frame.setDefaultLookAndFeelDecorated(true);
//frame.pack();
frame.setVisible(true) ;
}}
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.lang.*;
import java.awt.datatransfer.*;public class EditerDemo extends JFrame implements ActionListener { // JEditorPane jEditorPane1 = new JEditorPane();
// JScrollPane scrollPane = new JScrollPane(); // JBar bar = new JBar();
JMenuBar menuBar = new JMenuBar();
JToolBar toolBar = new JToolBar();
// JToolBar toolBar1 = new JToolBar();
JTextArea textArea = new JTextArea();
// 菜单栏
// JMenuBar menuBar = new JMenuBar();
// 菜单项
JMenu fileMenu = new JMenu("文件(F)");
JMenuItem newItem = new JMenuItem("新建(N)");
JMenuItem openItem = new JMenuItem("打开(O)");
JMenuItem saveItem = new JMenuItem("保存(S)");
JMenuItem saveAsItem = new JMenuItem("另存为(A)");
JMenuItem printItem = new JMenuItem("打印(P)");
JMenuItem exitItem = new JMenuItem("退出(X)"); JMenu editMenu = new JMenu("编辑(E)");
JMenuItem copyItem = new JMenuItem("复制(C)");
JMenuItem cutItem = new JMenuItem("剪切(T)");
JMenuItem pasteItem = new JMenuItem("粘贴(P)");
JMenuItem selectItem = new JMenuItem("全选(E)"); JMenu helpMenu = new JMenu("帮助(H)");
JMenuItem aboutItem = new JMenuItem("关于记事本"); // 工具栏
JButton toolNewFile = new JButton("新建");
JButton toolOpenFile = new JButton("打开");
JButton toolSaveFile = new JButton("保存");
JButton toolCopy = new JButton("复制");
JButton toolCut = new JButton("剪切");
JButton toolPaste = new JButton("粘贴"); String fileName = "NoName";
Toolkit toolkit = Toolkit.getDefaultToolkit();
Clipboard clipBoard = toolkit.getSystemClipboard(); /** 打开文件对话框和保存对话框 */
private FileDialog openFileDialog = new FileDialog(this, "Open File",
FileDialog.LOAD); private FileDialog saveAsFileDialog = new FileDialog(this, "Save File As",
FileDialog.SAVE); public EditerDemo() { setTitle("记事本Demo版");
setFont(new Font("Times New Roman", Font.PLAIN, 12));
// SetBackground(Color.while);
setSize(400, 300); fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(saveAsItem);
fileMenu.addSeparator();
fileMenu.add(printItem);
fileMenu.addSeparator();
fileMenu.add(exitItem); editMenu.add(copyItem);
editMenu.add(cutItem);
editMenu.add(pasteItem);
editMenu.addSeparator();
editMenu.add(selectItem); helpMenu.add(aboutItem); toolBar.add(toolNewFile);
toolBar.add(toolOpenFile);
toolBar.add(toolSaveFile);
toolBar.add(toolCopy);
toolBar.add(toolCut);
toolBar.add(toolPaste); menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu); // Container contentPane = getContentPane();
// contentPane.add(scrollPane,BorderLayout.SOUTH);
// contentPane.add(toolBar,BorderLayout.NORTH); setJMenuBar(menuBar);
// setJMenuBar(toolBar); add(textArea);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}); // 添加事件监听 newItem.addActionListener(this);
openItem.addActionListener(this);
saveItem.addActionListener(this);
printItem.addActionListener(this);
exitItem.addActionListener(this);
copyItem.addActionListener(this);
cutItem.addActionListener(this);
pasteItem.addActionListener(this);
selectItem.addActionListener(this);
aboutItem.addActionListener(this);
toolNewFile.addActionListener(this);
toolOpenFile.addActionListener(this);
toolSaveFile.addActionListener(this);
toolCut.addActionListener(this);
toolCopy.addActionListener(this);
toolPaste.addActionListener(this); } // 实现监听接口 public void actionPerformed(ActionEvent e) { Object eventSource = e.getSource(); if (eventSource == newItem) {
textArea.setText("");
} else if (eventSource == openItem) {
openFileDialog.setVisible(true);
fileName = openFileDialog.getDirectory() + openFileDialog.getFile();
if (fileName != null)
readFile(fileName);
} else if (eventSource == saveAsItem) {
saveAsFileDialog.setVisible(true);
fileName = saveAsFileDialog.getDirectory()
+ saveAsFileDialog.getFile();
if (fileName != null)
writeFile(fileName);
} else if (eventSource == selectItem) {
textArea.selectAll();
} else if (eventSource == copyItem) {
String text = textArea.getSelectedText();
StringSelection selection = new StringSelection(text);
clipBoard.setContents(selection, null);
} else if (eventSource == cutItem) {
String text = textArea.getSelectedText();
StringSelection selection = new StringSelection(text);
clipBoard.setContents(selection, null);
// 剪切时用空串代替被选的字符
textArea.replaceRange("", textArea.getSelectionStart(), textArea
.getSelectionEnd());
} else if (eventSource == pasteItem) {
Transferable contents = clipBoard.getContents(this);
if (contents == null)
return;
String text;
text = "";
try {
text = (String) contents
.getTransferData(DataFlavor.stringFlavor);
} catch (Exception exception) {
}
textArea.replaceRange(text, textArea.getSelectionStart(), textArea
.getSelectionEnd());
} else if (eventSource == exitItem) {
System.exit(0);
} } // 读文件
public void readFile(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 (Exception e) {
System.out.println("Error opening file");
}
} // 写文件
public void writeFile(String fileName) {
try {
File file = new File(fileName);
FileWriter writerOut = new FileWriter(file);
writerOut.write(textArea.getText());
writerOut.close();
} catch (Exception e) {
System.out.println("Error writing file");
} } public static void main(String[] args) { JFrame frame = new EditerDemo();
frame.setDefaultLookAndFeelDecorated(true);
// frame.pack();
frame.setVisible(true); }}这个是我的
JFrame frame = new EditerDemo();
这个是你的
JFrame frame = new JFrame();