Menues and Windows

EasyApp will make Menues and MenuItems using an add command, just like Buttons and TextFields.  But Menues don't require coordinates, since they automatically appear at the top of the Window.

Notice the actions method must use command.equals to check for a MenuItem being clicked, rather than source ==.

Every EasyApp extended class creates its own Window automatically, so any new object will automatically pop-up a new Window (if it extends EasyApp).  So the easy way to make multiple Windows is simply to make multiple classes.

You can use dispose() to destroy an object (and its Window), as long as there is no pointer pointing at it - e.g. the main program did not make a variable point at the object.

If you need a copy of EasyApp, download it here:  EasyApp



import
java.awt.*;
import java.awt.event.*;

public class Menues extends EasyApp
{  public static void main(String[] args)
   { new Menues(); }
   
   public Menues()
   {
      setSize(200,200);
      setTitle("Menues");
   }

   //--- Controls - create menus here ------
   
   Menu mFiles = addMenu("Files|Help|Exit");
   Menu mMath = addMenu("Math|Fractions|Quadratics|Random");
      
   public void actions(Object source,String command)
   {
      if (command.equals("Files|Exit"))
      {  System.exit(0); }
      else if (command.equals("Files|Help"))
      {  runProgram("explorer help.html"); }
      else if (command.equals("Math|Fractions"))
      {  new FractionCalculator(); }
      else if (command.equals("Math|Quadratics"))
      {  new QuadraticSolver(); }
      else if (command.equals("Math|Random"))
      {  new RandomGenerator(300,200); }
   }
}

class FractionCalculator extends EasyApp
{
   TextField top1 = addTextField("",50,50,50,30,this);
   TextField bot1 = addTextField("",50,100,50,30,this);
   TextField top2 = addTextField("",150,50,50,30,this);
   TextField bot2 = addTextField("",150,100,50,30,this);
   TextField top3 = addTextField("",250,50,50,30,this);
   TextField bot3 = addTextField("",250,100,50,30,this);
   Label plus = addLabel("+",120,80,20,20,this);
   Button equals = addButton("=",210,80,30,30,this);
   
   public FractionCalculator()
   {
      setTitle("Fraction Calculator");  // window title
      setBounds(100,100,340,200);       // position and size of window
      Font bigger = new Font("Arial",0,20);   // create a Font
      top1.setFont(bigger);             // Use new Font
      bot1.setFont(bigger);             // in all the TextFields
      top2.setFont(bigger);
      bot2.setFont(bigger);
      top3.setFont(bigger);
      bot3.setFont(bigger);
   }
   
   public void actions(Object source,String command)
   {
      if (source == equals)
      {
         int t1 = Integer.parseInt(top1.getText());  // change String to int
         int b1 = Integer.parseInt(bot1.getText());
         int t2 = Integer.parseInt(top2.getText());
         int b2 = Integer.parseInt(bot2.getText());
         int t3 = t1*b2 + t2*b1;
         int b3 = b1*b2;
         top3.setText(t3 + "");
         bot3.setText(b3 + "");
      }
   }
   
   public void paint(Graphics g)
   {
      g.setColor(Color.blue);             // use Blue ink
      
      g.setFont(new Font("Arial",0,20));  // Font size 24 pixels
                                          //   3 = bold italic
      
      g.drawString("Type 4 numbers, then press [=]",30,170);      
        // print at position 50,170 - that is the BOTTOM left corner
      
   }
}

class QuadraticSolver extends EasyApp
{
   public QuadraticSolver()
   {  
      setTitle("Quadratic Solver");
      double a = inputDouble("A = ");
      double b = inputDouble("B = ");
      double c = inputDouble("C = ");

      if (b == 0 && c/a >= 0)
      {           
         output("x = + or - " + Math.sqrt(c/a));
      }
      else
      {  output("I don't know how to solve that"); }

      dispose();              // closes window and destroys Object
   }
   
   public void paint(Graphics g)
   {
      g.setColor(Color.blue);            
      
      g.setFont(new Font("Arial",0,20));
      
      g.drawString("Solves quadratics in this form:",20,60);      
      g.drawString("A x^2 + B x + C = 0 ",20,90);      
      g.drawString("but only if B = 0",20,120);      
   }
}

class RandomGenerator extends EasyApp
{
   Button go = addButton("Go",100,50,100,50,this);
   TextField num = addTextField("",30,120,240,30,this);

   public RandomGenerator(int left,int top)
   {
      setTitle("Random Generator");
      setBounds(left,top,300,200);   
      num.setFont(new Font("Arial",0,20) );      
   }
   
   public void actions(Object source,String command)
   {
      num.setText(Math.random()+"");
   }
}