Program Description:
The program below is a java code, a progject that demonstrates basic arithmetic operations using JTextField, JRadioButton, and JButton. This is a revised code from the blog PlaneCodes which I try to make the codes more understandable by putting comments and making it more organized.
The program can detect if there is no arithmetic operator selected. The program only accepts integer numbers and it will detect if the user puts double or float or string. The program can also recognize if the JTextFields are empty.
The program can detect if there is no arithmetic operator selected. The program only accepts integer numbers and it will detect if the user puts double or float or string. The program can also recognize if the JTextFields are empty.
Output:
Code:
/**
* File: basicArithmeticOperation.java
* Tiltle: Basic Arithmetic Operation using JRadioButton, JTextField, and JButton
* Author: http://java-code-complete.blogspot.com
*/
//Java Core Package
import javax.swing.*;
//Java Extension Package
import java.awt.*;
import java.awt.event.*;
public class basicArithmeticOperation extends JFrame {
//Initializing JButton, JPanel, JRadioButton, JTextField, and the class ButtonGroup
private JPanel panel1, panel2;
private JRadioButton addition, multiplication, subtraction, division;
private JTextField input1, input2;
private JButton answer, reset;
private ButtonGroup group;
//Setting up GUI
public basicArithmeticOperation() {
//Setting up the Title of the Window
super("Basic Arithmetic Operation: +,-,*,/");
//Set Size of the Window (WIDTH, LENGTH)
setSize(475,100);
//Exit Property of the Window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Constructing JRadioButton
addition = new JRadioButton("Addition[+]",false);
multiplication = new JRadioButton("Multiplication[x]",false);
subtraction = new JRadioButton("Subtraction[-]",false);
division = new JRadioButton("Division[/]",false);
//Constructing JPanel 1
panel1 = new JPanel();
panel1.setLayout(new GridLayout(1,4)); //setting layout on JPanel 1
//Adding JRadioButton in JPanel
panel1.add(addition);
panel1.add(multiplication);
panel1.add(subtraction);
panel1.add(division);
//Constructing the class ButtonGroup
group = new ButtonGroup();
//Group the four radio button using the class ButtonGroup
group.add(addition);
group.add(multiplication);
group.add(subtraction);
group.add(division);
//Constructing JPanel 2
panel2 = new JPanel();
panel2.setLayout(new GridLayout(1,4)); //setting layout on JPanel 2
//Constructing JTextField input1 and input2 with a size of 20
input1 = new JTextField(20);
input2 = new JTextField(20);
//Constructing JButton
answer = new JButton("Show Answer");
reset = new JButton("Reset");
//Adding JButton and JTextField in JPanel 2
panel2.add(input1);
panel2.add(input2);
panel2.add(answer);
panel2.add(reset);
//Setting up the container ready for the components to be added.
Container pane = getContentPane();
setContentPane(pane);
GridLayout grid = new GridLayout(2,1);
pane.setLayout(grid);
//Implemeting Even-Listener on JButton copy
answer.addActionListener(
new ActionListener() {
//Handle JButton event if it is clicked
public void actionPerformed(ActionEvent event) {
//If no arithmetic operator selected, a pop-up message will appear.
if (addition.isSelected() == false && multiplication.isSelected() == false && subtraction.isSelected() == false && division.isSelected() == false) {
JOptionPane.showMessageDialog(null, "Please Select an Arithmetic Operator","Error",JOptionPane.ERROR_MESSAGE);
}
if(addition.isSelected()==true){ //if "addition" radio button is selected
try { //fetch an error using "try-catch" function.
int a = Integer.parseInt(input1.getText()); //Convert JTextField input1 to integer by parsing and pass it to a variable "a" ready for arithmetic processing.
int b = Integer.parseInt(input2.getText()); //Convert JTextField input2 to integer by parsing and pass it to a variable "b" ready for arithmetic processing.
int ans = a+b; //Process the two inputs using addition.
JOptionPane.showMessageDialog(null, "The Answer is: "+ans,"Answer:",JOptionPane.INFORMATION_MESSAGE); //Display the answer using JOptionPane
} catch (NumberFormatException e){ //Catch the error if the user inputs a non-integer or number
JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE); //Display the catched error using JOptionPane
} //end of try-catch function
} //end of "if" condition
if(multiplication.isSelected()==true){
try {
int a = Integer.parseInt(input1.getText());
int b = Integer.parseInt(input2.getText());
int ans = a*b;
JOptionPane.showMessageDialog(null, "The Answer is: "+ans,"Answer:",JOptionPane.INFORMATION_MESSAGE);
} catch (NumberFormatException e){
JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);
}
}
if(subtraction.isSelected()==true){
try {
int a = Integer.parseInt(input1.getText());
int b = Integer.parseInt(input2.getText());
int ans = a-b;
JOptionPane.showMessageDialog(null, "The Answer is: "+ans,"Answer:",JOptionPane.INFORMATION_MESSAGE);
} catch (NumberFormatException e){
JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);
}
}
if(division.isSelected()==true){
try {
int a = Integer.parseInt(input1.getText());
int b = Integer.parseInt(input2.getText());
int ans = a/b;
JOptionPane.showMessageDialog(null, "The Answer is: "+ans,"Answer:",JOptionPane.INFORMATION_MESSAGE);
} catch (NumberFormatException e){
JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);
}
}
}
}
);
//Implemeting Even-Listener on JButton reset
reset.addActionListener(
new ActionListener() {
//Handle JButton event if it is clicked
public void actionPerformed(ActionEvent event) {
input1.setText(null); //Empty JTextField input1 ready for the next input
input2.setText(null); //Empty JTextField input2 ready for the next input
}
}
);
//Adding the two panels in the container
pane.add(panel1);
pane.add(panel2);
/**Set all the Components Visible.
* If it is set to "false", the components in the container will not be visible.
*/
setVisible(true);
setResizable(false);
}
//Main Method
public static void main (String[] args) {
basicArithmeticOperation bao = new basicArithmeticOperation();
}
}
No comments:
Post a Comment