0

So basically the program will work like this. The first window comes up and asks with 2 buttons whether you want to continue the program or exit the program. if you choose to continue then the program continues on to whatever is in the if statement.

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.*;
import java.awt.*;
import java.util.Scanner;
import java.io.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.*;
 public class Herons extends JFrame implements ActionListener {
 public static JTextField a;
 public static JTextField b;
 public static JTextField c;
 public static JFrame main = new JFrame("Herons Formula");
 public static JPanel myPanel = new JPanel(new GridLayout (0,1));
public static void main(String args[]){
     //splashscr();
     Herons object = new Herons();
    }

Herons(){
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel myPanel1 = new JPanel(new GridBagLayout());
    myPanel.setPreferredSize(new Dimension(515,125));
    JLabel lab1 = new JLabel(
    ("Herons Formula uses the lenghts of the sides of a triangle to calculate its area."));
    main.add(myPanel);
    lab1.setHorizontalAlignment(JLabel.CENTER);
    myPanel.add(lab1);
    JButton button1 = new JButton("Use the Formula!");
    button1.setPreferredSize(new Dimension(20, 20));
    JButton button2 = new JButton("Exit the program");
    myPanel.add(button1);
    myPanel.add(button2);
    button1.addActionListener(this);
    button2.addActionListener(this);
    main.pack();
    main.setVisible(true);
//Not really sure what to do with this
    if (myPanel1.hasBeenDisposed()){
    //JFrame main = new JFrame("Herons Formula");
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //JPanel myPanel = new JPanel(new GridLayout (0,1));
    //JPanel pane = new JPanel(new GridLayout(0,1));

    myPanel.setPreferredSize(new Dimension(325,275));

    a = new JTextField(3);
    b = new JTextField(3);
    c = new JTextField(3);
    JButton find = new JButton("Calculate!");

    main.add(myPanel);
    myPanel.add(new JLabel ("Input the lengh of each side of the triangle"));
    main.add(myPanel);
    myPanel.add(new JLabel ("Side A:"));
    myPanel.add(a);

    myPanel.add(new JLabel ("Side B:"));
    myPanel.add(b);

    myPanel.add(new JLabel ("Side C:"));
    myPanel.add(c);

    myPanel.add(find);
    //find.setActionCommand("Calculate!");
    find.addActionListener(this);
    main.pack();
    main.setVisible(true);
}
}
 public void actionPerformed(ActionEvent e) {
  String actionCommand = ((JButton) e.getSource()).getActionCommand();
    //System.out.println("Action command for pressed button: " + actionCommand);
    if (actionCommand == "Calculate!") {

     main.setVisible(false);
     myPanel.setVisible(false);
     main.dispose();

    double aaa = Double.parseDouble(a.getText());
    double bbb = Double.parseDouble(b.getText());
    double ccc = Double.parseDouble(c.getText());
    double s = 0.5 * (aaa + bbb + ccc);
    double area = Math.sqrt(s*(s-aaa)*(s-bbb)*(s-ccc));
    area = (int)(area*10000+.5)/10000.0;
    if (area == 0){
        area = 0;
    }
     JOptionPane.showMessageDialog(this, "The area of the triangle is: " + area,"Herons Formula", JOptionPane.PLAIN_MESSAGE);

  }
   if (actionCommand ==  "Use the Formula!" ){
        myPanel1.setVisible(false);
    } 
    if (actionCommand == "Exit the program"){
        System.exit(0);
    }
}
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • 1
    What's your question/problem? – Paul Samsotha Feb 01 '14 at 06:30
  • 2
    Here's one `if (actionCommand == "Calculate!")`... **Don't** compare strings with `==`. Use `equals`. See [**How do I compare String in Java**](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). You need to change _all_ of them. – Paul Samsotha Feb 01 '14 at 06:32
  • For many components in one space, use a [`CardLayout`](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html) as seen in this [short example](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Feb 01 '14 at 07:30

0 Answers0