Need help in java project

Asked By 0 points N/A Posted on -
qa-featured

I need a java code which must hava calculator look and should have perform operations like +,-,*,/. I t shoulld have GUI.

SHARE
Best Answer by mani619cash
Best Answer
Best Answer
Answered By 0 points N/A #111962

Need help in java project

qa-featured

Okay, here I am submitting you a solution. It is completely tested and in working condition and has no errros.

 

 

import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;

public class calculator implements ActionListener,FocusListener
{
static JFrame frame;
JTextField value1=new JTextField(30);
JTextField value2=new JTextField(30);
JTextField result=new JTextField(30);
JButton mul=new JButton("*");
JButton add=new JButton("+");
JButton sub=new JButton("-");
JButton div=new JButton("/");
calculator()
{frame=new JFrame("calculator");
//Toolkit theKit = frame.getToolkit();
//Dimension wndSize = theKit.getScreenSize();

frame.setBounds(30,30,500,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
frame.getContentPane().setBackground(Color.BLACK);
Container con=frame.getContentPane();
con.setLayout(null);
JPanel c=new JPanel();
c.setLayout(null);
con.add(c);

 ImageIcon back = new ImageIcon("blue3.jpg");
        JLabel bg = new JLabel(back);
        bg.setBounds(0, 0, back.getIconWidth(), back.getIconHeight());
    frame.getLayeredPane().add(bg, new Integer(Integer.MIN_VALUE));
        c.setOpaque(false);
        frame.setContentPane(c);

value1.setBounds(200,100,150,20);
value1.addFocusListener(this);
c.add(value1);

value2.setBounds(200,140,150,20);
value2.addFocusListener(this);
c.add(value2);

result.setBounds(200,180,150,20);
c.add(result);
JLabel v1=new JLabel("Value 1");
v1.setBounds(140,100,150,20);
c.add(v1);
JLabel v2=new JLabel("Value 2");
v2.setBounds(140,140,150,20);
c.add(v2);
JLabel re=new JLabel("Result");
re.setBounds(140,180,150,20);
c.add(re);

add.setBounds(100,260,50,20);
add.addActionListener(this);
c.add(add);

sub.setBounds(200,260,50,20);
sub.addActionListener(this);
c.add(sub);

mul.setBounds(300,260,50,20);
mul.addActionListener(this);
c.add(mul);

div.setBounds(400,260,50,20);
div.addActionListener(this);
c.add(div);

frame.setResizable(false);
frame.setVisible(true);
}
public void focusGained(FocusEvent f)
{
if(f.getSource()==value1)
{
result.setText("");
}
if(f.getSource()==value2)
{
result.setText("");
}
}
public void focusLost(FocusEvent l)
{}
public void actionPerformed(ActionEvent e)
{
String ad,su,mu,di,x=value1.getText(),y=value2.getText();
int a=Integer.parseInt(x),b=Integer.parseInt(y),r;

if(e.getSource()==add)
{
r=a+b;
 ad=""+r;
value1.setText("");
value2.setText("");
result.setText(ad);
}
if(e.getSource()==sub)
{

r=a-b;
su=""+r;

value1.setText("");
value2.setText("");
result.setText(su);
}
if(e.getSource()==mul)
{
r=a*b;
mu=""+r;
value1.setText("");
value2.setText("");
result.setText(mu);
}
if(e.getSource()==div)
{
r=a/b;
di=""+r;
value1.setText("");
value2.setText("");
result.setText(di);
}
 
}
public static void main(String[]args)
{
new calculator();
}
}

Answered By 0 points N/A #111963

Need help in java project

qa-featured

 

Hi,

I have managed to create calculator with java code with GUI mode. I also have function of perform operations like +,-,*,/. I will submit my program code for you. You will find it helpful and you can edit it in your own ways.


import javax.swing.*;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
//<applet code=Calculator height=300 width=200></applet>
public class Calculator extends JApplet {
   public void init() {
      CalculatorPanel calc=new CalculatorPanel();
      getContentPane().add(calc);
      }
   }

