Home

Java Swing, tampilan Calculator dengan GridBagLayout

Mari berdiskusi bersama kami di Group Facebook Kurung Kurawal

Post kali ini, saya memberikan source code untuk membangun tampilan Calculator dengan Java Swing, menggunakan GridBagLayout.

Tentu saja, source code di bawah ini tidak akan berfungsi sebagai calculator utuh. Tombol tombol yang ada tidak akan berfungsi apa-apa.

Bagian menarik disini adalah pada bagian tombol “=” dan tombol “0”, dimana tombol “=” menggunakan 2 baris, dan tombol “0” menggunakan 2 kolom.

Berikut source code nya

1
2
3
4
5
6
7
8
9
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package whatever.name;
 
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import java.awt.*;
 
/**
 * Created with IntelliJ IDEA.
 * Author   : ali LIM ( [email protected] )
 * Website  : http://www.konglie.web.id
 * Date     : 1/9/2015
 * Time     : 10:57 PM
 * PERIZINAN
 * Saya bukan Pengacara yang mengerti Bahasa Hukum,
 * Namun, program ini saya berikan untuk tujuan pembelajaran
 * dan bagian atau seluruh code di file ini
 * TIDAK BOLEH didistribusikan untuk tujuan komersil atau
 * berbayar, tanpa izin tertulis dari saya.
 * LICENSE
 * I am not a lawyer, but part of or all of this source code
 * MAY NOT be distributed for commercial purpose, without
 * written permission from me.
 */
public class CalculatorGUI extends JFrame {
    public CalculatorGUI(){
        super("Calculator");
 
        try{
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e){
 
        }
        setResizable(false);
 
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        buildGUI();
        pack();
        setLocationRelativeTo(null);
    }
 
    private JTextField screen;
    private JPanel body;
    private Border pad = BorderFactory.createEmptyBorder(10,10,10,10);
    GridBagConstraints constraint = new GridBagConstraints();
    private void buildGUI(){
 
        constraint.insets = new Insets(2,2,2,2);
        constraint.ipadx = 2;
        constraint.ipady = 6;
 
        screen = new JTextField("0");
        Border screenBorder = new CompoundBorder(BorderFactory.createLineBorder(Color.GRAY, 2), new EmptyBorder(20,5,5,5));
        screen.setBorder(new CompoundBorder(new EmptyBorder(12,12,0,12), screenBorder));
        screen.setEditable(false);
        screen.setHorizontalAlignment(SwingConstants.RIGHT);
 
        getContentPane().add(screen, BorderLayout.NORTH);
        body = new JPanel(new GridBagLayout());
        body.setBorder(pad);
        getContentPane().add(body, BorderLayout.CENTER);
 
        addButton("MC,MR,MS,M+,M-");
        addButton("<html>&larr;</html>,CE,C,<html>&plusmn;</html>,<html>&radic;</html>");
        addButton("7,8,9,/,%");
        addButton("4,5,6,*,1/x");
        addButton("1,2,3,-");
 
        constraint.gridheight = 2;
        constraint.fill = GridBagConstraints.BOTH;
        constraint.gridx = 4;
        body.add(createButton("="), constraint);
 
        constraint.fill = GridBagConstraints.HORIZONTAL;
        constraint.gridheight = 1;
 
        constraint.gridwidth = 2;
        constraint.gridy = gridY;
        constraint.gridx = 0;
        body.add(createButton("0"), constraint);
 
        gridX = 2;
        constraint.gridwidth = 1;
        addButton(".,+");
    }
 
    private int gridX = 0, gridY = 0;
    private void addButton(String btns){
        constraint.fill = GridBagConstraints.HORIZONTAL;
        constraint.gridy = gridY;
        for(String s : btns.split(",")){
            constraint.gridx = gridX++;
            body.add(createButton(s), constraint);
        }
        gridX = 0;
        gridY++;
    }
 
    private JButton createButton(String s){
        JButton btn = new JButton(s);
        return btn;
    }
}

Dan berikut tampilannya,

swing-calculator

Semoga bisa berguna untuk kita semua.