Results 1 to 3 of 3
  1. #1
    harry.only1 is offline Member
    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Default Mod: Help with calculator program

    i made a calculator prog in java but want to separate clases e.g main class, listener class, interface class but the buttons are not show any thing when clicked in the textfield... heres the code .. plz help


    /// user interface class
    import java.awt.*;

    import javax.swing.*;

    import java.awt.event.*;
    import java.io.*;


    public class UI extends JFrame {




    public UI(){
    UITest(null);
    }
    private void UITest(UI UI)
    {
    JFrame f=new JFrame("Calculator");
    Container c=f.getContentPane();
    c.setLayout(new BorderLayout());

    JLabel l=new JLabel("My Calculator");

    JPanel p=new JPanel();
    p.setLayout(new GridLayout(4,4));

    JButton b1=new JButton("1");
    b1.addActionListener(new Listener(UI));


    JButton b2=new JButton("2");
    b2.addActionListener(new Listener(UI));

    JButton b3=new JButton("3");
    JButton b4=new JButton("4");
    JButton b5=new JButton("5");
    JButton b6=new JButton("6");
    JButton b7=new JButton("7");
    JButton b8=new JButton("8");
    JButton b9=new JButton("9");
    JButton b0=new JButton("0");
    JButton bplus=new JButton("+");

    JButton beql=new JButton("=");

    JTextField tf=new JTextField();


    p.add(tf);
    p.add(b1);
    p.add(b2);
    p.add(b3);
    p.add(b4);
    p.add(b5);
    p.add(b6);
    p.add(b7);
    p.add(b8);
    p.add(b9);
    p.add(b0);
    p.add(bplus);
    p.add(beql);




    c.add(tf,BorderLayout.NORTH);
    c.add(p,BorderLayout.CENTER);
    c.add(l,BorderLayout.SOUTH);

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    f.setSize(200,200);
    f.setVisible(true);
    f.setLocationRelativeTo(null);
    }






    }

    //Listener class
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JTextField;

    public class Listener implements ActionListener {

    UI ui;
    String num1;

    JTextField tf=new JTextField();


    Listener(UI ui)
    {
    this.ui=ui;



    }

    public void actionPerformed(ActionEvent e)
    {
    if(e.getActionCommand().equals("1"))
    System.out.println("hello");

    else if(e.getActionCommand().equals("2"))
    {
    num1=num1+"2";
    tf.setText(num1);
    }

    }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Mod: Help with calculator program

    Please don't hijack someone else's thread.
    And also please use [code] tags [/code] when posting code so it retains its formatting.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    sevoxx is offline Oracle Java Developer
    Join Date
    Apr 2013
    Location
    In a PineApple Under the Sea
    Posts
    20
    Rep Power
    0

    Default Re: Mod: Help with calculator program

    Quote Originally Posted by harry.only1 View Post
    i made a calculator prog in java but want to separate clases e.g main class, listener class, interface class but the buttons are not show any thing when clicked in the textfield... heres the code .. plz help
    1) You have most definitely not made a calculator. You've made some UI that you can't even render.
    2) Use CODE tags!
    3) You've imported packages in the middle of your class file? Really?
    4) Please RTFM/RTFD before attempting copy someones code.

    However, because I'm such a sweetheart, I've sorted this out for you -- I have only cleaned up and added the PSVM, finishing the rest of the code and actually putting that ActionListener to some use is up to you.

    UI.class
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class UI extends JFrame
    {
        public UI()
        {
            UITest(null);
        }
    
        private void UITest(UI UI)
        {
            JFrame f = new JFrame("Calculator");
            Container c = f.getContentPane();
            c.setLayout(new BorderLayout());
    
            JLabel l = new JLabel("My Calculator");
    
            JPanel p = new JPanel();
            p.setLayout(new GridLayout(4, 4));
    
            JButton b1 = new JButton("1");
            b1.addActionListener(new Listener(UI));
    
            JButton b2 = new JButton("2");
            b2.addActionListener(new Listener(UI));
    
            JButton b3 = new JButton("3");
            JButton b4 = new JButton("4");
            JButton b5 = new JButton("5");
            JButton b6 = new JButton("6");
            JButton b7 = new JButton("7");
            JButton b8 = new JButton("8");
            JButton b9 = new JButton("9");
            JButton b0 = new JButton("0");
            JButton bplus = new JButton("+");
            JButton beql = new JButton("=");
            
            JTextField tf = new JTextField();
    
            p.add(tf);
            p.add(b1);
            p.add(b2);
            p.add(b3);
            p.add(b4);
            p.add(b5);
            p.add(b6);
            p.add(b7);
            p.add(b8);
            p.add(b9);
            p.add(b0);
            p.add(bplus);
            p.add(beql);
    
            c.add(tf, BorderLayout.NORTH);
            c.add(p, BorderLayout.CENTER);
            c.add(l, BorderLayout.SOUTH);
    
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            f.setSize(200, 200);
            f.setVisible(true);
            f.setLocationRelativeTo(null);
        }
        
        public static void main(String[] args)
        {
            new UI();
        }
    }
    Listener.class
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JTextField;
    
    public class Listener implements ActionListener
    {
        UI ui;
        
        String num1;
        
        JTextField tf = new JTextField();
    
        Listener(UI ui)
        {
            this.ui = ui;
        }
    
        public void actionPerformed(ActionEvent e)
        {
            if (e.getActionCommand().equals("1"))
            {
                System.out.println("hello");
            }
            else if (e.getActionCommand().equals("2"))
            {
                num1 = num1 + "2";
                tf.setText(num1);
            }
    
        }
    }

Similar Threads

  1. Error in calculator program
    By Programmer3 in forum New To Java
    Replies: 3
    Last Post: 10-02-2012, 12:49 PM
  2. Calculator Program
    By CODinacup in forum New To Java
    Replies: 4
    Last Post: 09-27-2012, 11:35 PM
  3. Problem with if statement in a calculator program
    By peterhabe in forum New To Java
    Replies: 2
    Last Post: 08-26-2011, 05:07 PM
  4. Calculator program
    By kevzspeare in forum New To Java
    Replies: 6
    Last Post: 03-18-2009, 01:43 PM
  5. unreachable statement - Java calculator program
    By V2001Gordon in forum New To Java
    Replies: 3
    Last Post: 12-13-2008, 12:57 AM

Posting Permissions

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