简体   繁体   中英

How to throw IOException in Java when you are Extening JFrame and Extending ActionListing

I am writing a basic Chat Bot program, where is takes what the user types it, and responds. I am storing the questions it responds to, and the answers to those questions in a text file. I am using a scanner to read the file. This is also only my second program where I am using a GUI to interact with the user. I have one TextField, one Label, and Two buttons(One to enter the question asked and the other to exit the program). As I understand GUIs in Java, you must extend JFrame in the main class. This is the other program that I wrote for a graphical program that calculates the area of a rectangle:

 import java.awt.*; import javax.swing.*; import java.awt.event.*; public class RectangleProgram extends JFrame { private static final int WIDTH = 400; private static final int HEIGHT = 200; private JLabel lengthL, widthL, areaL; private JTextField lengthTF, widthTF, areaTF; private JButton calculateB, exitB; //Button handlers: private CalculateButtonHandler cbHandler; private ExitButtonHandler ebHandler; public RectangleProgram() { lengthL = new JLabel("Enter the length: ", SwingConstants.RIGHT); widthL = new JLabel("Enter the width: ", SwingConstants.RIGHT); areaL = new JLabel("Area: ", SwingConstants.RIGHT); lengthTF = new JTextField(10); widthTF = new JTextField(10); areaTF = new JTextField(10); //SPecify handlers for each button and add (register) ActionListeners to each button. calculateB = new JButton("Calculate"); cbHandler = new CalculateButtonHandler(); calculateB.addActionListener(cbHandler); exitB = new JButton("Exit"); ebHandler = new ExitButtonHandler(); exitB.addActionListener(ebHandler); setTitle("Sample Title: Area of a Rectangle"); Container pane = getContentPane(); pane.setLayout(new GridLayout(4, 3)); //Add things to the pane in the order you want them to appear (left to right, top to bottom) pane.add(lengthL); pane.add(lengthTF); pane.add(widthL); pane.add(widthTF); pane.add(areaL); pane.add(areaTF); pane.add(calculateB); pane.add(exitB); setSize(WIDTH, HEIGHT); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } private class CalculateButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { double width, length, area; length = Double.parseDouble(lengthTF.getText()); //We use the getText & setText methods to manipulate the data entered into those fields. width = Double.parseDouble(widthTF.getText()); area = length * width; areaTF.setText("" + area); } } public class ExitButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } public static void main(String[] args) { RectangleProgram rectObj = new RectangleProgram(); } } 

I am using this to test different things with GUIs with this program. When I try to throw and IOException within the CalculateButtonHandler, it says I can't. I am planning on using a very similar setup for the Chat Bot program, but am wondering if there is a workaround for the IOException, or if there is a better and easier way to make a GUI?

You cannot change methods already defined in an interface to throw Exceptions that they do not already declare to be thrown. If you really want to throw an Exception you can throw some sort of RuntimeException since that is not a checked exception it does not have to be declared as being thrown.

With that being said you should really avoid throwing exceptions in the Event Dispatch Thread. The Event Dispatch Thread is where all GUI related events are handled. When you write code that executes in the Event Dispatch Thread it should be short, fast and robust. Robust meaning is should not have code that needs error handling or it should quickly handle the error and move on.

For more one Event Dispath you can look at this explanation: Java Event-Dispatching Thread explanation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM