- Define Frame or Applet to hold components
- Add GUI components to Frame. Use layout manager to determine position
- Add actions to GUI. By adding event listeners to GUI components
The codes below show an event listener (handle button clicking) to calculate simple math calculation: addition, subtraction, multiplication, and division
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
try{
FileWriter fw = new FileWriter("db.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
double total = 0.0, n1=0.0, n2=0.0;
String insert, ops;
insert = num1.getText();
n1 = Integer.parseInt(insert);
insert = num2.getText();
n2 = Integer.parseInt(insert);
ops = (String) arithmetic.getSelectedItem();
if(ops.equalsIgnoreCase("+"))
total = n1 + n2;
else if(ops.equalsIgnoreCase("-"))
total = n1 - n2;
else if(ops.equalsIgnoreCase("/"))
total = n1 / n2;
else if(ops.equalsIgnoreCase("*"))
total = n1 * n2;
insert = String.valueOf(total);
pw.append(n1 + "," + n2 + "," + ops + "," + total );
pw.println();
answer.setText(insert);
pw.close();
}
catch(FileNotFoundException fnfe)
{
System.out.print(fnfe.getMessage());
}
catch(IOException ioe)
{
System.out.print(ioe.getMessage());
}
}//GEN-LAST:event_jButton1ActionPerformed
Download Full Code :here
Download BMI calculator: here
No comments:
Post a Comment