INFO KNOWLEDGE

Contoh Soal Java


1. Contoh Program Event Handling di Java.
Pada contoh berikut ini ditambahkan window konfirmasi saat tombol exit ditekan.
Berikut ini tampilannya:

Listing:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class ClickMe3 extends JFrame {

private JButton tombol, btnExit;

public ClickMe3() {

super (“Event Handling”);

Container container = getContentPane();

container.setLayout(new FlowLayout());

ClickListener cl = new ClickListener ();

tombol = new JButton (“Click Me!”);

tombol.addActionListener(cl);

container.add(tombol);

btnExit = new JButton (“Exit”);

btnExit.addActionListener(cl);

container.add(btnExit);

setSize (200,100);

setVisible (true);

}

public static void main (String arg[]) {

ClickMe3 test = new ClickMe3();

test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

//inner class

private class ClickListener implements ActionListener {

public void actionPerformed (ActionEvent e) {

if (e.getSource() == tombol) {

JOptionPane.showMessageDialog(null, “You click me again, guys !!!”);

} else if (e.getSource() == btnExit){

if ( JOptionPane.showConfirmDialog(null, “Apakah Anda yakin akan keluar ?”,”Konfirmasi”,

JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {

System.exit(0);

}

}

}

}

}

2.Contoh Massage Dialog dijava

Contoh program berikut ini menampilkan jenis-jenis window pesan di Java. Window pesan (message dialog) antara lain bertipe warning message, information message, confirmation message, dan juga input message. Class yang digunakan adalah class JOptionPane.

Berikut ini tampilannya:

Listing:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class MessageDialog extends JFrame {

private JButton tombol, btn2, btn3, btn4, btn5;

public MessageDialog() {

super (“Event Handling”);

Container container = getContentPane();

container.setLayout(new FlowLayout());

tombol = new JButton (“Message Dialog”);

tombol.addActionListener(

new ActionListener() {

public void actionPerformed (ActionEvent e) {

JOptionPane.showMessageDialog (null,”Contoh Message Dialog”);

}

}

);

container.add(tombol);

btn2 = new JButton (“Warning Message”);

btn2.addActionListener(

new ActionListener() {

public void actionPerformed (ActionEvent e) {

JOptionPane.showConfirmDialog(null, “Contoh Warning Message”,”Peringatan”,

JOptionPane.CLOSED_OPTION, JOptionPane.WARNING_MESSAGE);

}

}

);

container.add(btn2);

btn3 = new JButton (“Question Message”);

btn3.addActionListener(

new ActionListener() {

public void actionPerformed (ActionEvent e) {

JOptionPane.showConfirmDialog(null, “Contoh Question Message”,”Pertanyaan”,

JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);

}

}

);

container.add(btn3);

btn4 = new JButton (“Information Message”);

btn4.addActionListener(

new ActionListener() {

public void actionPerformed (ActionEvent e) {

JOptionPane.showConfirmDialog(null, “Contoh Information Message”,”Informasi”,

JOptionPane.NO_OPTION, JOptionPane.INFORMATION_MESSAGE);

}

}

);

container.add(btn4);

btn5 = new JButton (“Input Dialog”);

btn5.addActionListener(

new ActionListener() {

public void actionPerformed (ActionEvent e) {

String a = JOptionPane.showInputDialog(“Input Nama : “);

JOptionPane.showMessageDialog(null, a);

}

}

);

container.add(btn5);

setSize (200,300);

setLocationRelativeTo(null);

setVisible (true);

}

public static void main (String arg[]) {

MessageDialog test = new MessageDialog();

test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

 

Yang lebih Lengkap download disini.!!!

 

4 responses to “Contoh Soal Java”

  1. No cojo excesivamente adecuadamente la cuestion que has intentado hacer comentarios.
    Como sea me encanta tu internet web.

  2. Cuenta este weblog una seccion de contacto? Estoy twniendo con bastantes dificultades
    para encontrarla, me gustaria mandarlle mmi correo. Se me ocurren cuantiosas ideas para tu Web blog que seguramente estes interesado
    en oir. De todas maneras es un increible website y lo visitare
    con frecuencia.

  3. Guay! No he podido conseguir un pagina tan seductora como este
    en la totalidad de la on-line! La Mayoiria De la noticia resulta ser demasiado ventajoso.

  4. Me cautiva tu pagina. Yo Creo que has
    apujtado la asesoria de modo increiblemente bonita. Te incluyo rapido en predilectos!!!

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.