Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help needed with calculator code

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help needed with calculator code

    so guys here's my code for a simple calculator....please help me with ways on how to improve it....i.e any scientific features plus the layout etc
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
     
    public class MyCal {
    JFrame frame=new JFrame();
    JPanel panel = new JPanel();
    JTextField txt1= new JTextField();
    JTextField txt2= new JTextField();
    JTextField txt3= new JTextField();
    JButton btnadd = new JButton("+");
    JButton btnmul = new JButton("x");
    JButton btnsub = new JButton("-");
    JButton btndiv = new JButton("/");
    JLabel lbl1 = new JLabel("number1");
    JLabel lbl2 = new JLabel("number2");
    JLabel lbl3 = new JLabel("answer");
     
     
     
    public void AddTxtField(){
    txt1.setBounds(100, 50, 150, 20);
    txt2.setBounds(100, 70, 150, 20);
    txt3.setBounds(100, 90, 150, 20);
    panel.add(txt1);
    panel.add(txt2);
    panel.add(txt3);
     
     
    lbl1.setBounds(10, 50, 150, 20);
    panel.add(lbl1);
     
    lbl2.setBounds(10, 70, 150, 20);
    panel.add(lbl2);
     
    lbl3.setBounds(10, 90, 150, 20);
    panel.add(lbl3);
     
     
    }
    public void Addbottons(){
    btnadd.setBounds(100, 115, 100, 20);
    btnadd.addActionListener(new addhandaler());
    panel.add(btnadd);
    }
     
     
    public void Multiplybutton(){
    btnmul.setBounds(100,140, 100, 20);
    btnmul.addActionListener(new mulhandaler());
    panel.add(btnmul);
    }
     
     
    public void Divisionbutton(){
    btndiv.setBounds(210, 115, 100, 20);
    btndiv.addActionListener(new divhandaler());
    panel.add(btndiv);
    }
     
    public void Subtractionbutton(){
    btnsub.setBounds(210, 140, 100, 20);
    btnsub.addActionListener(new subhandaler());
    panel.add(btnsub);
    }
     
     
    class addhandaler implements ActionListener{
    public void actionPerformed(ActionEvent e) {
    int num1= Integer.parseInt(txt1.getText());
    int num2= Integer.parseInt(txt2.getText());
    int numtotal= num1+num2;
    txt3.setText(String.valueOf(numtotal));
     
    }
     
    }
     
    class subhandaler implements ActionListener{
    public void actionPerformed(ActionEvent e) {
    int num1= Integer.parseInt(txt1.getText());
    int num2= Integer.parseInt(txt2.getText());
    int numtotal= num1-num2;
    txt3.setText(String.valueOf(numtotal));
     
    }
     
    }
     
    class mulhandaler implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
    int num1= Integer.parseInt(txt1.getText());
    int num2= Integer.parseInt(txt2.getText());
    int numtotal= num1*num2;
    txt3.setText(String.valueOf(numtotal));
     
    }
     
    }
     
    class divhandaler implements ActionListener{
    public void actionPerformed(ActionEvent e) {
    int num1= Integer.parseInt(txt1.getText());
    int num2= Integer.parseInt(txt2.getText());
    int numtotal= num1/num2;
    txt3.setText(String.valueOf(numtotal));
     
    }
     
    }
     
    public void CreateFrom(){
    frame.setTitle("Cal");
    frame.setSize (400, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    panel.setLayout(null);
     
    }public MyCal( ){
    CreateFrom ();
    AddTxtField();
    Addbottons();
    Divisionbutton();
    Multiplybutton();
    Subtractionbutton();
    frame.add(panel);
    frame.setVisible(true);
     
     
    }
    public static void main(String[] args) {
    new MyCal();
    }
     
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help needed with calculator code

    That's not really a question we can answer (you didn't really ask a question). What do you need help with? Where are you stuck?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Help needed with calculator code

    SIN, COS, TAN and inverse functions, convert to radians, power function, root function, factorial, logarithms...
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide

Similar Threads

  1. Calculator
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 22nd, 2010, 09:00 AM
  2. making my code a little better, if needed.
    By vendetta in forum Object Oriented Programming
    Replies: 4
    Last Post: February 11th, 2010, 03:40 AM
  3. code needed for the following problem
    By romilc in forum Java Theory & Questions
    Replies: 1
    Last Post: October 11th, 2009, 10:05 AM
  4. Urgent code needed
    By subhvi in forum AWT / Java Swing
    Replies: 4
    Last Post: August 27th, 2009, 12:55 AM
  5. HELP "code needed"
    By Dave in forum File I/O & Other I/O Streams
    Replies: 14
    Last Post: August 26th, 2009, 11:06 AM