Contoh Program Java Swing Praktikum PBO

Sekedar share aja siapa tau ada yang butuh buat referensi

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

class MyCanvas extends JComponent {
    public void paint(Graphics g) {
        g.setColor(Color.yellow);
        g.fillRect(((300-100)/2),((300-100)/2),75,75);
    }
}

public class RectButton {
    public static void main(String[] a) {
        ActionListener actionListener = new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                System.exit(0);
            }
        };

        JFrame window = new JFrame("Contoh");
        JButton exitBtn = new JButton("EXIT");
        window.getContentPane().add(exitBtn, BorderLayout.SOUTH);
        exitBtn.addActionListener(actionListener);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setBounds(30, 30, 300, 300);
        window.getContentPane().add(new MyCanvas());
        window.setLocationRelativeTo(null);
        window.setVisible(true);
    }
}

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class temuTiga{
    private JTextField txtNim, txtNama, txtGender;
    private JFrame window;
    private JRadioButton satu, dua, tiga, empat;
    private JButton tampilkan, kosongkan, keluar;
    private JLabel lblNim, lblNama, lblGender;
    private ButtonGroup bgroup;
    private JPanel radioPanels, radioPanel;
    private String dataString [];
    private String stringData;

    public temuTiga(){
        dataString  = new String[3];
        window      = new JFrame("Contoh");
        satu        = new JRadioButton("Anggota Satu"  , true);
        dua         = new JRadioButton("Anggota Dua"   , false);
        tiga        = new JRadioButton("Anggota Tiga", false);
        empat       = new JRadioButton("Anggota Empat", false);
        tampilkan   = new JButton("Tampilkan");
        kosongkan   = new JButton("Kosongkan");
        keluar      = new JButton("Keluar");
        lblNim      = new JLabel("NIM");
        lblNama     = new JLabel("Nama");
        lblGender   = new JLabel("Jenis Kelamin");
        txtNim      = new JTextField(15);
        txtNama     = new JTextField(15);
        txtGender   = new JTextField(15);
        bgroup      = new ButtonGroup();
        radioPanel  = new JPanel();
        radioPanels = new JPanel();

        stringData  = "||";
        dataString[0] = "06.2007.1.04232";
        dataString[1] = "Fenny Su'aidah";
        dataString[2] = "Perempuan";

        satu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                rdP(1);
            }
        });

        dua.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                rdP(2);
            }
        });

        tiga.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                rdP(3);
            }
        });

        empat.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                rdP(4);
            }
        });

        tampilkan.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                rdP(5);
            }
        });

        kosongkan.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                rdP(6);
            }
        });

        keluar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                rdP(7);
            }
        });

        bgroup.add(satu);
        bgroup.add(dua);
        bgroup.add(tiga);
        bgroup.add(empat);

        radioPanel.setLayout(new GridLayout(2, 2));
        radioPanel.add(satu);
        radioPanel.add(dua);
        radioPanel.add(tiga);
        radioPanel.add(empat);

        radioPanels.setLayout(new GridLayout(5, 2, 4, 4));
        radioPanels.add(lblNim);
        radioPanels.add(txtNim);
        radioPanels.add(lblNama);
        radioPanels.add(txtNama);
        radioPanels.add(lblGender);
        radioPanels.add(txtGender);
        radioPanels.add(tampilkan);
        radioPanels.add(kosongkan);
        radioPanels.add(keluar);

        radioPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Anggota"));
        radioPanels.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Data"));

        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setBounds(30, 30, 605, 200);
        window.getContentPane().add(radioPanel, BorderLayout.WEST);
        window.getContentPane().add(radioPanels, BorderLayout.EAST);
        window.setLocationRelativeTo(null);
        window.setVisible(true);
    }

    private void rdP(int radioB){
        if(radioB==1){
            dataString[0] = "06.2007.1.04232";
            dataString[1] = "Fenny Su'aidah";
            dataString[2] = "Perempuan";
        }else if(radioB==2){
            dataString[0] = "06.2007.1.04900";
            dataString[1] = "Rendra Kurniawan";
            dataString[2] = "Laki - Laki";
        }else if(radioB==3){
            dataString[0] = "06.2007.1.04880";
            dataString[1] = "Kiki Fendi";
            dataString[2] = "Laki - Laki";
        }else if(radioB==4){
            dataString[0] = "06.2007.1.04901";
            dataString[1] = "Muadzin Rizal";
            dataString[2] = "Laki - Laki";
        }else if(radioB==5){
            txtNim.setText(dataString[0]);
            txtNama.setText(dataString[1]);
            txtGender.setText(dataString[2]);
        }else if(radioB==6){
            stringData  = "||";
            txtNim.setText("");
            txtNama.setText("");
            txtGender.setText("");
        }else{
            System.exit(0);
        }
    }

    public static void main(String[] a) {
        temuTiga m = new temuTiga();
    }
}

2 thoughts on “Contoh Program Java Swing Praktikum PBO

Leave a Reply