Results 1 to 3 of 3
  1. #1
    philip1597 is offline Member
    Join Date
    Aug 2012
    Location
    New York
    Posts
    13
    Rep Power
    0

    Default Repeating Parts of Code

    In this two class project I have created a GUI that takes the input from the user and calculates an answer. In my GUI class, I used a JOptionPane.showInputDialog to obtain the numbers from the user, and a JFrame with JButtons to choose the function. Is it possible, and if so how, to create a button that makes the JOptionPane part of code or, if that is not possible, the entire GUI class run again? Any help is appreciated.

    This is the class for the logic/calculations
    Java Code:
    import javax.swing.*;
    
    public class Calculator {
    	static int numeratorAnswer;
    	static int denominatorAnswer;
    
    	public static void addReduce(int numeratorOne, int denominatorOne,
    			int numeratorTwo, int denominatorTwo) {
    			numeratorOne *= denominatorTwo;
    			numeratorTwo *= denominatorOne;
    			denominatorTwo *= denominatorOne;
    			denominatorOne = denominatorTwo;
    			numeratorAnswer = numeratorOne + numeratorTwo;
    			denominatorAnswer = denominatorTwo;
    			if (numeratorAnswer < denominatorAnswer) {
    				for (int i = numeratorAnswer + 1; i > 1; i--) {
    					if (denominatorAnswer % numeratorAnswer == 0) {
    						denominatorAnswer /= numeratorAnswer;
    						numeratorAnswer = 1;
    					} else if (numeratorAnswer % i == 0
    							&& denominatorAnswer % i == 0) {
    						numeratorAnswer /= i;
    						denominatorAnswer /= i;
    					}
    				}
    			} else if (numeratorAnswer > denominatorAnswer) {
    				for (int i = denominatorAnswer + 1; i > 1; i--) {
    					if (denominatorAnswer % numeratorAnswer == 0) {
    						denominatorAnswer /= numeratorAnswer;
    						numeratorAnswer = 1;
    					} else if (numeratorAnswer % i == 0
    							&& denominatorAnswer % i == 0) {
    						numeratorAnswer /= i;
    						denominatorAnswer /= i;
    					}
    
    				}
    			} else if (numeratorAnswer == denominatorAnswer) {
    				numeratorAnswer = 1;
    				denominatorAnswer = 1;
    			}
    
    		JOptionPane.showMessageDialog(null, numeratorAnswer + "/"
    				+ denominatorAnswer);
    	}
    
    	public static void subtractReduce(int numeratorOne, int denominatorOne,
    			int numeratorTwo, int denominatorTwo) {
    		numeratorOne *= denominatorTwo;
    		numeratorTwo *= denominatorOne;
    		denominatorTwo *= denominatorOne;
    		denominatorOne = denominatorTwo;
    		numeratorAnswer = numeratorOne - numeratorTwo;
    		denominatorAnswer = denominatorTwo;
    		if (numeratorAnswer < denominatorAnswer) {
    			for (int i = numeratorAnswer + 1; i > 1; i--) {
    				if (denominatorAnswer % numeratorAnswer == 0) {
    					denominatorAnswer /= numeratorAnswer;
    					numeratorAnswer = 1;
    				} else if (numeratorAnswer % i == 0
    						&& denominatorAnswer % i == 0) {
    					numeratorAnswer /= i;
    					denominatorAnswer /= i;
    				}
    			}
    		} else if (numeratorAnswer > denominatorAnswer) {
    			for (int i = denominatorAnswer + 1; i > 1; i--) {
    				if (denominatorAnswer % numeratorAnswer == 0) {
    					denominatorAnswer /= numeratorAnswer;
    					numeratorAnswer = 1;
    				} else if (numeratorAnswer % i == 0
    						&& denominatorAnswer % i == 0) {
    					numeratorAnswer /= i;
    					denominatorAnswer /= i;
    				}
    
    			}
    		} else if (numeratorAnswer == denominatorAnswer) {
    			numeratorAnswer = 1;
    			denominatorAnswer = 1;
    		}
    		JOptionPane.showMessageDialog(null, numeratorAnswer + "/"
    				+ denominatorAnswer);
    	}
    
    	public static void multiplyReduce(int numeratorOne, int denominatorOne,
    			int numeratorTwo, int denominatorTwo) {
    		numeratorAnswer = numeratorOne * numeratorTwo;
    		denominatorAnswer = denominatorOne * denominatorTwo;
    
    		if (numeratorAnswer < denominatorAnswer) {
    			for (int i = numeratorAnswer + 1; i > 1; i--) {
    				if (denominatorAnswer % numeratorAnswer == 0) {
    					denominatorAnswer /= numeratorAnswer;
    					numeratorAnswer = 1;
    				} else if (numeratorAnswer % i == 0
    						&& denominatorAnswer % i == 0) {
    					numeratorAnswer /= i;
    					denominatorAnswer /= i;
    				}
    			}
    		} else if (numeratorAnswer > denominatorAnswer) {
    			for (int i = denominatorAnswer + 1; i > 1; i--) {
    				if (denominatorAnswer % numeratorAnswer == 0) {
    					denominatorAnswer /= numeratorAnswer;
    					numeratorAnswer = 1;
    				} else if (numeratorAnswer % i == 0
    						&& denominatorAnswer % i == 0) {
    					numeratorAnswer /= i;
    					denominatorAnswer /= i;
    				}
    
    			}
    		} else if (numeratorAnswer == denominatorAnswer) {
    			numeratorAnswer = 1;
    			denominatorAnswer = 1;
    		}
    		JOptionPane.showMessageDialog(null, numeratorAnswer + "/"
    				+ denominatorAnswer);
    	}
    
    	public static void divideReduce(int numeratorOne, int denominatorOne,
    			int numeratorTwo, int denominatorTwo) {
    		numeratorAnswer = numeratorOne * denominatorTwo;
    		denominatorAnswer = denominatorOne * numeratorTwo;
    		if (numeratorAnswer < denominatorAnswer) {
    			for (int i = numeratorAnswer + 1; i > 1; i--) {
    				if (denominatorAnswer % numeratorAnswer == 0) {
    					denominatorAnswer /= numeratorAnswer;
    					numeratorAnswer = 1;
    				} else if (numeratorAnswer % i == 0
    						&& denominatorAnswer % i == 0) {
    					numeratorAnswer /= i;
    					denominatorAnswer /= i;
    				}
    			}
    		} else if (numeratorAnswer > denominatorAnswer) {
    			for (int i = denominatorAnswer + 1; i > 1; i--) {
    				if (denominatorAnswer % numeratorAnswer == 0) {
    					denominatorAnswer /= numeratorAnswer;
    					numeratorAnswer = 1;
    				} else if (numeratorAnswer % i == 0
    						&& denominatorAnswer % i == 0) {
    					numeratorAnswer /= i;
    					denominatorAnswer /= i;
    				}
    
    			}
    		} else if (numeratorAnswer == denominatorAnswer) {
    			numeratorAnswer = 1;
    			denominatorAnswer = 1;
    		}
    		JOptionPane.showMessageDialog(null, numeratorAnswer + "/"
    				+ denominatorAnswer);
    	}
    }
    This is the class for the GUI.
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class GUI {
    
    	public static void main(String[] args) {
    		JButton addpiggy = new JButton("+");
    		JButton subtractpiggy = new JButton("-");
    		JButton multiplypiggy = new JButton("*");
    		JButton dividepiggy = new JButton("/");
    		JFrame functions = new JFrame("Choose a Function");
    		functions.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		functions.getContentPane().add(addpiggy, BorderLayout.WEST);
    		functions.getContentPane().add(subtractpiggy, BorderLayout.EAST);
    		functions.getContentPane().add(multiplypiggy, BorderLayout.NORTH);
    		functions.getContentPane().add(dividepiggy, BorderLayout.SOUTH);
    		functions.pack();
    		functions.setVisible(true);
    		addpiggy.addActionListener(new ActionListener(){
    
    	        @Override
    	        public void actionPerformed(ActionEvent arg0) {
    	             //clickedAdd();
    	        }
    	    });
    
    		String numerator1 = JOptionPane.showInputDialog("Insert a Numerator");
    		int numeratorOne = Integer.parseInt(numerator1);
    		String denominator1 = JOptionPane
    				.showInputDialog("Insert a Denominator");
    		int denominatorOne = Integer.parseInt(denominator1);
    		String numerator2 = JOptionPane
    				.showInputDialog("Insert Another Numerator");
    		int numeratorTwo = Integer.parseInt(numerator2);
    		String denominator2 = JOptionPane
    				.showInputDialog("Insert Another Denominator");
    		int denominatorTwo = Integer.parseInt(denominator2);
    
    	}
    	public static void clickedAdd(){
    		Calculator.addReduce(numeratorOne, denominatorOne, numeratorTwo, denominatorTwo)
    	}
    	public static void clickedSubtract(){
    		Calculator.subtractReduce(numeratorOne, denominatorOne, numeratorTwo, denominatorTwo)
    	}
    	public static void clickedMultiply(){
    		Calculator.multiplyReduce(numeratorOne, denominatorOne, numeratorTwo, denominatorTwo)
    	}
    	public static void clickedDivide(){
    		Calculator.divideReduce(numeratorOne, denominatorOne, numeratorTwo, denominatorTwo)
    	}
    }
    Last edited by philip1597; 12-03-2012 at 02:18 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default Re: JButtons/ActionListener

    Quote Originally Posted by philip1597 View Post
    it says that the variables are undefined despite the fact that are all defined immediately before the method is created.
    Yes but declared inside the main method making them local variables.

  3. #3
    philip1597 is offline Member
    Join Date
    Aug 2012
    Location
    New York
    Posts
    13
    Rep Power
    0

    Default Re: JButtons/ActionListener

    Quote Originally Posted by Junky View Post
    Yes but declared inside the main method making them local variables.
    Oh...I can not believe I did not think of that thank you for your time.

Similar Threads

  1. jbuttons
    By sarah jain in forum AWT / Swing
    Replies: 3
    Last Post: 03-16-2011, 07:13 AM
  2. Please help!JButtons
    By fourpixel in forum AWT / Swing
    Replies: 6
    Last Post: 09-29-2010, 01:48 PM
  3. Help with JButtons...
    By ashton in forum New To Java
    Replies: 8
    Last Post: 01-26-2009, 09:38 AM
  4. JButtons
    By jadaleus in forum Advanced Java
    Replies: 4
    Last Post: 10-17-2008, 02:49 AM
  5. JButtons
    By fgasimzade in forum SWT / JFace
    Replies: 1
    Last Post: 12-25-2007, 05:39 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •