import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
* Created by JFormDesigner on Fri Jan 27 12:02:45 CST 2012
*/
/**
* @author
*/
public class test extends JFrame {
//Creates variables for calculating, z = x op y
double x, y, z;
int op;
//Creates interface components
private JLabel label1 = new JLabel();
private JLabel label2 = new JLabel();
private JTextField textField1 = new JTextField();
private JTextField textField2 = new JTextField();
private JButton button1 = new JButton();
private JButton button2 = new JButton();
private JButton button3 = new JButton();
private JButton button4 = new JButton();
private JButton button5 = new JButton();
private JTextField textField3 = new JTextField();
private JTextField textField4 = new JTextField();
public test() {
//Sets layout
initComponents();
//Sets handler to required gui component
actionHandler Handler = new actionHandler();
button1.addActionListener(Handler);
button2.addActionListener(Handler);
button3.addActionListener(Handler);
button4.addActionListener(Handler);
button5.addActionListener(Handler);
textField1.addActionListener(Handler);
textField2.addActionListener(Handler);
textField3.addActionListener(Handler);
}
//Handler class
private class actionHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
//When +, -, *, / is pressed
if(event.getSource()==button1) { //When button is pressed
textField4.setText("+"); //Shows on textField4 which operation you pressed
op = 1; //Sets operation
}
if(event.getSource()==button2) {
textField4.setText("-");
op = 2;
}
if(event.getSource()==button3) {
textField4.setText("*");
op = 3;
}
if(event.getSource()==button4) {
textField4.setText("/");
op = 4;
}
if(event.getSource()==button5) { //when = is pressed
if(textField1.getText() != null && textField2.getText() != null && textField4.getText() != null) //Makes sure no textbox is empty
setData(//Sends x, y and op values to setData
Double.parseDouble(textField1.getText()), //x
Double.parseDouble(textField2.getText()), //y
op
);
textField3.setText(Double.toString(z)); //Displays final result
}
}
}
void setData(double x, double y, int op) { //Calculating part
switch (op) {
case 1:
z = x + y;
break;
case 2:
z = x - y;
break;
case 3:
z = x * y;
break;
case 4:
z = x / y;
break;
}
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
// Generated using JFormDesigner Evaluation license -
//======== this ========
setFont(new Font("Dialog", Font.PLAIN, 14));
setTitle("Simple Calculator");
Container contentPane = getContentPane();
//---- label1 ----
label1.setText("INPUT1");
label1.setFont(new Font("Tahoma", Font.PLAIN, 14));
//---- label2 ----
label2.setText("INPUT2");
label2.setFont(new Font("Tahoma", Font.PLAIN, 14));
//---- button1 ----
button1.setText("+");
//---- button2 ----
button2.setText("-");
//---- button3 ----
button3.setText("*");
//---- button4 ----
button4.setText("/");
//---- button5 ----
button5.setText("=");
GroupLayout contentPaneLayout = new GroupLayout(contentPane);
contentPane.setLayout(contentPaneLayout);
contentPaneLayout.setHorizontalGroup(
contentPaneLayout.createParallelGroup()
.addGroup(contentPaneLayout.createSequentialGroup()
.addGap(46, 46, 46)
.addGroup(contentPaneLayout.createParallelGroup()
.addGroup(contentPaneLayout.createSequentialGroup()
.addComponent(button5)
.addGap(18, 18, 18)
.addComponent(textField3, GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE))
.addGroup(contentPaneLayout.createSequentialGroup()
.addGroup(contentPaneLayout.createParallelGroup()
.addComponent(textField1, GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)
.addComponent(label1))
.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addGroup(contentPaneLayout.createSequentialGroup()
.addGap(62, 62, 62)
.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
.addComponent(button3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(button1, GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE))
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
.addComponent(button4, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(button2, GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 60, Short.MAX_VALUE))
.addGroup(contentPaneLayout.createSequentialGroup()
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(textField4, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)
.addGap(87, 87, 87)))
.addGroup(contentPaneLayout.createParallelGroup()
.addComponent(textField2, GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)
.addComponent(label2, GroupLayout.Alignment.TRAILING))))
.addGap(54, 54, 54))
);
contentPaneLayout.setVerticalGroup(
contentPaneLayout.createParallelGroup()
.addGroup(contentPaneLayout.createSequentialGroup()
.addGap(20, 20, 20)
.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(label1)
.addComponent(label2)
.addComponent(button1)
.addComponent(button2, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(contentPaneLayout.createParallelGroup()
.addGroup(contentPaneLayout.createSequentialGroup()
.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
.addComponent(button4, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(button3, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(textField4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addComponent(textField1, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
.addComponent(textField2, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE))
.addGap(22, 22, 22)
.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(button5)
.addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addContainerGap(56, Short.MAX_VALUE))
);
pack();
setLocationRelativeTo(getOwner());
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
// Generated using JFormDesigner Evaluation license -
// JFormDesigner - End of variables declaration //GEN-END:variables
}