Pada kesempatan kali ini saya akan menunjukkan bagaiman membuat aplikasi sederhana berupa kalkulator sederhana seperti pada gambar berikut:
Untuk membuat aplikasi ini, Anda dapat mengikuti langah-langkah berikut:
Buat sebuah Projek baru melalui menu File > New Project
Isi Nama Projek pada Project Name. sebagai contoh GUIApp, tentukan dimana projek akan disimpan pada Project Location, klik Finish.
Pada Projek yang Anda buat, arahkan pada Project Exploler, klik kanan kemudian pilih New > JFrame Form
Pada Jendela New Jframe Form isi nama Class pada bagian Class Name dengan Calcolator. Isi pada bagian Package dengan app.gui. Klik Finish.
Setelah JFrame Form terbuka, tambahkan beberapa komponen ke dalam Jframe sehingga seperti gambar berikut :
Untuk pengaturan masing-masing komponen dapat dilihat pada tabel berikut:
No. | Komponen | Properties | |
Key | Value | ||
1. | JPanel1 | Border | Etched Border |
2. | JLabel1 | Text | First Number : |
3. | JLabel2 | Second Number : | |
4. | JLabel3 | Operator : | |
5. | JLabel4 | Result : | |
6. | JTextField1 | Variable Name | txtFirst |
Text | <kosongkan> | ||
7. | JTextField2 | Variable Name | txtSecond |
Text | <kosongkan> | ||
8. | JTextField3 | Variable Name | txtResult |
Text | <kosongkan> | ||
9. | jComboBox1 | Variable Name | cmbOperator |
Model | +, -, *, / | ||
10. | jButton1 | Variable Name | btnProccess |
Text | Proccess | ||
11. | jButton2 | Variable Name | btnReset |
Text | Reset |
Tambahkan satu class baru. Isi bagian Class Name dengan Aritmatic dan Package dengan app.calc.Tambahkan kode program seperti berikut:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
| package app.calc; public class Aritmatic { private float first; private float second; private float result; private char operator; public void setFirst( float first) { this .first = first; } public void setOperator( char operator) { this .operator = operator; } public void setSecond( float second) { this .second = second; } public float getResult() { return result; } public void count() { switch ( this .operator) { case '+' : this .result = this .first + this .second; break ; case '-' : this .result = this .first - this .second; break ; case '*' : this .result = this .first * this .second; break ; case '/' : this .result = this .first / this .second; break ; default : this .result = 0 ; } } } |
Buka kembali file Calculator.java kemudian tambahkan kode program berikut untuk tombol Proccessdengan cara: Klik kanan > Events > Action > ActionPerformed.
1
2
3
4
5
6
7
8
| private void btnProccessActionPerformed(java.awt.event.ActionEvent evt) { Aritmatic ar = new Aritmatic(); ar.setFirst(Float.parseFloat(txtFirst.getText())); ar.setSecond(Float.parseFloat(txtSecond.getText())); ar.setOperator(cmbOperator.getSelectedItem().toString().charAt( 0 )); ar.count(); txtResult.setText(ar.getResult() + "" ); } |
Tambahkan kode program berikut untuk tombol Reset dengan cara: klik kanan > Events > Action >ActionPerformed.
1
2
3
4
5
| private void btnResetActionPerformed(java.awt.event.ActionEvent evt) { txtFirst.setText( "" ); txtSecond.setText( "" ); txtResult.setText( "" ); } |
Jalankan program, masukkan angka pertama pada First Number, Angka kedua pada Second Numberkemudian pilih operasi aritmatika pada combo Operator. Klik tombol Proccess. Perhatikan gambar berikut:
Selamat Anda telah berhasil membuat aplikasi java Swing GUI sederhana.
0Awesome Comments!