import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
*Created by ************
*on Dec 28
*to calculate the area of multiple shapes
*/
public class AreaCalculator extends JFrame implements ActionListener
{
static JTextField triwidth, trihieght, recwidth, reclength, cirradius;
static Container contentPane;
static JPanel infoPane;
static double circlearea;
static JLabel circleanswer;
public AreaCalculator ()
{
JMenuBar MenuBar;
JMenu menu;
JMenuItem MenuItem;
MenuBar = new JMenuBar ();
setJMenuBar (MenuBar);
//adding menu
menu = new JMenu ("File");
MenuBar.add (menu);
MenuItem = new JMenuItem ("Quit");
MenuItem.addActionListener (this);
menu.add (MenuItem);
infoPane = new JPanel ();
infoPane.setLayout (null); //setting layout to null so i can use absolute positioning with coordinates
contentPane = getContentPane ();
JLabel L1, L2, L3, L4, L5, circleanswer;
//all the JLabels
L1 = new JLabel ("Welcome to Jonathan's Area Calculator");
L1.setBounds (275, 0, 300, 15);
infoPane.add (L1);
L2 = new JLabel ("Find area of 1 shape at a time");
L2.setBounds (300, 20, 275, 15);
infoPane.add (L2);
L3 = new JLabel ("Circle...(Enter Radius)");
L3.setBounds (100, 100, 300, 15);
infoPane.add (L3);
L4 = new JLabel ("Triangle...(Enter base and hieght)");
L4.setBounds (100, 150, 400, 15);
infoPane.add (L4);
L5 = new JLabel ("Square/Rectangle...(Enter Length and Width)");
L5.setBounds (100, 200, 400, 15);
infoPane.add (L5);
//all JTextFields
cirradius = new JTextField (20);
cirradius.setToolTipText ("Radius"); //tip
cirradius.setBounds (450, 100, 80, 20);
infoPane.add (cirradius);
triwidth = new JTextField (20);
triwidth.setToolTipText ("Base"); //tip
triwidth.setBounds (450, 150, 80, 20);
infoPane.add (triwidth);
trihieght = new JTextField (20);
trihieght.setToolTipText ("Hieght"); //tip
trihieght.setBounds (550, 150, 80, 20);
infoPane.add (trihieght);
reclength = new JTextField (20);
reclength.setToolTipText ("Length"); //tip
reclength.setBounds (450, 200, 80, 20);
infoPane.add (reclength);
recwidth = new JTextField (20);
recwidth.setToolTipText ("Width"); //tip
recwidth.setBounds (550, 200, 80, 20);
infoPane.add (recwidth);
//all JButtons
JButton areacircle, areatriangle, arearecsquare;
areacircle = new JButton ("Calculate......");
areacircle.addActionListener (this);
areacircle.setBounds (650, 100, 100, 20);
infoPane.add (areacircle);
areatriangle = new JButton ("Calculate.......");
areatriangle.addActionListener (this);
areatriangle.setBounds (650, 150, 100, 20);
infoPane.add (areatriangle);
arearecsquare = new JButton ("Calculate........");
arearecsquare.setBounds (650, 200, 100, 20);
arearecsquare.addActionListener (this);
infoPane.add (arearecsquare);
//
//
//this is were i need help, its not updating circle area and its static
circleanswer = new JLabel ();
circleanswer.setText ("The Area is: " + circlearea);
circleanswer.setBounds (450, 250, 100, 20);
infoPane.add (circleanswer);
//
//
//
contentPane.add (infoPane);
validate ();
}
public void actionPerformed (ActionEvent e)
{
String event = e.getActionCommand ();
if (event.equals ("Quit"))
{
hide ();
System.exit (0);
}
if (event.equals ("Calculate......")) //"circle"
{
circleanswer.setText("The Area is: " + circlearea);
}
if (event.equals ("Calculate.......")) //"triangle"
{
}
if (event.equals ("Calculate........")) //"sqaure/rectangle"
{
}
}
class Calculate extends JPanel //method for finding the area of a circle
{
public void circle ()
{
double radius;
JTextField area;
String a = cirradius.getText (); //getting info that user inputed
radius = Double.parseDouble (a); //changing the string to an double
circlearea = radius * radius * Math.PI; //equation for circle area
}
}
public static void main (String[] args) //main
{
AreaCalculator window = new AreaCalculator ();
window.setTitle ("Universal Area Calculator");
window.setSize (800, 600);
window.setVisible (true);
}
}
I'm trying to make a program that will calculate the area of the shape that the user
inputs the dimensions for, but the circlearea Jlabel wont update. I got this far by looking at other programs, but now I'm just stuck.
I need help
The code runs fine. Right now I'm just working on calculating the area for the circle.
So when I enter a radius, and then click calculate I get an error