Java based for Calculator: The Java code with the help of AWT/Swing with event handling. Let's see the code of creating calculator in java.
package calculator;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.Font;
public class calculater extends JFrame
{
private JPanel labelPanel, textFieldPanl, buttonPanel, buttonp, JRadioButtonp;
private JLabel label1, label2, label3 ,label4, label5,label6,label7,label8,label9;
private JButton button[];
public JTextField textField;
private JRadioButton pJRadioButton,bJRadioButton,iJRadioButton,biJRadioButton;
private ButtonGroup radioGroup;
private JButton button1,button2,button3;
private Font plainFont,boldFont,italicFont,boldItalicFont;
char operator;
String value1, value2,result;
Double num1,num2,answer;
public calculater()
{
setTitle("Culculater of Mumtaz Gohar");
labelPanel = new JPanel();
labelPanel.setLayout(new GridLayout(1,5));
label1 = new JLabel("Edit");
labelPanel.add(label1);
label2 = new JLabel("View");
labelPanel.add(label2);
label3 = new JLabel("Help");
labelPanel.add(label3);
label4 = new JLabel("");
labelPanel.add(label4);
label5= new JLabel(" ");
labelPanel.add(label5);
label6= new JLabel(" ");
labelPanel.add(label6);
label7= new JLabel(" ");
labelPanel.add(label7);
label8= new JLabel(" ");
labelPanel.add(label8);
label9= new JLabel(" ");
labelPanel.add(label9);
add(labelPanel,BorderLayout.NORTH);
textFieldPanl = new JPanel();
textFieldPanl.setLayout(new GridLayout(3,1) );
textField = new JTextField(10);
textFieldPanl.add(textField);
//creating radioButtons and it's panel
JRadioButtonp = new JPanel();
JRadioButtonp.setLayout(new GridLayout(1,4));
pJRadioButton = new JRadioButton("P",true);
JRadioButtonp.add(pJRadioButton);
bJRadioButton = new JRadioButton("B",true);
JRadioButtonp.add(bJRadioButton);
iJRadioButton = new JRadioButton("I");
JRadioButtonp.add(iJRadioButton);
biJRadioButton = new JRadioButton("B/I");
JRadioButtonp.add(biJRadioButton);
radioGroup = new ButtonGroup();
radioGroup.add(pJRadioButton);
radioGroup.add(bJRadioButton);
radioGroup.add(iJRadioButton);
radioGroup.add(biJRadioButton);
textFieldPanl.add(JRadioButtonp);
plainFont = new Font("Serif",Font.PLAIN,14);
boldFont = new Font("Serif",Font.BOLD,14);
italicFont= new Font("Serif",Font.ITALIC,14);
boldItalicFont = new Font("Serif",Font.BOLD+Font.ITALIC,14);
textField.setFont(plainFont);
pJRadioButton.addItemListener(new RadioButtonHandler(plainFont));
bJRadioButton.addItemListener(new RadioButtonHandler(boldFont));
iJRadioButton.addItemListener(new RadioButtonHandler(italicFont));
biJRadioButton.addItemListener(new RadioButtonHandler(boldItalicFont));
ButtonHandler handler = new ButtonHandler();
buttonp = new JPanel();
buttonp.setLayout(new GridLayout(1,4));
label7= new JLabel(" ");
buttonp.add(label7);
button1 = new JButton("Bksp");
buttonp.add(button1);
button2 = new JButton("Clr");
buttonp.add(button2);
button3 = new JButton("C");
buttonp.add(button3);
textFieldPanl.add(buttonp);
button1.addActionListener(handler);
button2.addActionListener(handler);
add(textFieldPanl, BorderLayout.CENTER);
String button1[]= {"M","7","8","9","/","S","R","4","5","6","*","%","S","1","2","3","_","&","C","0",".","#","+","="};
button = new JButton[24];
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4,6));
for(int count = 0; count < button1.length; count++)
{
button[count] = new JButton(button1[count]);
buttonPanel.add(button[count]);
button[count].setVisible(true);
button[count].addActionListener(handler);
}
add(buttonPanel,BorderLayout.SOUTH);
}
//creat new buttonHandler for button event handling
private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
result = "";
value1="";
value2="";
if(event.getSource() == button2)
{
textField.setText("");
value1="";
}
else if(event.getSource()==button1)
{
String str = textField.getText();
int ln = str.length();
ln = ln-1;
String str1 =str.substring(0, ln);
textField.setText(str1);
}
if(event.getSource()==button[0])
{
result = textField.getText();
result=result+"M";
textField.setText(result);
}
else if(event.getSource()==button[1])
{
result = textField.getText();
result=result+"7";
textField.setText(result);
}
else if(event.getSource()==button[2])
{
result = "";
result = textField.getText();
result=result+"8";
textField.setText(result);
}
if(event.getSource()==button[3])
{
result = textField.getText();
result=result+"9";
textField.setText(result);
}
if(event.getSource()==button[4])
{
value1= textField.getText();
num1 = Double.parseDouble(value1);
textField.setText("");
operator= '/';
textField.setText(result);
}
if(event.getSource()==button[5])
{
result = "";
result = textField.getText();
result=result+"";
textField.setText(result);
}
if(event.getSource()==button[6])
{
result = textField.getText();
result=result+"";
textField.setText(result);
}
if(event.getSource()==button[7])
{
result = textField.getText();
result=result+"4";
textField.setText(result);
}
if(event.getSource()==button[8])
{
result = textField.getText();
result=result+"5";
textField.setText(result);
}
if(event.getSource()==button[9])
{
result = textField.getText();
result=result+"6";
textField.setText(result);
}if(event.getSource()==button[10])
{
value1= textField.getText();
num1 = Double.parseDouble(value1);
textField.setText("");
operator= '*';
textField.setText(result);
}
if(event.getSource()==button[11])
{
value1= textField.getText();
num1 = Double.parseDouble(value1);
textField.setText("");
operator= '%';
textField.setText(result);
}if(event.getSource()==button[12])
{
result = textField.getText();
result=result+"";
textField.setText(result);
}if(event.getSource()==button[13])
{
result = textField.getText();
result=result+"1";
textField.setText(result);
}if(event.getSource()==button[14])
{
result = textField.getText();
result=result+"2";
textField.setText(result);
}if(event.getSource()==button[15])
{
result = textField.getText();
result=result+"3";
textField.setText(result);
}if(event.getSource()==button[16])
{
value1= textField.getText();
num1 = Double.parseDouble(value1);
textField.setText("");
operator= '-';
textField.setText(result);
}if(event.getSource()==button[17])
{
result = textField.getText();
result=result+"";
textField.setText(result);
}if(event.getSource()==button[18])
{
result = textField.getText();
result=result+"";
textField.setText(result);
}if(event.getSource()==button[19])
{
result = textField.getText();
result=result+"0";
textField.setText(result);
}if(event.getSource()==button[20])
{
result = textField.getText();
result=result+".";
textField.setText(result);
}if(event.getSource()==button[21])
{
result = textField.getText();
result=result+"";
textField.setText(result);
}
if(event.getSource()==button[22])
{
value1= textField.getText();
num1 = Double.parseDouble(value1);
textField.setText("");
operator = '+';
}
if(event.getSource()==button[23])
{
value2=textField.getText();
num2 = Double.parseDouble(value2);
switch(operator)
{
case '+':
answer = (num1+num2);
break;
case '*':
answer = (num1*num2);
break;
case '-':
answer = (num1-num2);
break;
case'/':
answer = (num1/num2);
break;
case '%':
answer = (num1%num2);
break;
default :
answer = num1;
}
result=answer.toString();
textField.setText(result);
}
}
}
//Private inner class to handle radio button events
private class RadioButtonHandler implements ItemListener
{
private Font font;
public RadioButtonHandler(Font f)
{
font = f;
}
public void itemStateChanged(ItemEvent event)
{
textField.setFont(font);
}
}
public static void main(String args[])
{
calculater calc = new calculater();
calc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calc.setSize(300,250);
calc.setVisible(true);
}
}
|
Programming
This blog sharing the valuable information about the programming , new trends in programming, Machine Learning Algorithms, Semantic Web.
Tuesday, November 5, 2019
Thursday, April 18, 2013
Employer Orientation Test Answers
1: Multiple Freelancer.com accounts are required in order to
both bid on jobs as a freelancer and hire freelancers as an employer.
18: I haven't received as many bids
as expected for my project. Which of the following can NOT be done to attract
more bidders, while the project is active?
48: Posting a project as Featured
incurs an additional fee of ____________.
Subscribe to:
Posts (Atom)