Completed Code (Calculator)
package nebulous.tutorial;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import java.awt.Color;
public class Calculator extends JFrame {
/* Completed Calculator Code
* By: Ben Ratcliff
*
* A simple calculator
*
*/
private static final long serialVersionUID = 1L; // Serial ID Code for JFrame (ignore this)
// Vars for Jframe (Buttons, textfields, etc)
private JPanel contentPane;
private static JTextField answer;
private static JFormattedTextField input1;
private static JFormattedTextField input2;
private static JRadioButton add;
private static JRadioButton sub;
private static JRadioButton dev;
private static JRadioButton mul;
private static JButton calc;
// Main function
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() { // Sets up thread
public void run() { // Runs thread
try {
Calculator frame = new Calculator(); // New instance of Calculator
frame.setVisible(true); // Sets frame to visable
logic(); // Starts logic code
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
// Vars for calculation (inputs / calculations)
public static float in1 = 0.0f;
public static float in2 = 0.0f;
public static float out = 0.0f;
// Logic method
// Calls logic functions for calculation
public static void logic(){
add.setSelected(true); // Sets add option to enabled by default
calc.addActionListener(new ActionListener() { // Adds action listener to calculate button
@Override
public void actionPerformed(ActionEvent e) {
in1 = Float.parseFloat(input1.getText()); // Parses first number to a float, then sets in1 to that number
in2 = Float.parseFloat(input2.getText()); // Parses second number to a float, then sets in2 to that number
if(add.isSelected()) out = in1 + in2; // Sets output to sum
if(sub.isSelected()) out = in1 - in2; // Sets output to difference
if(dev.isSelected()) out = in1 / in2; // Sets output to quotient
if(mul.isSelected()) out = in1 * in2; // Sets output to product
answer.setText(Float.toString(out)); // Sets answer field to the output
}
});
}
// Calculator (constructor)
// Most of this code is generated by WindowBuilder
public Calculator() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e){
e.printStackTrace();
}
setBounds(100, 100, 390, 400);
setResizable(false);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
input1 = new JFormattedTextField();
input1.setFont(new Font("Tahoma", Font.PLAIN, 16));
input1.setBounds(73, 52, 251, 40);
contentPane.add(input1);
input1.setColumns(10);
input2 = new JFormattedTextField();
input2.setFont(new Font("Tahoma", Font.PLAIN, 16));
input2.setColumns(10);
input2.setBounds(73, 103, 251, 40);
contentPane.add(input2);
add = new JRadioButton(" + ");
add.setBounds(330, 45, 44, 23);
contentPane.add(add);
add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sub.setSelected(false);
dev.setSelected(false);
mul.setSelected(false);
}
});
sub = new JRadioButton(" - ");
sub.setBounds(330, 69, 44, 23);
contentPane.add(sub);
sub.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
add.setSelected(false);
dev.setSelected(false);
mul.setSelected(false);
}
});
dev = new JRadioButton(" / ");
dev.setBounds(330, 95, 44, 23);
contentPane.add(dev);
dev.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sub.setSelected(false);
add.setSelected(false);
mul.setSelected(false);
}
});
mul = new JRadioButton(" * ");
mul.setBounds(330, 120, 44, 23);
contentPane.add(mul);
mul.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sub.setSelected(false);
dev.setSelected(false);
add.setSelected(false);
}
});
answer = new JTextField();
answer.setFont(new Font("Tahoma", Font.PLAIN, 16));
answer.setBackground(Color.WHITE);
answer.setBounds(17, 299, 341, 40);
answer.setEditable(false);
contentPane.add(answer);
answer.setColumns(10);
JLabel lblNum = new JLabel("Num 1");
lblNum.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNum.setBounds(18, 52, 93, 40);
contentPane.add(lblNum);
JLabel lblNum_1 = new JLabel("Num 2");
lblNum_1.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNum_1.setBounds(18, 103, 93, 40);
contentPane.add(lblNum_1);
JLabel lblBasicCalculator = new JLabel("BASIC CALCULATOR");
lblBasicCalculator.setFont(new Font("Tahoma", Font.PLAIN, 18));
lblBasicCalculator.setBounds(18, 5, 286, 41);
contentPane.add(lblBasicCalculator);
calc = new JButton("CALCULATE");
calc.setFont(new Font("Microsoft Sans Serif", Font.BOLD, 16));
calc.setBounds(18, 158, 342, 89);
contentPane.add(calc);
JLabel lblAnswer = new JLabel("Answer:");
lblAnswer.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblAnswer.setBounds(16, 253, 93, 40);
contentPane.add(lblAnswer);
}
}