Professional Documents
Culture Documents
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
calci window = new calci();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public calci() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 343, 433);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
//button 0
btn0 = new JButton("0");
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //hello
String number = textField.getText()+btn0.getText();
textField.setText(number);
}
});
btn0.setBounds(10, 325, 68, 58);
frame.getContentPane().add(btn0);
//button 1
btn1 = new JButton("1");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //hello
String number = textField.getText()+btn1.getText();
textField.setText(number);
}
});
btn1.setBounds(10, 263, 68, 58);
frame.getContentPane().add(btn1);
//button 2
btn2 = new JButton("2");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //hello
String number = textField.getText()+btn2.getText();
textField.setText(number);
}
});
btn2.setBounds(88, 263, 68, 58);
frame.getContentPane().add(btn2);
//button 3
btn3 = new JButton("3");
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //hello
String number = textField.getText()+btn3.getText();
textField.setText(number);
}
});
btn3.setBounds(166, 263, 68, 58);
frame.getContentPane().add(btn3);
//button 4
btn4 = new JButton("4");
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //hello
String number = textField.getText()+btn4.getText();
textField.setText(number);
}
});
btn4.setBounds(10, 202, 68, 58);
frame.getContentPane().add(btn4);
//button 5
btn5 = new JButton("5");
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //hello
String number = textField.getText()+btn5.getText();
textField.setText(number);
}
});
btn5.setBounds(88, 202, 68, 58);
frame.getContentPane().add(btn5);
//button 6
btn6 = new JButton("6");
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //hello
String number = textField.getText()+btn6.getText();
textField.setText(number);
}
});
btn6.setBounds(166, 202, 68, 58);
frame.getContentPane().add(btn6);
//button 7
btn7 = new JButton("7");
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //hello
String number = textField.getText()+btn7.getText();
textField.setText(number);
}
});
btn7.setBounds(10, 141, 68, 58);
frame.getContentPane().add(btn7);
//button 8
btn8 = new JButton("8");
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //hello
String number = textField.getText()+btn8.getText();
textField.setText(number);
}
});
btn8.setBounds(88, 141, 68, 58);
frame.getContentPane().add(btn8);
//button 9
btn9 = new JButton("9");
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //hello
String number = textField.getText()+btn9.getText();
textField.setText(number);
}
});
btn9.setBounds(166, 141, 68, 58);
frame.getContentPane().add(btn9);
//button backspace
btnback = new JButton("\uf0e7");
btnback.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //hello
String s = textField.getText();
if(s.length()>0)
{
s=s.substring(0,s.length()-1);
textField.setText(s);
}
}
});
btnback.setFont(new Font("Wingdings", Font.PLAIN, 11));
btnback.setBounds(10, 80, 68, 58);
frame.getContentPane().add(btnback);
//button clear
btnclear = new JButton("C");
btnclear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(null);
}
});
btnclear.setBounds(88, 80, 68, 58);
frame.getContentPane().add(btnclear);
//button 00
btndoublezero = new JButton("00");
btndoublezero.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //hello
String number =
textField.getText()+btndoublezero.getText();
textField.setText(number);
}
});
btndoublezero.setBounds(166, 80, 68, 58);
frame.getContentPane().add(btndoublezero);
//button sum(+)
btnsum = new JButton("+");
btnsum.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
n1 = Double.parseDouble(textField.getText());
textField.setText(null);
operation="+"; //to indicate that + button is
pressed.
}
});
btnsum.setBounds(244, 80, 68, 58);
frame.getContentPane().add(btnsum);
//button difference(-)
btnsub = new JButton("-");
btnsub.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
n1 = Double.parseDouble(textField.getText());
textField.setText(null);
operation="-";
}
});
btnsub.setBounds(244, 141, 68, 58);
frame.getContentPane().add(btnsub);
//button product(*)
btnmul = new JButton("*");
btnmul.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
n1 = Double.parseDouble(textField.getText());
textField.setText(null);
operation="*";
}
});
btnmul.setBounds(244, 202, 68, 58);
frame.getContentPane().add(btnmul);
//button division(/)
btndiv = new JButton("/");
btndiv.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
n1 = Double.parseDouble(textField.getText());
textField.setText(null);
operation="/";
}
});
btndiv.setBounds(244, 263, 68, 58);
frame.getContentPane().add(btndiv);
//button percent(%)
btnmod = new JButton("%");
btnmod.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
n1 = Double.parseDouble(textField.getText());
textField.setText(null);
operation="%";
}
});
btnmod.setBounds(244, 325, 68, 58);
frame.getContentPane().add(btnmod);
//button dot
btndot = new JButton(".");
btndot.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //hello
String number =
textField.getText()+btndot.getText();
textField.setText(number);
}
});
btndot.setBounds(88, 325, 68, 58);
frame.getContentPane().add(btndot);
//button equal(=)
btnequal = new JButton("=");
btnequal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
n2 = Double.parseDouble(textField.getText());
switch(operation)
{
case "+":
result = n1+n2;
textField.setText(Double.toString(result));
break;
case "-":
result = n1-n2;
textField.setText(Double.toString(result));
break;
case "*":
result = n1*n2;
textField.setText(Double.toString(result));
break;
case "/":
result = n1/n2;
textField.setText(Double.toString(result));
break;
case "%":
result = n1%n2;
textField.setText(Double.toString(result));
break;
}
});
btnequal.setBounds(166, 325, 68, 58);
frame.getContentPane().add(btnequal);
}
}