Sei sulla pagina 1di 13

/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package gui.runner.com;

import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;

/**
*
* @author FACULTY
*/
public class GuiDemo extends javax.swing.JFrame {

/**
* Creates new form GuiDemo
*/
public GuiDemo() {
initComponents();
groupRadioButtons();
}

public void groupRadioButtons(){

ButtonGroup group = new ButtonGroup();


group.add(rdbAddition);
group.add(rdbSubtraction);
group.add(rdbMultiplication);
group.add(rdbDivision);

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

txtFirstNum = new javax.swing.JTextField();


txtSecondNum = new javax.swing.JTextField();
txtResult = new javax.swing.JTextField();
btnCalculate = new javax.swing.JButton();
btnClear = new javax.swing.JButton();
btnClose = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();

jLabel3 = new javax.swing.JLabel();


rdbAddition = new javax.swing.JRadioButton();
rdbSubtraction = new javax.swing.JRadioButton();
rdbMultiplication = new javax.swing.JRadioButton();
rdbDivision = new javax.swing.JRadioButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

txtFirstNum.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N

txtSecondNum.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N

txtResult.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N

btnCalculate.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N


btnCalculate.setText("Calculate");
btnCalculate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCalculateActionPerformed(evt);
}
});

btnClear.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N


btnClear.setText("Clear");
btnClear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {

btnClearActionPerformed(evt);
}
});

btnClose.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N


btnClose.setText("Close");
btnClose.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCloseActionPerformed(evt);
}
});

jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N


jLabel1.setText("First Number:");

jLabel2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N


jLabel2.setText("Second Number:");

jLabel3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N


jLabel3.setText("Result:");

rdbAddition.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N


rdbAddition.setText("Addition");

rdbSubtraction.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N


rdbSubtraction.setText("Subtraction");

rdbMultiplication.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N


rdbMultiplication.setText("Multiplication");

rdbDivision.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N


rdbDivision.setText("Division");

javax.swing.GroupLayout layout = new


javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addGroup(layout.createSequentialGroup()
.addGap(80, 80, 80)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING
)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(txtSecondNum)

.addComponent(txtFirstNum)
.addComponent(txtResult,
javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addComponent(rdbAddition)
.addComponent(rdbSubtraction)
.addComponent(rdbMultiplication)
.addComponent(rdbDivision)))
.addGroup(layout.createSequentialGroup()
.addGap(47, 47, 47)
.addComponent(btnCalculate)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnClear,
javax.swing.GroupLayout.PREFERRED_SIZE, 103,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnClose,
javax.swing.GroupLayout.PREFERRED_SIZE, 103,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(34, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(51, 51, 51)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E)
.addComponent(txtFirstNum,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1)
.addComponent(rdbAddition))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addGroup(layout.createSequentialGroup()
.addComponent(rdbSubtraction)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(rdbMultiplication)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(rdbDivision)
.addGap(26, 26, 26))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING
)
.addComponent(jLabel2)
.addComponent(txtSecondNum,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(18, 18, 18)


.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING
)
.addComponent(txtResult,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E)
.addComponent(btnCalculate)
.addComponent(btnClose)
.addComponent(btnClear))
.addContainerGap(26, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void btnCalculateActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
if(txtFirstNum.getText().equals("")){
JOptionPane.showMessageDialog(null,
"Please enter the first number",
"Simple Calculator",

JOptionPane.WARNING_MESSAGE);
this.txtFirstNum.requestFocusInWindow();
}else if(txtSecondNum.getText().equals("")){
JOptionPane.showMessageDialog(null,
"Please enter the second number",
"Simple Calculator",
JOptionPane.WARNING_MESSAGE);
this.txtSecondNum.requestFocusInWindow();
}else{

int firstNum = Integer.parseInt(txtFirstNum.getText());


int secondNum = Integer.parseInt(txtSecondNum.getText());

if(rdbAddition.isSelected()){
int result = firstNum + secondNum;
txtResult.setText(Integer.toString(result));
}else if(rdbSubtraction.isSelected()){
int result = firstNum - secondNum;
txtResult.setText(Integer.toString(result));
}else if(rdbMultiplication.isSelected()){
int result = firstNum * secondNum;
txtResult.setText(Integer.toString(result));
}else if(rdbDivision.isSelected()){
if(secondNum == 0){
JOptionPane.showMessageDialog(null,
"Division by zero",

"Division",JOptionPane.ERROR_MESSAGE);
}else{
int result = firstNum / secondNum;
txtResult.setText(Integer.toString(result));
}
}else{
JOptionPane.showMessageDialog(null,
"Select an Operation",
"Simple Calculator",
JOptionPane.WARNING_MESSAGE);
}
}
}

private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
System.exit(0);
}

private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
if(txtFirstNum.getText().equals("")||txtSecondNum.getText().equals("")||
txtResult.getText().equals("")){
JOptionPane.showMessageDialog(null,"There's an empty
field","Clear",JOptionPane.WARNING_MESSAGE);
}else{
txtFirstNum.setText(null);

txtSecondNum.setText(null);
txtResult.setText(null);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code
(optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look
and feel.
* For details see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(GuiDemo.class.getName()).log(java.util.logging.L
evel.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(GuiDemo.class.getName()).log(java.util.logging.L
evel.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GuiDemo.class.getName()).log(java.util.logging.L
evel.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GuiDemo.class.getName()).log(java.util.logging.L
evel.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GuiDemo().setVisible(true);
}
});
}

// Variables declaration - do not modify


private javax.swing.JButton btnCalculate;
private javax.swing.JButton btnClear;

private javax.swing.JButton btnClose;


private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JRadioButton rdbAddition;
private javax.swing.JRadioButton rdbDivision;
private javax.swing.JRadioButton rdbMultiplication;
private javax.swing.JRadioButton rdbSubtraction;
private javax.swing.JTextField txtFirstNum;
private javax.swing.JTextField txtResult;
private javax.swing.JTextField txtSecondNum;
// End of variables declaration
}

Potrebbero piacerti anche