Results 1 to 6 of 6
  1. #1
    youngdemetre is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default Calculator Functions

    I can get the numbers and *,+,- signs to appear, but I can't get the numbers to equal anything or solve out, and the cancel button doesn't work either. I've been doing Java for two days maybe my code is messed up.




    import java.awt.FlowLayout;
    import javax.swing.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;


    public class conversion_ui implements ActionListener {


    JFrame frame = new JFrame("Conversion");
    JPanel panel = new JPanel(new FlowLayout());

    JTextArea text = new JTextArea(1,20);
    JButton but1 = new JButton("1");
    JButton but2 = new JButton("2");
    JButton but3 = new JButton("3");
    JButton but4 = new JButton("4");
    JButton but5 = new JButton("5");
    JButton but6 = new JButton("6");
    JButton but7 = new JButton("7");
    JButton but8 = new JButton("8");
    JButton but9 = new JButton("9");
    JButton but0 = new JButton("0");

    JButton butadd = new JButton("+");
    JButton butsub = new JButton("-");
    JButton butmulti = new JButton("*");
    JButton buteq = new JButton("=");

    JButton butclear = new JButton("C");

    Double number1,number2,result;
    int addc=0,subc=0,multic=0;
    private JButton source;

    public void ui()
    {
    frame.setVisible(true);
    frame.setSize(250,200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    frame.add(panel);

    panel.add(text);

    panel.add(but1);
    panel.add(but2);
    panel.add(but3);
    panel.add(but4);
    panel.add(but5);
    panel.add(but6);
    panel.add(but7);
    panel.add(but8);
    panel.add(but9);
    panel.add(but0);
    panel.add(butadd);
    panel.add(butsub);
    panel.add(butmulti);
    panel.add(buteq);
    panel.add(butclear);

    but1.addActionListener(this);
    but2.addActionListener(this);
    but3.addActionListener(this);
    but4.addActionListener(this);
    but5.addActionListener(this);
    but6.addActionListener(this);
    but7.addActionListener(this);
    but8.addActionListener(this);
    but9.addActionListener(this);
    but0.addActionListener(this);
    butadd.addActionListener(this);
    butsub.addActionListener(this);
    butmulti.addActionListener(this);
    buteq.addActionListener(this);
    butclear.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {


    String btnText = e.getActionCommand();
    if ("1234567890+-*".contains(btnText)) {
    text.setText(text.getText() + btnText);
    }
    if(source==butclear)
    {
    number1=0.0;
    number2=0.0;
    text.getText();

    }

    if(source==butadd)
    {
    number1=number_reader();
    text.setText("");
    addc=1;
    subc=0;
    multic=0;

    } if(source==butsub)
    {
    number1=number_reader();
    text.setText("");
    addc=0;
    subc=1;
    multic=0;

    } if(source==butmulti)
    {
    number1=number_reader();
    text.setText("");
    addc=0;
    subc=0;
    multic=1;

    }
    if(source==buteq)
    {
    number2=number_reader();
    if(addc>0)
    {
    result=number1+number2;
    text.setText(Double.toString(result));

    }
    if(subc>0)
    {
    result=number1-number2;
    text.setText(Double.toString(result));
    }
    if(multic>0)
    {
    result=number1*number2;
    text.setText(Double.toString(result));

    }
    }
    }



    public double number_reader()
    {

    Double num1;
    String s;
    s=text.getText();
    num1=Double.valueOf(s);

    return num1;

    }
    }

  2. #2
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    - Please use code-tags (wrap your code in [co de][/code]), and proper indentation.
    - The convention is that Class-names begin with a capital.
    - Your class is nor an applet nor has it a main-function so it doesn't run as posted.
    - If possible post a SSCCE (small self contained compiling example).
    (It is not very polite to make someone reconstruct your intensions.)

    I added a class with a main():
    - Conversion_UI has no constructor, so nothing happens on instantiating.
    I added a call to ui() in my new main-class:
    Rejoice, something happens. A gui shows up with appropriate buttons for a calculator. Not bad for a start!
    (The layout appears to be rather primitive: stretching the panel aligns the components: a typical flowlayout. Something to fix later.)

    Local JButton variable "source" in the start of "actionPerformed()" is supposed to magically contain a reference to the pressed button: I added
    source=(JButton)e.getSource();
    to make this magic come true.
    Now the buttons work (check this by some println-statements), but the code produces exceptions (Numberformat and Conversion to begin with),
    but the code is reached now.

    Good luck repairing. Please post a complete SSCCE in your next post.
    Last edited by Jodokus; 05-01-2011 at 12:16 AM.

  3. #3
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    After answering your question, I saw that there is another earlier thread about the same subject, WITH the code I had to reconstruct!
    In your next posts, please DO NOT start a new thread, but continue the same thread while your project is developing.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Jodokus View Post
    After answering your question, I saw that there is another earlier thread about the same subject, WITH the code I had to reconstruct!
    In your next posts, please DO NOT start a new thread, but continue the same thread while your project is developing.
    Thanks for the warning. I second this advice and will delete duplicate threads if they occur in the future.

  5. #5
    youngdemetre is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Thanks, so if I'm using a Gui I need to post the _main part as well?? I never thought that the Calculator Fucntions and my previous post was the same, but it makes sense with the project. I'll start working on understanding how to post information more correctlly

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If you make a post about a calculator, ask any and all questions you have in regards to that particular project, save new threads for different projects and assignments.

Similar Threads

  1. Functions and Classes
    By DoubleDee in forum New To Java
    Replies: 12
    Last Post: 09-13-2010, 02:15 AM
  2. static functions
    By jatinrai199 in forum New To Java
    Replies: 6
    Last Post: 08-31-2010, 06:44 PM
  3. JSP Functions
    By sysout in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-07-2009, 05:10 AM
  4. 2 functions
    By baze7 in forum New To Java
    Replies: 3
    Last Post: 08-14-2009, 05:41 AM
  5. Functions in jsp
    By samson in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 03-25-2009, 11:04 PM

Posting Permissions

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