Results 1 to 5 of 5
  1. #1
    jimbob is offline Member
    Join Date
    Jul 2009
    Posts
    3
    Rep Power
    0

    Post Simple Calculator Display Problem :(

    Hey guys

    Im new to this.

    Im have written a half finished calculator applet. When I try to compile and
    run the applet i get a little message in the bottom left hand of the window
    saying "Start: Applet not initialized".

    I am using BlueJ.

    import java.awt.*;
    import javax.swing.*;
    import java.applet.Applet;
    import java.awt.event.*;
    import java.applet.*;




    /**
    * Calculator
    *
    * @author (Me!)
    *
    */

    public class Clac extends Applet implements ActionListener

    {
    JButton zero;
    JButton one;
    JButton two;
    JButton three;
    JButton four;
    JButton five;
    JButton six;
    JButton seven;
    JButton eight;
    JButton nine;
    JButton plus;
    JButton take;
    JButton divide;
    JButton multiply;
    JButton equals;
    TextField Field;
    int number;
    int number1;



    public void init()
    {
    setBackground(new Color(0,100,255));
    setForeground(Color.black);

    zero=new JButton("0");
    one =new JButton("1");
    two=new JButton("2");
    three=new JButton("3");
    four=new JButton("4");
    five=new JButton("5");
    six=new JButton("6");
    seven=new JButton("7");
    eight=new JButton("8");
    nine=new JButton("9");
    plus=new JButton("+");
    take=new JButton("-");
    divide=new JButton("/");
    equals=new JButton("=");


    number = 0;
    number1=0;

    zero.addActionListener(this);
    one.addActionListener(this);
    two.addActionListener(this);
    three.addActionListener(this);
    four.addActionListener(this);
    five.addActionListener(this);
    six.addActionListener(this);
    seven.addActionListener(this);
    eight.addActionListener(this);
    nine.addActionListener(this);


    // havent done multiply, add, divide, subtract yet.

    setLayout(new GridLayout(3,5));
    add(seven);
    add(eight);
    add(nine);
    add(divide);
    add(four);
    add(five);
    add(six);
    add(one);
    add(two);
    add(three);
    add(plus);
    add(zero);
    add(multiply);
    add(equals);
    add(Field);





    }

    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==zero)
    {

    number = number * 10;
    Field.setText(""+number);
    }

    if(e.getSource()==one)
    {

    number = number * 10 + 1;
    Field.setText(""+number);
    }


    if(e.getSource()==two)

    {

    number = number * 10 + 2;
    Field.setText(""+number);

    }
    if(e.getSource()==three)
    {
    number = number * 10 + 3;
    Field.setText(""+number);

    }
    if(e.getSource()==four)
    {

    number = number * 10 + 4;
    Field.setText(""+number);

    }
    if(e.getSource()==five)
    {

    number = number * 10 + 5;
    Field.setText(""+number);

    {
    if(e.getSource()==six)
    {

    number = number * 10 + 6;
    Field.setText(""+number);


    }
    if(e.getSource()==seven)
    {

    number = number * 10 + 7;
    Field.setText(""+number);

    }
    if(e.getSource()==eight)
    {

    number = number * 10 + 8;
    Field.setText(""+number);

    }
    if(e.getSource()==nine)
    {

    number = number * 10 + 9;
    Field.setText(""+number);

    }


    }
    }
    }
    }


    thanks for your help ;)

    as i said, yeah im new to this and also this applet is not finished! it should only display entered numbers (Hopefully!)

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    800
    Rep Power
    6

    Default

    Hi,
    You have started with awt and ended with swings ;)

    U have not added the component properly.That is why lot of errors.

    Please gothru the Sun tutorial and other java tutorials before doing something.I have corrected the code without exception.Proceed further and complete the code.Use loops.why this many if conditions?.U can simplify the code.Please gothru the correct code without exception.

    import java.awt.*;
    import javax.swing.*;
    import java.applet.Applet;
    import java.awt.event.*;
    import java.applet.*;
    /*
    <applet code="Clac.class" width=200 height=200>
    </applet>
    */

    /**
    * Calculator
    *
    * @author (Me!)
    *
    */

    Java Code:
    public class Clac extends JApplet implements ActionListener
    {
    	JButton zero;
    	JButton one;
    	JButton two;
    	JButton three;
    	JButton four;
    	JButton five;
    	JButton six;
    	JButton seven;
    	JButton eight;
    	JButton nine;
    	JButton plus;
    	JButton take;
    	JButton divide;
    	JButton multiply;
    	JButton equals;
    	TextField Field;
    	int number;
    	int number1;
    
    
    
    	public void init()
    	{
    		setBackground(new Color(0,100,255));
    		setForeground(Color.black);
    
    		zero=new JButton("0");
    		one =new JButton("1");
    		two=new JButton("2");
    		three=new JButton("3");
    		four=new JButton("4");
    		five=new JButton("5");
    		six=new JButton("6");
    		seven=new JButton("7");
    		eight=new JButton("8");
    		nine=new JButton("9");
    		plus=new JButton("+");
    		take=new JButton("-");
    		divide=new JButton("/");
    		equals=new JButton("=");
    
    
    		number = 0;
    		number1=0;
    
    		// havent done multiply, add, divide, subtract yet.
    		Container cp = getContentPane();
    		cp.setLayout(new GridLayout(3,5));
    		cp.add(seven);
    		cp.add(eight);
    		cp.add(nine);
    		cp.add(divide);
    		cp.add(four);
    		zero.addActionListener(this);
    		one.addActionListener(this);
    		two.addActionListener(this);
    		three.addActionListener(this);
    		four.addActionListener(this);
    		five.addActionListener(this);
    		six.addActionListener(this);
    		seven.addActionListener(this);
    		eight.addActionListener(this);
    		nine.addActionListener(this);
    	}//init
    
    	public void actionPerformed(ActionEvent e)
    	{
    		if(e.getSource()==zero)
    		{
    
    			number = number * 10;
    			Field.setText(""+number);
    		}
    		if(e.getSource()==two)
    		{
    
    			number = number * 10 + 2;
    			Field.setText(""+number);
    		}
    		if(e.getSource()==three)
    		{
    			number = number * 10 + 3;
    			Field.setText(""+number);
    		}
    		if(e.getSource()==four)
    		{
    			number = number * 10 + 4;
    			Field.setText(""+number);
    
    		}
    		if(e.getSource()==five)
    		{
    
    			number = number * 10 + 5;
    			Field.setText(""+number);
    
    		}
    		if(e.getSource()==six)
    		{
    
    			number = number * 10 + 6;
    			Field.setText(""+number);
    
    		}
    		if(e.getSource()==seven)
    		{
    
    			number = number * 10 + 7;
    			Field.setText(""+number);
    
    		}
    		if(e.getSource()==eight)
    		{
    
    			number = number * 10 + 8;
    			Field.setText(""+number);
    
    		}
    		if(e.getSource()==nine)
    		{
    			number = number * 10 + 9;
    			Field.setText(""+number);
    		}
    
    		
      }//actionPerformed
    }
    Ramya:cool:

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

    Default

    Here's a good thread to preach the gospel of not using "this" as an ActionListener. i.e.,:
    Java Code:
    xxxx.addActionListener(this);
    The number buttons can all share an ActionListener, and the operations buttons can share one, but they all shouldn't share the same single actionlistener. Create two inner classes that implement the ActionListener interface so that you can separate out that which should remain separate.

  4. #4
    jimbob is offline Member
    Join Date
    Jul 2009
    Posts
    3
    Rep Power
    0

    Default

    thanks for helping me with my code ;) it got it working.



    Now all i really need to do is do a gridbag layout and make the calculator actually calculate :)

    cheers.

  5. #5
    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 jimbob View Post
    Now all i really need to do is do a gridbag layout
    I would avoid that unless I were held over a vat of boiling oil. Much better here would be to nest JPanels each using their own more simple layout such as GridLayout and BorderLayout.

    ... and make the calculator actually calculate :)
    Yep, I heartily support this.

Similar Threads

  1. [SOLVED] Simple Conversion Calculator
    By dbashby in forum New To Java
    Replies: 6
    Last Post: 03-20-2009, 01:06 AM
  2. [SOLVED] Simple Calculator Applet Weird Error
    By sari in forum New To Java
    Replies: 5
    Last Post: 01-28-2009, 04:57 AM
  3. Help Needed, simple jframe display
    By typh0n in forum New To Java
    Replies: 7
    Last Post: 10-03-2008, 01:06 AM
  4. Calculator Problem. Thanks for helping! ^^
    By clark_sandy in forum New To Java
    Replies: 3
    Last Post: 07-06-2008, 04:01 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
  •