import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JOptionPane; import java.awt.*; class test extends JFrame{ //Create Button Variables private Button add; public test(){ super("Calculator"); setLayout(new FlowLayout()); //Addition Button try{ add = new Button("+"); add(add, BorderLayout.SOUTH); String addfn = JOptionPane.showInputDialog("Enter First Number: "); String addsn = JOptionPane.showInputDialog("Enter Second Number: "); double addnum1 = Double.parseDouble(addfn); double addnum2 = Double.parseDouble(addsn); double addsum; addsum = addnum1 + addnum2; JOptionPane.showMessageDialog(null, "The Sum of " + addnum1 + " and " + addnum2 + " is " + addsum, "Addition Operation", JOptionPane.PLAIN_MESSAGE ); }catch(Exception e){ } //Handler Class HandlerClass handler = new HandlerClass(); add.addActionListener(handler); } private class HandlerClass implements ActionListener{ //actionPerformed is the only method to override public void actionPerformed(ActionEvent event){ JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand())); } } public static void main(String[] args){ //Created Object for Window test wo = new test(); //Allow Program to Close on Exit wo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Program Window Settings wo.setSize(400,300); wo.setVisible(true); } }
[This is only a portion of my program for the sake of space]
In the process of teaching myself Java, I'm trying to make a Simple Calculator that opens up a window with several buttons.. Each button sends the user to its corresponding operation.. "+" button sends you to an operation that allows you to add two numbers.. etc..
However, (Sorry if my terminology is off, I will try to explain my issue as best as possible) when I run my program, it goes straight to the content I want my "Addition Button" to contain instead of going to the window with the clickable "+" button.
Basically, the program runs backwards.. Once I type in the two numbers I want to add, it goes to the window with the "+" button.. So I'm pretty much asking these questions.. How can I start at the window containing the buttons? How can I add functionality to each button so the user can choose which operation he/she would like to use? Also, how can I return to my main window containing the buttons once the user has finished their first operation..
I've looked through several Youtube videos and I have also researched this. Either I cannot find anything that is relevant to my issue, or the information I am provided with is too complex for a beginner. Any help will be greatly appreciated.