//监听字体选择器
jFileChooser1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jFileChooser1_actionPerformed(e);
}
});
jToolBar.add(jButton1);
jToolBar.add(jButton2);
jToolBar.add(jButton3);
jMenuFile.add(jMenuIFileNew);
jMenuFile.add(jMenuOpenFile);
jMenuFile.add(jMenuSaveFile);
jMenuFile.add(jMenuSaveasFile);
jMenuFile.addSeparator();
jMenuFile.add(jMenuFileExit);
jMenuHelp.add(jMenuHelpAbout);
jMenuBar1.add(jMenuFile);
jMenuBar1.add(jMenuEdit);
jMenuBar1.add(jMenuHelp);
this.setJMenuBar(jMenuBar1);
contentPane.add(jToolBar, BorderLayout.NORTH);
contentPane.add(statusBar, BorderLayout.SOUTH);
contentPane.add(TextScrollPane, BorderLayout.CENTER);
TextScrollPane.getViewport().add(jTextArea1, null);
jMenuEdit.add(jMenuEditFont);
jMenuEdit.add(jMenuForegroundColor);
jMenuEdit.add(jMenuBackgroundColor);
}
//File | Exit action performed
//退出事件
public void jMenuFileExit_actionPerformed(ActionEvent e) {
if(!okToAbandon()){
return;
}
System.exit(0);
}
//Help | About action performed
//关于事件
public void jMenuHelpAbout_actionPerformed(ActionEvent e) {
TextEditorFrame_AboutBox dlg = new TextEditorFrame_AboutBox(this);
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.pack();
dlg.show();
}
//Overridden so we can exit when window is closed
//当窗口关闭时,结束进程
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
jMenuFileExit_actionPerformed(null);
}
}
//新开文档事件
void jMenuIFileNew_actionPerformed(ActionEvent e) {
if(okToAbandon()){
jTextArea1.setText("");
String currentFileName = null;
boolean saveNeeded = false;
updateTitle();
} } //设置font事件
void jMenuEditFont_actionPerformed(ActionEvent e) {
fontChooser1.setSelectedFont(jTextArea1.getFont());
if(fontChooser1.showDialog()){
jTextArea1.setFont(fontChooser1.getSelectedFont());
}
this.repaint();
jTextArea1.repaint();
}
//设置字体颜色
void jMenuForegroundColor_actionPerformed(ActionEvent e) {
Color color = JColorChooser.showDialog(this,"Foreground Color",jTextArea1.getForeground());
if(color != null){
jTextArea1.setForeground(color);
}
this.repaint();
}
//设置背景颜色
void jMenuBackgroundColor_actionPerformed(ActionEvent e) {
Color color = JColorChooser.showDialog(this,"Background Color",jTextArea1.getBackground());
if(color != null){
jTextArea1.setBackground(color);
}
this.repaint(); }
//打开文件事件
void jMenuOpenFile_actionPerformed(ActionEvent e) {
//openFile();
}
//保存文件事件
void jMenuSaveFile_actionPerformed(ActionEvent e) {
saveFile();
}
//另存文件事件
void jMenuSaveasFile_actionPerformed(ActionEvent e) {
saveAsFile();
}
//okToAbandon方法实现在打开一个新的文件,推出程序,新建一个文件是提示用户对作过改动的文件进行的存储
boolean okToAbandon(){
//boolean saveNeeded=false;
if(!saveNeeded){
return true;
}
int value = JOptionPane.showConfirmDialog(this,"保存文件吗?","文本编辑",JOptionPane.YES_NO_CANCEL_OPTION);
switch(value){
case JOptionPane.YES_OPTION:return saveFile();
case JOptionPane.NO_OPTION:return true;
case JOptionPane.CANCEL_OPTION:
default: return false;
}
}
//saveFile方法保存文本文件 boolean saveFile(){
boolean saveNeeded;
String currentFileName;
if(currentFileName == null){
return saveAsFile();
}
try{
File file = new File(currentFileName);
FileWriter out = new FileWriter(file);
String text = jTextArea1.getText();
out.write(text);
out.close();
this.saveNeeded = false;
updateTitle();
return true;
}
catch (IOException e){
statusBar.setText("存储"+currentFileName+"出错");
}
return false;
}
//SaveAs方法另存文件
boolean saveAsFile(){
//boolean saveNeeded;
//String currentFileName;
this.repaint();
if(JFileChooser.APPROVE_OPTION == jFileChooser1.showSaveDialog(this)){
currentFileName = jFileChooser1.getSelectedFile().getPath();
this.repaint();
}
else{
this.repaint();
return false;
}
}
void updateTitle(){
//String currentFileName;
//boolean saveNeeded=false;
String title;
if(currentFileName == null){
title = "未命名文件";
}else{
title=currentFileName;
}
if(saveNeeded==true){
title="*"+title;
}
title="文本编辑器-"+title;
this.setTitle(title);
} void jFileChooser1_actionPerformed(ActionEvent e) { }}class ActionListener implements java.awt.event.ActionListener {
TextEditorFrame adaptee; ActionListener(TextEditorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuBackgroundColor_actionPerformed(e); // ChangedUpdate(DocumentEvent e);
// InsertUpdate(DocumentEvent e);
// RemoveUpdate(DocumentEvent e);
}
}
调试后,错误为:
"TextEditorFrame.java": Error #: 300 : method ActionListener() not found in class textedit.ActionListener at line 82, column 55
"TextEditorFrame.java": Error #: 300 : constructor ActionListener() not found in class textedit.ActionListener at line 82, column 59
"TextEditorFrame.java": Error #: 300 : method ActionListener() not found in class textedit.ActionListener at line 90, column 56
"TextEditorFrame.java": Error #: 300 : constructor ActionListener() not found in class textedit.ActionListener at line 90, column 60
"TextEditorFrame.java": Error #: 300 : class IOException not found in class textedit.TextEditorFrame at line 298, column 12
jFileChooser1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jFileChooser1_actionPerformed(e);
}
});
jToolBar.add(jButton1);
jToolBar.add(jButton2);
jToolBar.add(jButton3);
jMenuFile.add(jMenuIFileNew);
jMenuFile.add(jMenuOpenFile);
jMenuFile.add(jMenuSaveFile);
jMenuFile.add(jMenuSaveasFile);
jMenuFile.addSeparator();
jMenuFile.add(jMenuFileExit);
jMenuHelp.add(jMenuHelpAbout);
jMenuBar1.add(jMenuFile);
jMenuBar1.add(jMenuEdit);
jMenuBar1.add(jMenuHelp);
this.setJMenuBar(jMenuBar1);
contentPane.add(jToolBar, BorderLayout.NORTH);
contentPane.add(statusBar, BorderLayout.SOUTH);
contentPane.add(TextScrollPane, BorderLayout.CENTER);
TextScrollPane.getViewport().add(jTextArea1, null);
jMenuEdit.add(jMenuEditFont);
jMenuEdit.add(jMenuForegroundColor);
jMenuEdit.add(jMenuBackgroundColor);
}
//File | Exit action performed
//退出事件
public void jMenuFileExit_actionPerformed(ActionEvent e) {
if(!okToAbandon()){
return;
}
System.exit(0);
}
//Help | About action performed
//关于事件
public void jMenuHelpAbout_actionPerformed(ActionEvent e) {
TextEditorFrame_AboutBox dlg = new TextEditorFrame_AboutBox(this);
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.pack();
dlg.show();
}
//Overridden so we can exit when window is closed
//当窗口关闭时,结束进程
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
jMenuFileExit_actionPerformed(null);
}
}
//新开文档事件
void jMenuIFileNew_actionPerformed(ActionEvent e) {
if(okToAbandon()){
jTextArea1.setText("");
String currentFileName = null;
boolean saveNeeded = false;
updateTitle();
} } //设置font事件
void jMenuEditFont_actionPerformed(ActionEvent e) {
fontChooser1.setSelectedFont(jTextArea1.getFont());
if(fontChooser1.showDialog()){
jTextArea1.setFont(fontChooser1.getSelectedFont());
}
this.repaint();
jTextArea1.repaint();
}
//设置字体颜色
void jMenuForegroundColor_actionPerformed(ActionEvent e) {
Color color = JColorChooser.showDialog(this,"Foreground Color",jTextArea1.getForeground());
if(color != null){
jTextArea1.setForeground(color);
}
this.repaint();
}
//设置背景颜色
void jMenuBackgroundColor_actionPerformed(ActionEvent e) {
Color color = JColorChooser.showDialog(this,"Background Color",jTextArea1.getBackground());
if(color != null){
jTextArea1.setBackground(color);
}
this.repaint(); }
//打开文件事件
void jMenuOpenFile_actionPerformed(ActionEvent e) {
//openFile();
}
//保存文件事件
void jMenuSaveFile_actionPerformed(ActionEvent e) {
saveFile();
}
//另存文件事件
void jMenuSaveasFile_actionPerformed(ActionEvent e) {
saveAsFile();
}
//okToAbandon方法实现在打开一个新的文件,推出程序,新建一个文件是提示用户对作过改动的文件进行的存储
boolean okToAbandon(){
//boolean saveNeeded=false;
if(!saveNeeded){
return true;
}
int value = JOptionPane.showConfirmDialog(this,"保存文件吗?","文本编辑",JOptionPane.YES_NO_CANCEL_OPTION);
switch(value){
case JOptionPane.YES_OPTION:return saveFile();
case JOptionPane.NO_OPTION:return true;
case JOptionPane.CANCEL_OPTION:
default: return false;
}
}
//saveFile方法保存文本文件 boolean saveFile(){
boolean saveNeeded;
String currentFileName;
if(currentFileName == null){
return saveAsFile();
}
try{
File file = new File(currentFileName);
FileWriter out = new FileWriter(file);
String text = jTextArea1.getText();
out.write(text);
out.close();
this.saveNeeded = false;
updateTitle();
return true;
}
catch (IOException e){
statusBar.setText("存储"+currentFileName+"出错");
}
return false;
}
//SaveAs方法另存文件
boolean saveAsFile(){
//boolean saveNeeded;
//String currentFileName;
this.repaint();
if(JFileChooser.APPROVE_OPTION == jFileChooser1.showSaveDialog(this)){
currentFileName = jFileChooser1.getSelectedFile().getPath();
this.repaint();
}
else{
this.repaint();
return false;
}
}
void updateTitle(){
//String currentFileName;
//boolean saveNeeded=false;
String title;
if(currentFileName == null){
title = "未命名文件";
}else{
title=currentFileName;
}
if(saveNeeded==true){
title="*"+title;
}
title="文本编辑器-"+title;
this.setTitle(title);
} void jFileChooser1_actionPerformed(ActionEvent e) { }}class ActionListener implements java.awt.event.ActionListener {
TextEditorFrame adaptee; ActionListener(TextEditorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jMenuBackgroundColor_actionPerformed(e); // ChangedUpdate(DocumentEvent e);
// InsertUpdate(DocumentEvent e);
// RemoveUpdate(DocumentEvent e);
}
}
调试后,错误为:
"TextEditorFrame.java": Error #: 300 : method ActionListener() not found in class textedit.ActionListener at line 82, column 55
"TextEditorFrame.java": Error #: 300 : constructor ActionListener() not found in class textedit.ActionListener at line 82, column 59
"TextEditorFrame.java": Error #: 300 : method ActionListener() not found in class textedit.ActionListener at line 90, column 56
"TextEditorFrame.java": Error #: 300 : constructor ActionListener() not found in class textedit.ActionListener at line 90, column 60
"TextEditorFrame.java": Error #: 300 : class IOException not found in class textedit.TextEditorFrame at line 298, column 12
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货