Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: New to Java: Adding Functionality to my Buttons

  1. #1
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default [SOLVED]New to Java: Adding Functionality to my Buttons

    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.
    Last edited by Staticity; July 24th, 2011 at 01:34 AM.


Similar Threads

  1. Replies: 1
    Last Post: July 20th, 2011, 09:03 AM
  2. Help with Java buttons
    By Nameless _1 in forum AWT / Java Swing
    Replies: 2
    Last Post: June 18th, 2011, 04:16 PM
  3. Java App - Add, Delete, Reorder elements of the buttons
    By alibm in forum AWT / Java Swing
    Replies: 5
    Last Post: March 7th, 2011, 08:46 AM
  4. Java swing buttons are flickering in windows 7 environment
    By javasam in forum Member Introductions
    Replies: 1
    Last Post: February 17th, 2011, 09:51 AM
  5. FAX Functionality in JAVA Application
    By animesh in forum Java Theory & Questions
    Replies: 0
    Last Post: February 11th, 2010, 07:39 AM