我是用TextPad,它自带了java的编译和执行功能。我编译的时候是一次编译就一下产生这么
多类文件。
我的类里面好像没有什么匿名类啊。
我的源代码如下:
MyEditor.java
import java.awt.*;
import java.awt.event.*;
public class MyEditor extends Frame implements ActionListener
{
private static MyEditor myEditor;
private Dimension screenSize;
private static TextArea textArea = new TextArea(10,12);
private yqMainMenu mainMenu;
public MyEditor()
{
mainMenu = new yqMainMenu();
screenSize = getToolkit().getScreenSize();
setBackground(Color.lightGray);
setSize(screenSize);
super.setTitle("我的编辑器");
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e) {System.exit(0);}});
setMenuBar(mainMenu);
add(textArea);
show();
}
public static TextArea getTextArea(){return textArea;}
public static MyEditor getMyEditor(){return myEditor;}
public void actionPerformed(ActionEvent e){}
public static void main(String[] args)
{
myEditor = new MyEditor();
}
}
yqMainMenu.java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.datatransfer.*;
public class yqMainMenu extends MenuBar
{
private Menu fileMenu = new Menu("文件");
private MenuItem newItem = new MenuItem("新建");
private MenuItem openItem = new MenuItem("打开");
private MenuItem saveItem = new MenuItem("保存");
private MenuItem saveAsItem = new MenuItem("Save as");
private MenuItem exitItem = new MenuItem("Exit");
private Menu editMenu = new Menu("编辑");
private MenuItem selectItem = new MenuItem("选择全部");
private MenuItem copyItem = new MenuItem("复制");
private MenuItem cutItem = new MenuItem("剪切");
private MenuItem pasteItem = new MenuItem("粘贴");
private String fileName;
private Toolkit toolKit=Toolkit.getDefaultToolkit();
private Clipboard clipBoard;
public yqMainMenu()
{
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.addSeparator();
fileMenu.add(saveItem);
fileMenu.add(saveAsItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);
editMenu.add(selectItem);
editMenu.add(copyItem);
editMenu.add(cutItem);
editMenu.add(pasteItem);
clipBoard = toolKit.getSystemClipboard();
newItem.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e)
{
MyEditor.getTextArea().setText("");
}
});
openItem.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e)
{
FileDialog openFileDialog = new FileDialog(MyEditor.getMyEditor(),"OPEN FILE",FileDialog.LOAD);
openFileDialog.show();
fileName = openFileDialog.getDirectory()+openFileDialog.getFile();
if(fileName!=null)
readFile(fileName);
}
});
saveItem.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e)
{
if(fileName!=null)
writeFile(fileName);
}
});
saveAsItem.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e)
{
FileDialog saveAsFileDialog = new FileDialog(MyEditor.getMyEditor(),"SAVE AS",FileDialog.SAVE);
saveAsFileDialog.show();
fileName = saveAsFileDialog.getDirectory()+saveAsFileDialog.getFile();
if(fileName!=null)
writeFile(fileName);
}
});
selectItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
MyEditor.getTextArea().selectAll();
}
});
copyItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String text= MyEditor.getTextArea().getSelectedText();
StringSelection selection = new StringSelection(text);
clipBoard.setContents(selection,null);
}
});
cutItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String text= MyEditor.getTextArea().getSelectedText();
StringSelection selection = new StringSelection(text);
clipBoard.setContents(selection,null);
MyEditor.getTextArea().replaceRange("",MyEditor.getTextArea().getSelectionStart(),
MyEditor.getTextArea().getSelectionEnd());
}
});
pasteItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Transferable contents=clipBoard.getContents(this);
if(contents==null)
{
return;
}
String text;
text="";
try{
text=(String)contents.getTransferData(DataFlavor.stringFlavor);
}catch(Exception exception){
}
MyEditor.getTextArea().replaceRange(text,MyEditor.getTextArea().getSelectionStart(),
MyEditor.getTextArea().getSelectionEnd());
}
});
exitItem.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent e)
{System.exit(0);
}
});
add(fileMenu);
add(editMenu);
}
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();
MyEditor.getTextArea().setText(new String(content, 0, charsRead));
}
catch(IOException e){
}
}
public void writeFile(String fileName){
try{
File file = new File (fileName);
FileWriter writeOut = new FileWriter(file);
writeOut.write( MyEditor.getTextArea().getText());
writeOut.close();
}
catch(IOException e){
//errorWarning("Error writing file");
}
}
}
请各位看一看。