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

    Default Rerunning 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)
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Rerunning Parts of Code

    Don't double post. Please go through the Forum Rules -- particularly the second paragraph.

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 0
    Last Post: 09-12-2011, 01:06 PM
  2. Eclipse not underlining incorrect parts of the code
    By eLancaster in forum New To Java
    Replies: 3
    Last Post: 07-03-2011, 05:31 PM
  3. several threads for different parts
    By Itun in forum AWT / Swing
    Replies: 2
    Last Post: 03-20-2011, 11:18 PM
  4. Replies: 4
    Last Post: 03-14-2009, 10:36 AM
  5. Selecting parts of an image
    By shaungoater in forum Java 2D
    Replies: 1
    Last Post: 12-15-2007, 10:06 PM

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
  •