Minggu, 12 Mei 2013

Program GUI


Listing Program

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

public class infongr extends JFrame implements ActionListener, ItemListener {
String[] negara = {"Indonesia", "Arab Saudi", "Italia", "Jepang", "prancis", "Belanda", "Cina", "Filipina", "Korea Selatan", "Spanyol"};
String[] ibukota = {"Jakarta" , "Riyadh", "Roma", "Tokyo", "Paris", "Amesterdam" , "Beijing", "Manila", "Seoul" ,"Madrid"};
String[] benua = {"Asia", "Asia", "Eropa", "Asia", "Eropa", "Eropa", "Asia", "Asia", "Asia", "Eropa" };

JLabel ketNegara = new JLabel ("Negara : ");
JComboBox komboNegara = new JComboBox ();
JLabel info = new JLabel ("Informasi : ");
JCheckBox cek_ibukota = new JCheckBox ("Ikutkan info ibukota", true);

ButtonGroup rbg = new ButtonGroup();
JRadioButton rb1 = new JRadioButton ("Semua negara", true);
JRadioButton rb2 = new JRadioButton ("Negara di Asia saja", false);

public infongr () {
super("Informasi Negara");
setSize (500, 200);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

for (int i=0; i < negara.length; i++)
komboNegara.addItem(negara[i]);
komboNegara.addItemListener(this);

JPanel panelAtas = new JPanel ();

panelAtas.add(ketNegara);
panelAtas.add(komboNegara);
panelAtas.add (info);

JPanel panelBawah = new JPanel ();

panelBawah.add(cek_ibukota);

rbg.add (rb1);
rbg.add (rb2);

panelBawah.add(rb1);
panelBawah.add(rb2);

getContentPane().add ("North", panelAtas);
getContentPane().add ("South", panelBawah);

rb1.addActionListener(this);
rb2.addActionListener(this);
cek_ibukota.addActionListener(this);
setVisible (true);
}
public void itemStateChanged (ItemEvent evt) {
int pilihan = komboNegara.getSelectedIndex();
if (pilihan != -1)
info.setText ("Ibukotanya: " + ibukota[pilihan]);
else
info.setText ("");
}
public void actionPerformed (ActionEvent evt) {
String s = evt.getActionCommand();

if (s.equals ("Semua negara")) {
komboNegara.removeAllItems();
for (int i=0; i < negara.length; i++)
komboNegara.addItem(negara[i]);
}
else
if (s.equals ("Negara di Asia saja")) {
komboNegara.removeAllItems();
for (int i=0; i < negara.length; i++)
if (benua[i] == "Asia")
komboNegara.addItem(negara[i]);
}
else
if (s.equals("Ikutkan info ibukota")) {
if (cek_ibukota.isSelected())
info.setVisible(true);
else
info.setVisible(false);
}}
public static void main (String[]args) {
infongr app = new infongr ();
}}

Output Program




0 komentar:

Posting Komentar