Minggu, 10 April 2016

Java : KotakPesan.java

Assalamu'alaikum

1.      Pertama buat folder dengann nama bab25, lalu ketik kode dibawah ini dan simpat dengan nama KotakPesan.java

package bab25;
import java.awt.*;
import java.awt.event.*;

public class KotakPesan extends Dialog {
            String pesan;

            public KotakPesan (Frame f, String pesan) {
                        super(f, "Informasi", true);
                        this.pesan = pesan;
            }

            public void show() {
                        add ("North", new Label(this.pesan, Label.CENTER));
                        Button b = new Button ("OK");

                        Panel p = new Panel();
                        p.add(b);
                        add ("South", p);
                        super.pack();

                        b.addActionListener(
                                    new KotakPesan.PenanganTombol());
                        addWindowListener (
                                    new KotakPesan.PenanganKejadian());

                        super.show();
            }

            class PenanganKejadian extends WindowAdapter {
                        public void windowClosing (WindowEvent e) {
                                    dispose();
                        }
            }

            class PenanganTombol implements ActionListener {
                        public void actionPerformed (ActionEvent e) {
                                    String s = e.getActionCommand ();
                                    if (s.equals("OK")) {
                                                dispose();
                                    }
                        }
            }

}


2.      Kemudian ketik kode dibawah ini, tetapi simpan di luar folder bab25 tadi dengan nama ContohDialog.java
import java.awt.*;
import java.awt.event.*;
import bab25.KotakPesan;

public class ContohDialog extends Frame {
      public static void main (String [] args) {
                  ContohDialog apl = new ContohDialog ();
      }

      public ContohDialog () {
                  super ("Contoh Dialog");
                  setSize (300, 100);

                  Button tombolDialog = new Button ("Dialog");
                  Button tombolSelesai = new Button ("Selesai");

                  Panel panelTombol = new Panel();
                  panelTombol.add(tombolDialog);
                  panelTombol.add(tombolSelesai);
                  add ("South", panelTombol);

                  //Penangan Kejadian
                  tombolDialog.addActionListener(
                              new ContohDialog.PenanganTombol());
                  tombolSelesai.addActionListener(
                              new ContohDialog.PenanganTombol());

                  addWindowListener(
                              new ContohDialog.PenanganKejadian());

                  show();
      }
      class PenanganKejadian extends WindowAdapter {
                  public void windowClosing (WindowEvent e){
                              System.exit(0);
                  }
      }
      class PenanganTombol implements ActionListener {
                  public void actionPerformed (ActionEvent e) {
                              String s = e.getActionCommand();

                              if (s.equals("Dialog")) {
                                          KotakPesan kp =
                                                      new KotakPesan (ContohDialog.this,
                                                      "Anda mengklik tombol Dialog");
                                          kp.show();
                              }
                              else
                              if (s.equals("Selesai")) {
                                          System.exit (0);
                              }
                  }
      }
}

3.      Buka file KotakPesan.java dan ContohDialog.java dengan textpad
4.      Compile KotakPesan.java terlebih dahulu kemudian compile juga file ContohDialog.java
5.      Jalankan ContohDialog.java
6.      Hasilnya akan terlihat seperti gambar dibawah
7.       



Apabila di klik tombol Dialog maka akan muncul jenndela seperti gambar dibawah dan  jika di tombol Selesai di tekan, maka akan menutup jendela.



Jika di klik OK, jendela akan menghilang.



0 komentar:

Posting Komentar