IOException in Java [message #1858060] |
Tue, 14 March 2023 00:04 |
bretny relly Messages: 14 Registered: March 2023 |
Junior Member |
|
|
I'm working on a rudimentary Chat Bot application that replies to what the user enters. I'm saving the questions it responds to, as well as the answers to those questions, in a text file. I am reading the file with a scanner. This is also just my second application in which I utilise a GUI to interface 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 far as I understand GUIs in Java, you must extend JFrame in the main class. This is the other programme I made for a graphical software that estimates 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'm using this application to experiment with alternative GUIs. It says I can't raise an IOException within the CalculateButtonHandler. I want to use a pretty similar structure for the Chat Bot software, however following this document I'm curious whether there is a solution for the IOException, or if there is a better and easier approach to create a GUI?
[Updated on: Wed, 15 March 2023 11:23] Report message to a moderator
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03137 seconds