1. Member
Join Date
Aug 2012
Location
New York
Posts
13
Rep Power
0

## 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 {

public static void addReduce(int numeratorOne, int denominatorOne,
int numeratorTwo, int denominatorTwo) {
numeratorOne *= denominatorTwo;
numeratorTwo *= denominatorOne;
denominatorTwo *= denominatorOne;
denominatorOne = denominatorTwo;
for (int i = numeratorAnswer + 1; i > 1; i--) {
} else if (numeratorAnswer % i == 0
&& denominatorAnswer % i == 0) {
}
}
for (int i = denominatorAnswer + 1; i > 1; i--) {
} else if (numeratorAnswer % i == 0
&& denominatorAnswer % i == 0) {
}

}
}

}

public static void subtractReduce(int numeratorOne, int denominatorOne,
int numeratorTwo, int denominatorTwo) {
numeratorOne *= denominatorTwo;
numeratorTwo *= denominatorOne;
denominatorTwo *= denominatorOne;
denominatorOne = denominatorTwo;
for (int i = numeratorAnswer + 1; i > 1; i--) {
} else if (numeratorAnswer % i == 0
&& denominatorAnswer % i == 0) {
}
}
for (int i = denominatorAnswer + 1; i > 1; i--) {
} else if (numeratorAnswer % i == 0
&& denominatorAnswer % i == 0) {
}

}
}
}

public static void multiplyReduce(int numeratorOne, int denominatorOne,
int numeratorTwo, int denominatorTwo) {

for (int i = numeratorAnswer + 1; i > 1; i--) {
} else if (numeratorAnswer % i == 0
&& denominatorAnswer % i == 0) {
}
}
for (int i = denominatorAnswer + 1; i > 1; i--) {
} else if (numeratorAnswer % i == 0
&& denominatorAnswer % i == 0) {
}

}
}
}

public static void divideReduce(int numeratorOne, int denominatorOne,
int numeratorTwo, int denominatorTwo) {
for (int i = numeratorAnswer + 1; i > 1; i--) {
} else if (numeratorAnswer % i == 0
&& denominatorAnswer % i == 0) {
}
}
for (int i = denominatorAnswer + 1; i > 1; i--) {
} else if (numeratorAnswer % i == 0
&& denominatorAnswer % i == 0) {
}

}
}
}
}```
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 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.pack();
functions.setVisible(true);

@Override
public void actionPerformed(ActionEvent arg0) {
}
});

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 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. ## Re: JButtons/ActionListener

Originally Posted by philip1597
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. Member
Join Date
Aug 2012
Location
New York
Posts
13
Rep Power
0

## Re: JButtons/ActionListener

Originally Posted by Junky
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.