Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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();
}
/**
* 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() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
btnClearActionPerformed(evt);
}
});
.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))
pack();
}// </editor-fold>
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{
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);
}
}
}
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>