I have a extremely small problem, I think its my own fault, but i just cant seem to find what it is. I'm making a program with a gui, and in the gui there is a main menu with 2 JButtons. When I click on a JButton the JPanel/JFrame is totally cleared and some other JButtons are displayed. One of these JButtons says Main Menu, so when it gets clicked, I want it to go back to the Main Menu, but nothing happens. I think it is a problem with the way I'm trying to make the program go to the method new AnimalProfitRatioCalc (); in public void actionPerformed (ActionEvent e)
here is my code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
/*
*Created by
*on Jan 4
*to Calculate the profit ratio of animals and display them in sorted order
*/
public class AnimalProfitRatioCalc extends JFrame implements ActionListener
{
static Container contentPane;
static JPanel infoPane;
public AnimalProfitRatioCalc ()
{
contentPane = getContentPane ();
if (infoPane != null)
{
contentPane.remove (infoPane);
}
infoPane = new JPanel ();
infoPane.setLayout (null); //setting layout to null so i can use absolute positioning with coordinates
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);
menu = new JMenu ("Options");
MenuItem.addActionListener (this);
MenuBar.add (menu);
MenuItem = new JMenuItem ("Main Menu");
MenuItem.setToolTipText ("Go to main menu"); //tip
MenuItem.addActionListener (this);
menu.add (MenuItem);
MenuItem = new JMenuItem ("Enter Animal Statistics");
MenuItem.setToolTipText ("Animal category, Breed, Acquisition price, selling price "); //tip
MenuItem.addActionListener (this);
menu.add (MenuItem);
MenuItem = new JMenuItem ("Display Animals with profit ratio");
MenuItem.setToolTipText ("Display Animals with preferred profit ratio in sorted order"); //tip
MenuItem.addActionListener (this);
menu.add (MenuItem);
JLabel L1;
//all the JLabels
L1 = new JLabel ("Animal Profit Ratio Calculator");
L1.setFont (new Font ("Arial", Font.BOLD, 24));
L1.setBounds (232, 10, 342, 20);
infoPane.add (L1);
//all JButtons
JButton eas, da;
eas = new JButton ("Enter Animal Statistics");
eas.setToolTipText ("Animal category, Breed, Acquisition price, selling price "); //tip
eas.setFont (new Font ("Arial", Font.BOLD, 18));
eas.addActionListener (this);
eas.setBounds (275, 75, 256, 50);
infoPane.add (eas);
da = new JButton ("Display Animals with profit ratio");
da.setToolTipText ("Display Animals with preferred profit ratio in sorted order"); //tip
da.setFont (new Font ("Arial", Font.BOLD, 18));
da.addActionListener (this);
da.setBounds (246, 175, 312, 50);
infoPane.add (da);
contentPane.add (infoPane);
validate ();
}
public void actionPerformed (ActionEvent e)
{
String event = e.getActionCommand ();
if (event.equals ("Quit"))
{
hide ();
System.exit (0);
}
if (event.equals ("Enter Animal Statistics"))
{
AddStatistics ();
}
if (event.equals ("Display Animals with profit ratio"))
{
DisplayStatistics ();
}
if (event.equals ("Main Menu"))
{
new AnimalProfitRatioCalc ();
}
}
public void AddStatistics ()
{
contentPane = getContentPane ();
if (infoPane != null)
{
contentPane.remove (infoPane);
}
infoPane = new JPanel ();
infoPane.setLayout (null); //setting layout to null so i can use absolute positioning with
JLabel L1, L2, L3, L4, L5;
//all the JLabels
L1 = new JLabel ("Enter Animal Statistics");
L1.setFont (new Font ("Arial", Font.BOLD, 24));
L1.setBounds (272, 10, 261, 20);
infoPane.add (L1);
L2 = new JLabel ("Animal Catagory");
L2.setFont (new Font ("Arial", Font.PLAIN, 18));
L2.setBounds (125, 100, 261, 20);
infoPane.add (L2);
L3 = new JLabel ("Animal Breed");
L3.setFont (new Font ("Arial", Font.PLAIN, 18));
L3.setBounds (125, 150, 261, 20);
infoPane.add (L3);
L4 = new JLabel ("Acquisition Price");
L4.setFont (new Font ("Arial", Font.PLAIN, 18));
L4.setBounds (125, 200, 261, 20);
infoPane.add (L4);
L5 = new JLabel ("Selling Price");
L5.setFont (new Font ("Arial", Font.PLAIN, 18));
L5.setBounds (125, 250, 261, 20);
infoPane.add (L5);
JTextField J1, J2, J3, J4;
//all JTextFields
J1 = new JTextField (20);
J1.setFont (new Font ("Arial", Font.PLAIN, 14));
J1.setToolTipText ("Catagory"); //tip
J1.setBounds (350, 100, 120, 20);
infoPane.add (J1);
J2 = new JTextField (20);
J2.setFont (new Font ("Arial", Font.PLAIN, 14));
J2.setToolTipText ("Breed"); //tip
J2.setBounds (350, 150, 120, 20);
infoPane.add (J2);
J3 = new JTextField (20);
J3.setFont (new Font ("Arial", Font.PLAIN, 14));
J3.setToolTipText ("Acquisition Price"); //tip
J3.setBounds (350, 200, 120, 20);
infoPane.add (J3);
J4 = new JTextField (20);
J4.setFont (new Font ("Arial", Font.PLAIN, 14));
J4.setToolTipText ("Selling Price"); //tip
J4.setBounds (350, 250, 120, 20);
infoPane.add (J4);
JButton B1, B2;
B1 = new JButton ("Add Animal");
B1.setToolTipText ("Add animal to data base"); //tip
B1.setFont (new Font ("Arial", Font.PLAIN, 18));
B1.addActionListener (this);
B1.setBounds (550, 100, 130, 170);
infoPane.add (B1);
B2 = new JButton ("Main Menu");
B2.setFont (new Font ("Arial", Font.PLAIN, 18));
B2.addActionListener (this);
B2.setBounds (272, 300, 261, 50);
infoPane.add (B2);
contentPane.add (infoPane);
validate ();
}
public void DisplayStatistics ()
{
}
public static void main (String[] args) //main
{
AnimalProfitRatioCalc window = new AnimalProfitRatioCalc ();
window.setTitle ("Animal Profit Ratio Calculator");
window.setSize (800, 600);
window.setVisible (true);
}
}