Попробуй и поймай ошибки - PullRequest
0 голосов
/ 12 апреля 2020

У меня проблемы с моим кодом. Я получаю ошибку каждый раз с блоком try-catch. Полученная ошибка говорит о том, что переменная e в блоке catch уже определена в методе actionPerformed (ActionEvent). Я чувствую, что это легко исправить, но, к сожалению, у меня проблемы. В основном программа проверит, находится ли файл, который вводится, на компьютере, если это не произойдет, проблем не произойдет. Если пользователь ничего не печатает и нажимает «ОК» или «Да», он просто вернется к GUI, но если пользователь введет неправильное имя файла или его нет на компьютере, он будет go через оператор try, и программа выполнит команду бросить е.

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(); 
  }
}```
...