Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- class CobaAction extends JFrame{
- // attribute
- JRadioButton cb = new JRadioButton("Makan");
- JRadioButton cb2 = new JRadioButton("Ayam");
- JRadioButton cb3 = new JRadioButton("goreng");
- ButtonGroup group = new ButtonGroup();
- JCheckBox cekbox1 = new JCheckBox("S1");
- JCheckBox cekbox2 = new JCheckBox("S2");
- JCheckBox cekbox3 = new JCheckBox("S3");
- JLabel labelrb = new JLabel("radio button yang dipilih ?");
- JTextArea labelcb = new JTextArea();
- JScrollPane scrl ;
- // anda juga dapat meletakan tag HTML di label
- JLabel ket1 = new JLabel("<html><u>Radio button</u></html>");
- JLabel ket2 = new JLabel("<html><i>CheckBox</i></html>");
- public CobaAction(){
- //frame seting---------------------------------------------------------------------
- //memanggil konstruktor kelas induk (JFrame)
- super("Aksi chekbox dan radiobutton");
- //seting besar frame LEBAR x TINGGI
- this.setSize(300,350);
- //seting agar bisa ditutup
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- //seting kemunculan frame di tengah window
- this.setLocationRelativeTo(null);
- //seting layout frame
- this.getContentPane().setLayout(null);
- //frame seting---------------------------------------------------------------------
- }
- public void konfig(){
- this.add(ket1);
- this.add(cb);
- this.add(cb2);
- this.add(cb3);
- this.add(labelrb);
- this.add(ket2);
- this.add(cekbox1);
- this.add(cekbox2);
- this.add(cekbox3);
- scrl = new JScrollPane(labelcb);
- this.add(scrl);
- // seting text area tidak bisa di input
- labelcb.setEditable(false);
- // jadikan radion button dalam 1 grup agar hanya 1 yang dapat dipilih
- group.add(cb);
- group.add(cb2);
- group.add(cb3);
- // kita set tata letak dan ukurannya
- // setbounds (x,y,lebar/panjang,tinggi)
- ket1.setBounds(10,5,150,30);
- cb.setBounds(10,35,150,30);
- cb2.setBounds(10,65,150,30);
- cb3.setBounds(10,95,150,30);
- labelrb.setBounds(10,125,250,30);
- ket2.setBounds(10,155,150,30);
- cekbox1.setBounds(10,185,150,30);
- cekbox2.setBounds(10,215,150,30);
- cekbox3.setBounds(10,245,150,30);
- scrl.setBounds(160,185,120,90);
- }
- public void setAksi(){
- // aksi cekbox
- cb.addActionListener(new ActionListener( ) {
- public void actionPerformed(ActionEvent e){
- // kode disini; cek keadaan check box
- labelrb.setText("Anda pilih makan");
- }
- });
- cb2.addActionListener(new ActionListener( ) {
- public void actionPerformed(ActionEvent e){
- // kode disini; cek keadaan check box
- labelrb.setText("Anda pilih ayam");
- }
- });
- cb3.addActionListener(new ActionListener( ) {
- public void actionPerformed(ActionEvent e){
- // kode disini; cek keadaan check box
- labelrb.setText("Anda pilih makan");
- }
- });
- cekbox1.addActionListener(new ActionListener( ) {
- public void actionPerformed(ActionEvent e){
- // kode disini; cek keadaan check box
- if(cekbox1.isSelected()){
- labelcb.append("-Anda sudah S1\n");
- }else{
- labelcb.append("-Anda belum S1\n");
- }
- }
- });
- cekbox2.addActionListener(new ActionListener( ) {
- public void actionPerformed(ActionEvent e){
- // kode disini; cek keadaan check box
- if(cekbox2.isSelected()){
- labelcb.append("-Anda sudah S2\n");
- }else{
- labelcb.append("-Anda belum S2\n");
- }
- }
- });
- cekbox3.addActionListener(new ActionListener( ) {
- public void actionPerformed(ActionEvent e){
- // kode disini; cek keadaan check box
- if(cekbox3.isSelected()){
- labelcb.append("-Anda sudah S3\n");
- }else{
- labelcb.append("-Anda belum S3\n");
- }
- }
- });
- }
- public static void main(String[] args){
- CobaAction act = new CobaAction();
- act.konfig();
- act.setAksi();
- // metode show bawaan dari JFrame (kelas induk)
- // untuk menampilkan Frame kelayar
- act.show();
- }
- }
Add Comment
Please, Sign In to add comment