“Calculator” dengan Java Swing (NetBeans IDE 7.4)
Contoh pembuatan aplikasi “Calculator” dengan bahasa pemrograman Java SE berbasis GUI dengan kontrol Swing dan menggunakan editor NetBeans 7.4.
Sebagai referensi visualisasi pembuatan Project baru dan lain-lain dapat teman-teman lihat di sini.
-
Jalankan NetBeans IDE 7.4.
-
Klik File > New Project atau tekan tombol Ctrl+Shift+N pada keyboard.
-
Pada jendela New Project, pada bagian Categories pilih “Java”. Pada pilihan Project pilih “Java Application” kemudian klik tombol “Next >”.
-
Pada jendela New Java Application, Pada kotak isian Project Name: tuliskan “CalculatorApp”. Hilangkan pilihan Create Main Class. Kemudian klik tombol Finish.
-
Pada jendela Project, klik kanan CalculatorApp pilih New > JFrame Form…
-
Pada jendela New JFrame Form, Pada kotak isian Class Name: tuliskan “CalculatorAppUI”. Pada kotak isian Package: tuliskan “My.ControlPropertiesApp” kemudian klik tombol “Finish”
Penerapan kontrol Swing properti sebagai berikut:
Objek | Nama | Properti | Penerapan |
Form | jFrame | title | Calculator |
Text Field | jTextField | Font text | Tahoma 14 Bold (Kosongkan) |
Button | jButton1 | text | Bck |
Button | jButton2 | text | 7 |
Button | jButton3 | text | 4 |
Button | jButton4 | text | 1 |
Button | jButton5 | text | . |
Button | jButton6 | text | CE |
Button | jButton7 | text | 8 |
Button | jButton8 | text | 5 |
Button | jButton9 | text | 2 |
Button | jButton10 | text | 0 |
Button | jButton11 | text | C |
Button | jButton12 | text | 9 |
Button | jButton13 | text | 6 |
Button | jButton14 | text | 3 |
Button | jButton15 | text | +/- |
Button | jButton16 | text | / |
Button | jButton17 | text | * |
Button | jButton18 | text | - |
Button | jButton19 | text | + |
Button | jButton20 | text | = |
Button | jButton21 | text | Off |
Button | jButton22 | text | Sqrt |
Button | jButton23 | text | Sin |
Button | jButton24 | text | Cos |
Button | jButton25 | text | Tan |
Perintah program kejadian pada pembuatan aplikasi ini:
1: public class CalculatorAppUI extends javax.swing.JFrame {
2:
3: private double total1 = 0.0;
4: private double total2 = 0.0;
5: private char math_operator;
6: /**
7: * Creates new form CalculatorAppUI
8: */
9: public CalculatorAppUI() {
10: initComponents();
11: }
12:
13: private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
14: String btnOne = jTextField1.getText() + jButton1.getText();
15: jTextField1.setText(btnOne);
16: }
17:
18: private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
19: String btnTwo = jTextField1.getText() + jButton2.getText();
20: jTextField1.setText(btnTwo);
21: }
22:
23: private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
24: String btnThree = jTextField1.getText() + jButton3.getText();
25: jTextField1.setText(btnThree);
26: }
27:
28: private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
29: String btnFour = jTextField1.getText() + jButton4.getText();
30: jTextField1.setText(btnFour);
31: }
32:
33: private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
34: String btnFive = jTextField1.getText() + jButton5.getText();
35: jTextField1.setText(btnFive);
36: }
37:
38: private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
39: String btnSix = jTextField1.getText() + jButton6.getText();
40: jTextField1.setText(btnSix);
41: }
42:
43: private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
44: String btnSeven = jTextField1.getText() + jButton7.getText();
45: jTextField1.setText(btnSeven);
46: }
47:
48: private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
49: String btnEight = jTextField1.getText() + jButton8.getText();
50: jTextField1.setText(btnEight);
51: }
52:
53: private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
54: String btnNine = jTextField1.getText() + jButton9.getText();
55: jTextField1.setText(btnNine);
56: }
57:
58: private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
59: String btnZero = jTextField1.getText() + jButton10.getText();
60: jTextField1.setText(btnZero);
61: }
62:
63: private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {
64: String button_text = jButton16.getText();
65: getOperator(button_text);
66: }
67:
68: private void jButton24ActionPerformed(java.awt.event.ActionEvent evt) {
69: System.exit(0);
70: }
71:
72: private void jButton21ActionPerformed(java.awt.event.ActionEvent evt) {
73: total2 = 0;
74: jTextField1.setText("");
75: }
76:
77: private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {
78: String btnPoint = jTextField1.getText() + jButton12.getText();
79: jTextField1.setText(btnPoint);
80: }
81:
82: private void jButton20ActionPerformed(java.awt.event.ActionEvent evt) {
83: switch(math_operator){
84: case '+':
85: total2 = total1 + Double.parseDouble(jTextField1.getText());
86: break;
87: case '-':
88: total2 = total1 - Double.parseDouble(jTextField1.getText());
89: break;
90: case '*':
91: total2 = total1 * Double.parseDouble(jTextField1.getText());
92: break;
93: case '/':
94: total2 = total1 / Double.parseDouble(jTextField1.getText());
95: break;
96: }
97: jTextField1.setText(Double.toString(total2));
98: total1 = 0;
99: }
100:
101: private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {
102: String button_text = jButton15.getText();
103: getOperator(button_text);
104: }
105:
106: private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {
107: String button_text = jButton14.getText();
108: getOperator(button_text);
109: }
110:
111: private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {
112: String button_text = jButton13.getText();
113: getOperator(button_text);
114: }
115:
116: private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {
117: Double btnPlusMin = Double.parseDouble(String.valueOf(jTextField1.getText()));
118: btnPlusMin = btnPlusMin * (-1);
119: jTextField1.setText(String.valueOf(btnPlusMin));
120: }
121:
122: private void jButton17ActionPerformed(java.awt.event.ActionEvent evt) {
123: Double btnSqrt = Math.sqrt(Double.parseDouble(String.valueOf(jTextField1.getText())));
124: jTextField1.setText(String.valueOf(btnSqrt));
125: }
126:
127: private void jButton18ActionPerformed(java.awt.event.ActionEvent evt) {
128: Double btnSin = Math.sin(Double.parseDouble(String.valueOf(jTextField1.getText())));
129: jTextField1.setText(String.valueOf(btnSin));
130: }
131:
132: private void jButton19ActionPerformed(java.awt.event.ActionEvent evt) {
133: Double btnCos = Math.cos(Double.parseDouble(String.valueOf(jTextField1.getText())));
134: jTextField1.setText(String.valueOf(btnCos));
135: }
136:
137: private void jButton25ActionPerformed(java.awt.event.ActionEvent evt) {
138: Double btnTan = Math.tan(Double.parseDouble(String.valueOf(jTextField1.getText())));
139: jTextField1.setText(String.valueOf(btnTan));
140: }
141:
142: private void jButton23ActionPerformed(java.awt.event.ActionEvent evt) {
143: String btnBack = jTextField1.getText();
144: String tmp = btnBack.substring(0,btnBack.length()-1);
145: jTextField1.setText(tmp);
146: }
147:
148: private void jButton22ActionPerformed(java.awt.event.ActionEvent evt) {
149: jTextField1.setText("");
150: }
151:
152: private void getOperator(String btnText){
153: math_operator = btnText.charAt(0);
154: total1 = total1 + Double.parseDouble(jTextField1.getText());
155: jTextField1.setText("");
156: }
7. Jalankan Project, pada ToolBar klik tombol Run Project atau klik F6 pada keyboard
8. Pada jendela Run Project klik tombol OK untuk menjadikan CalculatorAppUI menjadi main class.
Label: Java SE
0 Komentar:
Posting Komentar
Berlangganan Posting Komentar [Atom]
<< Beranda