Thread: Mini project

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Smile Mini project


    this is mini project for my subject java programmer...plz help me i already create the interfece for my calculator but i am stuck at public void actionPerformed to create the action..

    Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class calkulator extends JApplet implements ActionListener{
    	private JTextField teks;
    	private JPanel p;
    	private JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
    	private JButton b10,b11,b12,b13,b14,b15;
    	private int total= 0;
    
    	public void init(){
    		Container pane = getContentPane();
    		pane.setBackground(Color.white);
    		pane.setLayout(new BorderLayout());
    
    		teks = new JTextField("0");
    		pane.add(teks, "North");
    
    		p = new JPanel();
    		p.setLayout(new GridLayout(4,4));
    
    		b7 = new JButton("7"); b7.addActionListener(this); p.add(b7);
    		b8 = new JButton("8"); b8.addActionListener(this);p.add(b8);
    		b9 = new JButton("9"); b9.addActionListener(this);p.add(b9);
    		b10 = new JButton("/"); b10.addActionListener(this);p.add(b10);
    		b4 = new JButton("4"); b4.addActionListener(this);p.add(b4);
    		b5 = new JButton("5"); b5.addActionListener(this);p.add(b5);
    		b6 = new JButton("6"); b6.addActionListener(this);p.add(b6);
    		b11 = new JButton("*"); b11.addActionListener(this);p.add(b11);
    		b1 = new JButton("1"); b1.addActionListener(this);p.add(b1);
    		b2 = new JButton("2"); b2.addActionListener(this);p.add(b2);
    		b3 = new JButton("3"); b3.addActionListener(this);p.add(b3);
    		b12 = new JButton("-"); b12.addActionListener(this);p.add(b12);
    		b0 = new JButton("0"); b0.addActionListener(this);p.add(b0);
    		b13 = new JButton("."); b13.addActionListener(this);p.add(b13);
    		b14 = new JButton("+"); b14.addActionListener(this);p.add(b14);
    		b15 = new JButton("="); b15.addActionListener(this);p.add(b15);
    
    		pane.add(p, "Center");
    
    	}
    
    	public void actionPerformed(ActionEvent e){
    				String s = ((JButton) e.getSource()).getText();
    	            teks.setText(s);
    				//if (s.equals("7"))
    					//total= 7;
    				//else if (s.equals("8"))
    					//total= 8;
    				//else if (s.equals("+"))
    				//	total =  ;
    
    				//teks.setText(Integer.toString(total));
    		}
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,715
    Rep Power
    348
    i am stuck
    Please explain what your problem is. If you are getting errors, copy the full text and paste it here.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by NormR
    Please explain what your problem is. If you are getting errors, copy the full text and paste it here.
    i newbie in java, the problem is i don't know how to create the action in (public void actionPerformed) the operation calculator like add,sub,div,multiple and equal...sory about my poor english
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,715
    Rep Power
    348
    how to create the action
    Do them one at a time.
    For example:
    What values are need to do the add? Get the values, do the computation, display the results.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by NormR
    Do them one at a time.
    For example:
    What values are need to do the add? Get the values, do the computation, display the results.
    here the action i create...the problem is i can run the coding without error i textpad but my operation add,sub,div and mul not function

    Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class Calculator extends JApplet implements ActionListener{
    	private JTextField teks;
    	private JPanel p;
    	private JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
    	private JButton b10,b11,b12,b13,b14,b15,b16;
           // {
    int c,n;
    String s1,s2,s3,s4,s5;
    Frame f;
    //JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;
    //Panel p;
    TextField tf1;
    GridLayout gl;
    //Calkulator()
    {
    	//private int jumlah = 0;
    
    	//public void init(){
    		Container pane = getContentPane();
    		pane.setBackground(Color.white);
    		pane.setLayout(new BorderLayout());
    
    		teks = new JTextField("0");
    		pane.add(teks, "North");
    
    		p = new JPanel();
    		p.setLayout(new GridLayout(4,4));
    
    		b7 = new JButton("7"); b7.addActionListener(this); p.add(b7);
    		b8 = new JButton("8"); b8.addActionListener(this);p.add(b8);
    		b9 = new JButton("9"); b9.addActionListener(this);p.add(b9);
    		b10 = new JButton("/"); b10.addActionListener(this);p.add(b10);
    		b4 = new JButton("4"); b4.addActionListener(this);p.add(b4);
    		b5 = new JButton("5"); b5.addActionListener(this);p.add(b5);
    		b6 = new JButton("6"); b6.addActionListener(this);p.add(b6);
    		b11 = new JButton("*"); b11.addActionListener(this);p.add(b11);
    		b1 = new JButton("1"); b1.addActionListener(this);p.add(b1);
    		b2 = new JButton("2"); b2.addActionListener(this);p.add(b2);
    		b3 = new JButton("3"); b3.addActionListener(this);p.add(b3);
    		b12 = new JButton("-"); b12.addActionListener(this);p.add(b12);
    		b0 = new JButton("0"); b0.addActionListener(this);p.add(b0);
    		b13 = new JButton("."); b13.addActionListener(this);p.add(b13);
    		b14 = new JButton("+"); b14.addActionListener(this);p.add(b14);
    		b15 = new JButton("="); b15.addActionListener(this);p.add(b15);
    
    		pane.add(p, "Center");
    
    	}
    
    	public void actionPerformed(ActionEvent e){
    				String s = ((JButton) e.getSource()).getText();
    	            teks.setText(s);
    				if(e.getSource()==b1)
    {
    
    s3=tf1.getText();
    s4="0";
    s5=s3+s4;
    tf1.setText(s5);
    }
    if(e.getSource()==b2)
    {
    s3=tf1.getText();
    s4="1";
    s5=s3+s4;
    tf1.setText(s5);
    }
    if(e.getSource()==b3)
    {
    s3=tf1.getText();
    s4="2";
    s5=s3+s4;
    tf1.setText(s5);
    }
    if(e.getSource()==b4)
    {
    s3=tf1.getText();
    s4="3";
    s5=s3+s4;
    tf1.setText(s5);
    }
    if(e.getSource()==b5)
    {
    s3=tf1.getText();
    s4="4";
    s5=s3+s4;
    tf1.setText(s5);
    }
    if(e.getSource()==b6)
    {
    s3=tf1.getText();
    s4="5";
    s5=s3+s4;
    tf1.setText(s5);
    }
    if(e.getSource()==b7)
    {
    s3=tf1.getText();
    s4="6";
    s5=s3+s4;
    tf1.setText(s5);
    }
    if(e.getSource()==b8)
    {
    s3=tf1.getText();
    s4="7";
    s5=s3+s4;
    tf1.setText(s5);
    }
    if(e.getSource()==b9)
    {
    s3=tf1.getText();
    s4="8";
    s5=s3+s4;
    tf1.setText(s5);
    }
    if(e.getSource()==b10)
    {
    s3=tf1.getText();
    s4="9";
    s5=s3+s4;
    tf1.setText(s5);
    }
    if(e.getSource()==b11)
    {
    s1=tf1.getText();
    tf1.setText("");
    c=1;
    }
    if(e.getSource()==b12)
    {
    s1=tf1.getText();
    tf1.setText("");
    c=2;
    }
    if(e.getSource()==b13)
    {
    s1=tf1.getText();
    tf1.setText("");
    c=3;
    }
    if(e.getSource()==b14)
    {
    s1=tf1.getText();
    tf1.setText("");
    c=4;
    }
    if(e.getSource()==b15)
    {
    s2=tf1.getText();
    if(c==1)
    {
    n=Integer.parseInt(s1)+Integer.parseInt(s2);
    tf1.setText(String.valueOf(n));
    }
    else
    if(c==2)
    {
    n=Integer.parseInt(s1)-Integer.parseInt(s2);
    tf1.setText(String.valueOf(n));
    }
    else
    if(c==3)
    {
    n=Integer.parseInt(s1)*Integer.parseInt(s2);
    tf1.setText(String.valueOf(n));
    }
    else
    if(c==4)
    {
    n=Integer.parseInt(s1)/Integer.parseInt(s2);
    tf1.setText(String.valueOf(n));
    }
    }
    if(e.getSource()==b16)
    {
    tf1.setText("");
    }
    }
    
    }
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,715
    Rep Power
    348
    The code in the listener method has lost all its formatting. Please edit the post and fix the formatting of the code.

IMN logo majestic logo threadwatch logo seochat tools logo