Results 1 to 8 of 8
  1. #1
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    24
    Rep Power
    0

    Default Help with JFrame calculator

    Here's the code:
    0 is for division
    1 for multiplication
    2 for addition
    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);
    add(fnum);
    add(op);
    add(snum);
    add(submit);
    handler handObj = new handler();
    submit.addActionListener(handObj);

    }
    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);

    }
    }







    }

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    911
    Rep Power
    2

    Default Re: Help with JFrame calculator

    Thank you for sharing. Did you have an actual question?

  3. #3
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    24
    Rep Power
    0

    Default Re: Help with JFrame calculator

    yes--- it doesn't work: it displays all of the operations

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    911
    Rep Power
    2

    Default Re: Help with JFrame calculator

    "It doesn't work" isn't a question.

  5. #5
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    24
    Rep Power
    0

    Default 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

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    swedishfished is offline Member
    Join Date
    Sep 2012
    Posts
    24
    Rep Power
    0

    Default 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

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help with JFrame calculator

    Quote Originally Posted by swedishfished View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Click in JFrame Titlebar sets the JFrame Opaque
    By tim8w in forum AWT / Swing
    Replies: 1
    Last Post: 02-02-2013, 07:29 AM
  2. Replies: 4
    Last Post: 06-06-2012, 06:16 AM
  3. Replies: 3
    Last Post: 11-27-2011, 05:18 AM
  4. Replies: 3
    Last Post: 08-07-2010, 05:28 AM
  5. Replies: 5
    Last Post: 06-14-2010, 06:10 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
  •