   class CalculatorPanel extends JPanel implements ActionListener {
      JButton
n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,plus,minus,mul,div,dot,equal;
      static JTextField result=new JTextField("0",45);
      static String lastCommand=null;
      JOptionPane p=new JOptionPane();
      double preRes=0,secVal=0,res;

      private static void assign(String no)
        {
         if((result.getText()).equals("0"))
            result.setText(no);
          else if(lastCommand=="=")
           {
            result.setText(no);
            lastCommand=null;
           }
          else
            result.setText(result.getText()+no);
         }

      public CalculatorPanel() {
         setLayout(new BorderLayout());
         result.setEditable(false);
         result.setSize(300,200);
         add(result,BorderLayout.NORTH);
         JPanel panel=new JPanel();
         panel.setLayout(new GridLayout(4,4));

         n7=new JButton("7");
         panel.add(n7);
         n7.addActionListener(this);
         n8=new JButton("8");
         panel.add(n8);
         n8.addActionListener(this);
         n9=new JButton("9");
         panel.add(n9);
         n9.addActionListener(this);
         div=new JButton("/");
         panel.add(div);
         div.addActionListener(this);

         n4=new JButton("4");
         panel.add(n4);
         n4.addActionListener(this);
         n5=new JButton("5");
         panel.add(n5);
         n5.addActionListener(this);
         n6=new JButton("6");
         panel.add(n6);
         n6.addActionListener(this);
         mul=new JButton("*");
         panel.add(mul);
         mul.addActionListener(this);

         n1=new JButton("1");
         panel.add(n1);
         n1.addActionListener(this);
         n2=new JButton("2");
         panel.add(n2);
         n2.addActionListener(this);
         n3=new JButton("3");
         panel.add(n3);
         n3.addActionListener(this);
         minus=new JButton("-");
         panel.add(minus);
         minus.addActionListener(this);

         dot=new JButton(".");
         panel.add(dot);
         dot.addActionListener(this);
         n0=new JButton("0");
         panel.add(n0);
         n0.addActionListener(this);
         equal=new JButton("=");
         panel.add(equal);
         equal.addActionListener(this);
         plus=new JButton("+");
         panel.add(plus);
         plus.addActionListener(this);
         add(panel,BorderLayout.CENTER);
      }
      public void actionPerformed(ActionEvent ae)
         {
      if(ae.getSource()==n1) assign("1");
      else if(ae.getSource()==n2) assign("2");
      else if(ae.getSource()==n3) assign("3");
      else if(ae.getSource()==n4) assign("4");
      else if(ae.getSource()==n5) assign("5");
      else if(ae.getSource()==n6) assign("6");
      else if(ae.getSource()==n7) assign("7");
      else if(ae.getSource()==n8) assign("8");
      else if(ae.getSource()==n9) assign("9");
      else if(ae.getSource()==n0) assign("0");
      else if(ae.getSource()==dot)
            {
             if(((result.getText()).indexOf("."))==-1)
                result.setText(result.getText()+".");
           }
      else if(ae.getSource()==minus)
             {
             preRes=Double.parseDouble(result.getText());
             lastCommand="-";
             result.setText("0");
             }
      else if(ae.getSource()==div)
             {
             preRes=Double.parseDouble(result.getText());
             lastCommand="/";
             result.setText("0");
             }
      else if(ae.getSource()==equal)
             {
             secVal=Double.parseDouble(result.getText());
             if(lastCommand.equals("/"))
                  res=preRes/secVal;
             else if(lastCommand.equals("*"))
                  res=preRes*secVal;
             else if(lastCommand.equals("-"))
                  res=preRes-secVal;
             else if(lastCommand.equals("+"))
                  res=preRes+secVal;
             result.setText(" "+res);
             lastCommand="=";
             }
      else if(ae.getSource()==mul)
             {
              preRes=Double.parseDouble(result.getText());
              lastCommand="*";
              result.setText("0");
              }
      else if(ae.getSource()==plus)
              {
              preRes=Double.parseDouble(result.getText());
              lastCommand="+";
              result.setText("0");
              }

       }
 }

Login/Register to Answer

Related Questions