我的代码遇到麻烦。每次使用try-catch块时都会出现错误。我得到的错误是,catch块中的变量e已在actionPerformed(ActionEvent)方法中定义。我觉得这是一个容易解决的错误,但是很不幸,我遇到了麻烦。基本上,程序将检查输入的文件是否在计算机上(如果没有问题的话)。如果用户没有输入任何内容并单击ok或yes,它将返回GUI,但是如果用户输入了错误的文件名或不在计算机上,它将通过try语句,程序将抛出e。
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
//The check boxes and radio buttons should change the font and the style
//in text area
//To do this, make an inner listener class that handles just check boxes
//and radio buttons.
public class Checkpoint21 extends JFrame
{
private JTextArea area;
private JButton newfile,open,save,cut,paste,copy,exit;
private JCheckBox bold, italic;
private JRadioButton timesnewromam,courier,arial;
private ButtonGroup sg;
private File f;
public Checkpoint21()
{
super("Toy Editor");
setBounds (100,100,500,500);
area = new JTextArea();
add(area);
area.setFont(new Font("Arial",Font.PLAIN,12));
JScrollPane sp=new JScrollPane(area,ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(sp,BorderLayout.CENTER);
area.setLineWrap(true);
//North Panel
JPanel buttonPanel = new JPanel();
newfile=new JButton("New File");
open=new JButton("Open");
save=new JButton("Save");
cut=new JButton("Cut");
paste=new JButton("Paste");
copy=new JButton("Copy");
exit=new JButton("Exit");
buttonPanel.add(newfile);
buttonPanel.add(open);
buttonPanel.add(save);
buttonPanel.add(cut);
buttonPanel.add(paste);
buttonPanel.add(copy);
buttonPanel.add(exit);
add(buttonPanel,BorderLayout.NORTH);
//South Panel
JPanel s = new JPanel();
bold = new JCheckBox("Bold");
italic = new JCheckBox("Italic");
timesnewromam=new JRadioButton("Times New Roman",false);
courier=new JRadioButton("Courier",false);
arial=new JRadioButton("Arial",true);
sg=new ButtonGroup();
sg.add(timesnewromam);
sg.add(courier);
sg.add(arial);
//panel add
s.add(timesnewromam);
s.add(courier);
s.add(arial);
add(s,BorderLayout.SOUTH);
setVisible(true);
//button actionlisteners
bold.addActionListener(new BoxesAndButtons());
italic.addActionListener(new BoxesAndButtons());
timesnewromam.addActionListener(new BoxesAndButtons());
courier.addActionListener(new BoxesAndButtons());
arial.addActionListener(new BoxesAndButtons());
//button action listeners
newfile.addActionListener(new BoxesAndButtons());
open.addActionListener(new BoxesAndButtons());
save.addActionListener(new BoxesAndButtons());
cut.addActionListener(new BoxesAndButtons());
paste.addActionListener(new BoxesAndButtons());
copy.addActionListener(new BoxesAndButtons());
exit.addActionListener(new BoxesAndButtons());
}
private class BoxesAndButtons implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
String s="";
if(bold.isSelected())
{
area.setFont(new Font("Arial",Font.BOLD,12));
}
if(italic.isSelected())
{
area.setFont(new Font("Arial",Font.ITALIC,12));
}
if(timesnewromam.isSelected())
{
area.setFont(new Font("Times New Roman",Font.PLAIN,12));
}
if(courier.isSelected())
{
area.setFont(new Font("Courier",Font.PLAIN,12));
}
if(arial.isSelected())
{
area.setFont(new Font("Arial",Font.PLAIN,12));
}
if (e.getSource() == cut)
{
area.cut();
}
else if (e.getSource() == paste)
{
area.paste();
}
else if (e.getSource() ==copy)
{
area.copy();
}
else if(e.getSource() ==newfile)
{
area.setText("");
}
else if(e.getSource()==open)
{
s=JOptionPane.showInputDialog(null, "Enter File Name:");
if(s==null)
{
return;
}
else
{
f=new File("");
try
{
FileReader in = new FileReader(f);
area.read(in, null);
in.close();
}
catch(FileNotFoundException e)
{
JOptionPane.showMessageDialog(null,"File not Found","Input Error", JOptionPane.ERROR_MESSAGE);
s=JOptionPane.showInputDialog(null, "Enter File Name:");
}
}
}
else if(e.getSource()==save)
{
f=new File("");
FileWriter out = new FileWriter(f);
area.write(out);
out.close();
}
else
{
System.exit(0);
}
}
}
public static void main(String[]args)
{
Checkpoint21 c=new Checkpoint21();
}
}```