# Help with JFrame calculator

• 04-25-2013, 10:32 PM
swedishfished
Help with JFrame calculator
Here's the code:
0 is for division
1 for multiplication
3 for subtraction
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
import javax.swing.JComboBox;

public class aosd extends JFrame {

String[] operations = {
"0","1","2","3" };
private JButton submit;
private JTextField fnum;
private JTextField snum;
private JComboBox op;
int fnum1;
int snum1;
int cow;
public aosd(){
/*JFrame frame = new JFrame();
JPanel panel = new JPanel(new GridLayout(1,4)); // this means the panel will have 1 row, and 4 columns, so your 4 buttons will fit into it nicely
frame.add( panel , BorderLayout.NORTH); // puts it at the top of the frame*/
fnum = new JTextField(10);
snum = new JTextField(10);
op = new JComboBox(operations);
submit = new JButton("Calculate");
setLayout(new FlowLayout());
submit.setSize(10,10);
handler handObj = new handler();

}
private class handler implements ActionListener{
public void actionPerformed(ActionEvent event){
int sum;
if(event.getSource()==submit){
getOp();
JOptionPane.showMessageDialog(null, cow);
}
}
}
public void getOp(){
Object operation1 = op.getSelectedItem();
String operation2 = operation1.toString();
for(int i=0; i<4; i++){
if(operation2==operations[i]){
cow = i;
doOp(cow);
}

}
}
public void doOp(int cow1){
String text = fnum.getText();
fnum1 = Integer.parseInt(text);
String text2 = snum.getText();
snum1 = Integer.parseInt(text2);
int fnum11;
switch(cow1){
case 0:
fnum11 = fnum1/snum1;
JOptionPane.showMessageDialog(null, fnum11);

case 1:
fnum11 = fnum1*snum1;
JOptionPane.showMessageDialog(null, fnum11);

case 2:
fnum11 = fnum1+snum1;
JOptionPane.showMessageDialog(null, fnum11);
case 3:
fnum11 = fnum1-snum1;
JOptionPane.showMessageDialog(null, fnum11);

}
}

}
• 04-25-2013, 10:36 PM
SurfMan
Re: Help with JFrame calculator
Thank you for sharing. Did you have an actual question?
• 04-25-2013, 11:31 PM
swedishfished
Re: Help with JFrame calculator
yes--- it doesn't work: it displays all of the operations
• 04-26-2013, 07:40 AM
SurfMan
Re: Help with JFrame calculator
"It doesn't work" isn't a question.
• 04-26-2013, 01:32 PM
swedishfished
Re: Help with JFrame calculator
Ok-- why does it display, when I run it, all of the operations. ie: instead of x*y, it does x*y, x/y, x+y and x-y
• 04-26-2013, 01:40 PM
Tolls
Re: Help with JFrame calculator
When posting code please wrap it in [code] tags [/code] so it retains its formatting.
Unformatted code is hard to follow.

Because of the lack of formatting this is a bit of a guess, but it looks like you have no 'break' statements in your 'switch' clause. This means each case after the selected one is also run.
• 04-27-2013, 03:35 PM
swedishfished
Re: Help with JFrame calculator
no, it still doesn't work :( and why would it do the other cases if they weren't true? ie: cow =/= 3
• 04-27-2013, 04:04 PM
JosAH
Re: Help with JFrame calculator
Quote:

Originally Posted by swedishfished
no, it still doesn't work :( and why would it do the other cases if they weren't true? ie: cow =/= 3

Because case-clauses 'fall through'; try the subtraction operator and see for yourself: it only does the subtraction because there is nothing below that case-clause; now try a division and see your program running through all the other operators ...

kind regards,

Jos