Results 1 to 5 of 5

Thread: Calculator

  1. #1
    water is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Unhappy Calculator

    I have been making a calculator.Could you give me any codes of that,please.:)
    Thanks so much!

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

    Default

    If you have a specific question regarding your code, we'd be more than happy to help you. If on the other hand you're looking for a "give me some code" site, then this isn't it.

    Much luck.

  3. #3
    water is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Default Calculator

    i have just designed GUI,but i have got a problem in solving events.
    XML Code:
    import java.text.DecimalFormat;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     class Caculator implements ActionListener{
     		double currentTotal = 0.0;
      		DecimalFormat df = new DecimalFormat("0.00");
      		boolean totalDisplayed = true;
    		JFrame f;
        	JTextField t1;
        	JLabel l1;
        	JPanel p2 ;
    		JButton b1;
        	JButton b2;
        	JButton b3;
        	JButton b4;
        	JButton b5;
        	JButton b6;
        	JButton b7;
        	JButton b8;
        	JButton b9;
        	JButton b10;
        	JButton b11;
        	JButton b12;
        	JButton b13;
        	JButton b14;
        	JButton b15;
        	JButton b16;
        	JButton b17;
        	JButton b18;
        	JButton b19;
        	JButton b20;
        	
        public Caculator() {
    		 f   = new JFrame("Caculator");
        	 l1  = new JLabel("|--------CASIO--------|");
        	 p2  = new JPanel();
        	 t1 = new JTextField("0.00",25);
        	 b1 = new JButton("MODE");
        	 b2 = new JButton("^");
        	 b3 = new JButton("AC");
        	 b4 = new JButton("DEL");
        	 b3.setBackground(Color.pink);
        	 b4.setBackground(Color.pink);
        	 b5 = new JButton("1");
        	 b6 = new JButton("2");
        	 b7 = new JButton("3");
        	 b8 = new JButton("+");
        	 b9 = new JButton("4");
        	 b10 = new JButton("5");
        	 b11 = new JButton("6");
        	 b12 = new JButton("-");
        	 b13 = new JButton("7");
        	 b14 = new JButton("8");
        	 b15 = new JButton("9");
        	 b16 = new JButton("x");
        	 b17 = new JButton("P");
        	 b18 = new JButton(".");
        	 b19 = new JButton("/");
        	 b20 = new JButton("=");
        }
         public void fFrame(){
         
        	p2.setLayout(new GridLayout(5,4,4,4));
        	p2.add(b1);
        	p2.add(b2);
        	p2.add(b3);
        	p2.add(b4);
        	p2.add(b5);
        	p2.add(b6);
        	p2.add(b7);
        	p2.add(b8);
        	p2.add(b9);
        	p2.add(b10);
        	p2.add(b11);
        	p2.add(b12);
        	p2.add(b13);
        	p2.add(b14);
        	p2.add(b15);
        	p2.add(b16);
        	p2.add(b17);
        	p2.add(b18);
        	p2.add(b19);
        	p2.add(b20);
        	f.setLayout(new  FlowLayout());	
        	f.add(t1);
        	f.add(l1);
        	f.add(p2);
        	t1.setBackground(Color.YELLOW);
        	f.setBackground(Color.YELLOW);
        	f.setResizable(false);
        	f.setBounds(300,300,300,240);
        	f.setVisible(true);
        	/*
        	 b1.setActionCommand("b1");
        	 b2.setActionCommand("b2");
        	 b3.setActionCommand("b3");
        	 b4.setActionCommand("b4");
        	 b5.setActionCommand("b5");
        	 b6.setActionCommand("b6");
        	 b7.setActionCommand("b7");
        	 b8.setActionCommand("b8");
        	 b9.setActionCommand("b9");
        	 b10.setActionCommand("b10");
        	 b11.setActionCommand("b11");
        	 b12.setActionCommand("b12");
        	 b13.setActionCommand("b13");
        	 b14.setActionCommand("b14");
        	 b15.setActionCommand("b15");
        	 b16.setActionCommand("b16");
        	 b17.setActionCommand("b17");
        	 b18.setActionCommand("b18");
        	 b19.setActionCommand("b19");
        	 b20.setActionCommand("b20");
        	 */
        	
        	 b1.addActionListener(this);
        	 b2.addActionListener(this);
        	 b3.addActionListener(this);
        	 b4.addActionListener(this);
        	 b5.addActionListener(this);
        	 b6.addActionListener(this);
        	 b7.addActionListener(this);
        	 b8.addActionListener(this);
        	 b9.addActionListener(this);
        	 b10.addActionListener(this);
        	 b11.addActionListener(this);
        	 b12.addActionListener(this);
        	 b13.addActionListener(this);
        	 b14.addActionListener(this);
        	 b15.addActionListener(this);
        	 b16.addActionListener(this);
        	 b17.addActionListener(this);
        	 b18.addActionListener(this);
        	 b19.addActionListener(this);
        	 b20.addActionListener(this);
        	
         }
        	 public void actionPerformed(ActionEvent e){
        	 
        	 //[COLOR="Red"][SIZE="5"] i can't do it.[/SIZE][/COLOR]
        	 }
     
        public static void main (String[] args) {
    	  Caculator a= new Caculator();
    	  a.fFrame();
    	  
    }
        
    }

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

    Default

    Quote Originally Posted by water View Post
    i have just designed GUI,but i have got a problem in solving events.
    Hello again. What problems are you having? I'm not trying to nit pic here, but rather I'm trying to make it easier for others to help you since the more specific your question, the more specific and helpful the answer. Much luck and welcome to the forum.

    Edit: also some other advice:
    1) If you used arrays here, you could shorten this code 3-fold if not more. This would also make it easier to debug and enhance the code since any changes desired in the behavior of say the number buttons could be done in one small method, rather than 10 separate methods. This lessons the chances of making an error.
    2) I strongly suggest that you not use "this" as your ActoinListener but rather use a few private inner classes instead. What this would do would be to group the actions that should stay together (such as the number button actions) yet separate the actions that should be kept apart (for instance a number button shouldn't share the same ActionListener as the "+" operations button since they do vastly different things. This separation will make it easier to figure out what goes in the action listeners -- starting with the easiest -- the numbers.

    Much luck!
    Last edited by Fubarable; 09-23-2009 at 05:49 AM.

  5. #5
    water is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Default

    thank for your advice.i'll contact to you if i have an other problem.hope you help me again.

Similar Threads

  1. help with calculator
    By kalibballer in forum New To Java
    Replies: 8
    Last Post: 04-01-2009, 12:57 PM
  2. mortgage calculator help
    By kalibballer in forum New To Java
    Replies: 1
    Last Post: 03-26-2009, 11:35 AM
  3. Calculator program
    By kevzspeare in forum New To Java
    Replies: 6
    Last Post: 03-18-2009, 01:43 PM
  4. Calculator help.
    By madkidd02 in forum New To Java
    Replies: 2
    Last Post: 10-25-2008, 07:42 AM
  5. Swing Calculator
    By nemo in forum AWT / Swing
    Replies: 1
    Last Post: 05-28-2007, 11:07 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
  •