17
//PROGRAM 13-2 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DemoAppletSwing extends JApplet implements ActionListener { public JTextField textField; public JButton button; public void init() { // mendapatkan container Container cont = getContentPane(); cont.setLayout(new FlowLayout()); // menambahkan komponen JTextField textField = new JTextField(25); cont.add(textField, BorderLayout.CENTER); // menambahkan komponen JButton button = new JButton("Tombol Applet"); button.addActionListener(this); cont.add(button, BorderLayout.CENTER); } // menangani event klik pada komponen JButton public void actionPerformed(ActionEvent event) { if (event.getSource() == button) { textField.setText("Anda telah melakukan klik " + "pada tombol Applet"); } } } //PROGRAM 13-4 import java.awt.*; import java.awt.event.*; import javax.swing.*; class DemoButton implements ActionListener { public JLabel label; public JButton btn1, btn2; public DemoButton() { ImageIcon icon = new ImageIcon("images/tunjuk.jpg"); label = new JLabel("Belum ada button yang diklik"); label.setLocation(60, 40); label.setSize(label.getPreferredSize()); btn1 = new JButton("Button #1"); btn1.setLocation(40, 70); btn1.setSize(btn1.getPreferredSize()); btn1.addActionListener(this); btn2 = new JButton("Button #2", icon); btn2.setLocation(135, 70); btn2.setSize(btn2.getPreferredSize()); btn2.addActionListener(this); } public void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("Demo JButton"); frame.setLayout(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON _CLOSE);

PROGRAM 13-2 import java.awt.*; //PROGRAM 13-4 · import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DemoAppletSwing extends JApplet implements ActionListener

Embed Size (px)

Citation preview

Page 1: PROGRAM 13-2 import java.awt.*; //PROGRAM 13-4 · import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DemoAppletSwing extends JApplet implements ActionListener

//PROGRAM 13-2

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class DemoAppletSwing extends JApplet

implements ActionListener {

public JTextField textField;

public JButton button;

public void init() {

// mendapatkan container

Container cont = getContentPane();

cont.setLayout(new FlowLayout());

// menambahkan komponen JTextField

textField = new JTextField(25);

cont.add(textField, BorderLayout.CENTER);

// menambahkan komponen JButton

button = new JButton("Tombol Applet");

button.addActionListener(this);

cont.add(button, BorderLayout.CENTER);

}

// menangani event klik pada komponen

JButton

public void actionPerformed(ActionEvent event)

{

if (event.getSource() == button) {

textField.setText("Anda telah melakukan klik " +

"pada tombol Applet");

}

}

}

//PROGRAM 13-4

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class DemoButton implements ActionListener {

public JLabel label;

public JButton btn1, btn2;

public DemoButton() {

ImageIcon icon = new

ImageIcon("images/tunjuk.jpg");

label = new JLabel("Belum ada button yang

diklik");

label.setLocation(60, 40);

label.setSize(label.getPreferredSize());

btn1 = new JButton("Button #1");

btn1.setLocation(40, 70);

btn1.setSize(btn1.getPreferredSize());

btn1.addActionListener(this);

btn2 = new JButton("Button #2", icon);

btn2.setLocation(135, 70);

btn2.setSize(btn2.getPreferredSize());

btn2.addActionListener(this);

}

public void createAndShowGUI() {

JFrame.setDefaultLookAndFeelDecorated(true);

JFrame frame = new JFrame("Demo JButton");

frame.setLayout(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON

_CLOSE);

Page 2: PROGRAM 13-2 import java.awt.*; //PROGRAM 13-4 · import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DemoAppletSwing extends JApplet implements ActionListener

frame.getContentPane().add(label,

BorderLayout.CENTER);

frame.getContentPane().add(btn1);

frame.getContentPane().add(btn2);

frame.setBounds(0, 0, 300, 200);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

public void actionPerformed(ActionEvent event)

{

if (event.getSource() == btn1) {

label.setText("Button #1 telah diklik");

} else if (event.getSource() == btn2) {

label.setText("Button #2 telah diklik");

}

}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new

Runnable() {

public void run() {

DemoFrame app = new DemoFrame();

app.createAndShowGUI();

}

});

}

}

//PROGRAM 13-5

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class DemoCheckBox implements ItemListener {

public JLabel label1;

public JCheckBox checkBox1, checkBox2,

checkBox3;

private JFrame frame;

public DemoCheckBox() {

label1 = new JLabel("Pilihan:");

label1.setLocation(10, 10);

label1.setSize(label1.getPreferredSize());

checkBox1 = new JCheckBox("C", true);

checkBox1.setLocation(10, 25);

checkBox1.addItemListener(this);

checkBox1.setSize(checkBox1.getPreferredSize());

checkBox2 = new JCheckBox("C++", true);

checkBox2.setLocation(10, 50);

checkBox2.addItemListener(this);

checkBox2.setSize(checkBox2.getPreferredSize());

checkBox3 = new JCheckBox("Java", true);

checkBox3.setLocation(10, 75);

checkBox3.addItemListener(this);

checkBox3.setSize(checkBox3.getPreferredSize());

}

public void createAndShowGUI() {

JFrame.setDefaultLookAndFeelDecorated(true);

frame = new JFrame("Demo JCheckBox");

frame.setLayout(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON

_CLOSE);

frame.getContentPane().add(label1);

Page 3: PROGRAM 13-2 import java.awt.*; //PROGRAM 13-4 · import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DemoAppletSwing extends JApplet implements ActionListener

frame.getContentPane().add(checkBox1);

frame.getContentPane().add(checkBox2);

frame.getContentPane().add(checkBox3);

frame.setBounds(0, 0, 300, 200);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

public void itemStateChanged(ItemEvent event)

{

JCheckBox cb = (JCheckBox) event.getSource();

if (event.getStateChange() ==

ItemEvent.SELECTED) {

JOptionPane.showMessageDialog(frame,

"Anda telah memilih: " + cb.getText());

} else {

JOptionPane.showMessageDialog(frame,

"Anda telah telah melepas pilihan: " +

cb.getText());

}

}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new

Runnable() {

public void run() {

DemoCheckBox app = new DemoCheckBox();

app.createAndShowGUI();

}

});

}

}

//PROGRAM 13-9

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class DemoComboBox implements ActionListener

{

public JLabel labelNama, labelLahir,

labelTanggal, labelBulan, labelTahun;

public JTextField tfNama, tfInfo;

public JComboBox comboTanggal, comboBulan,

comboTahun;

public JButton btnProses;

public DemoComboBox() {

String s1 = "<html><font

color=red>Nama:</font></html>";

labelNama = new JLabel(s1);

labelNama.setLocation(10, 10);

labelNama.setSize(labelNama.getPreferredSize())

;

tfNama = new JTextField(25);

tfNama.setLocation(10, 30);

tfNama.setSize(tfNama.getPreferredSize());

String s2 =

"<html><font color=red>Tanggal

Lahir:</font></html>";

labelLahir = new JLabel(s2);

labelLahir.setLocation(10, 55);

labelLahir.setSize(labelLahir.getPreferredSize());

labelTanggal = new JLabel("Hari ke-");

labelTanggal.setLocation(35, 75);

Page 4: PROGRAM 13-2 import java.awt.*; //PROGRAM 13-4 · import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DemoAppletSwing extends JApplet implements ActionListener

labelTanggal.setSize(labelTanggal.getPreferredSiz

e());

comboTanggal = new JComboBox();

comboTanggal.setLocation(35, 93);

comboTanggal.setSize(labelTanggal.getPreferredS

ize());

for (int i=0; i<31; i++) {

comboTanggal.addItem(new

String().valueOf(i+1));

}

labelBulan = new JLabel("Bulan");

labelBulan.setLocation(95, 75);

labelBulan.setSize(labelBulan.getPreferredSize());

comboBulan = new JComboBox();

comboBulan.setLocation(95, 93);

comboBulan.setSize(labelLahir .getPreferredSize()

);

String[] bulan =

{"Januari","Februari","Maret","April",

"Mei","Juni","Juli","Agustus",

"September","Oktober","November","Desember

"};

for (int i=0; i<bulan.length; i++) {

comboBulan.addItem(bulan[i]);

}

labelTahun = new JLabel("Tahun");

labelTahun.setLocation(190, 75);

labelTahun.setSize(labelTahun.getPreferredSize()

);

comboTahun = new JComboBox();

comboTahun.setLocation(190, 93);

comboTahun.setSize(labelLahir .getPreferredSize()

);

for (int i=1960; i<=2007; i++) {

comboTahun.addItem(new

String().valueOf(i));

}

btnProses = new JButton("Proses Data");

btnProses.setLocation(305, 25);

btnProses.setSize(btnProses.getPreferredSize());

btnProses.addActionListener(this);

btnProses.setMnemonic('P');

tfInfo = new JTextField(50);

tfInfo.setLocation(10, 290);

tfInfo.setSize(400, 20);

tfInfo.setEditable(false);

}

public void createAndShowGUI() {

JFrame.setDefaultLookAndFeelDecorated(true);

JFrame frame = new JFrame("Demo

JComboBox");

frame.setLayout(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON

_CLOSE);

frame.getContentPane().add(labelNama);

frame.getContentPane().add(tfNama);

frame.getContentPane().add(labelLahir);

frame.getContentPane().add(labelTanggal);

frame.getContentPane().add(comboTanggal);

frame.getContentPane().add(labelBulan);

frame.getContentPane().add(comboBulan);

frame.getContentPane().add(labelTahun);

Page 5: PROGRAM 13-2 import java.awt.*; //PROGRAM 13-4 · import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DemoAppletSwing extends JApplet implements ActionListener

frame.getContentPane().add(comboTahun);

frame.getContentPane().add(btnProses);

frame.getContentPane().add(tfInfo);

frame.setBounds(0, 0, 430, 350);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

public void actionPerformed(ActionEvent event)

{

if (event.getSource() == btnProses) {

String s = tfNama.getText() + ", lahir pada " +

comboTanggal.getSelectedItem() + " " +

comboBulan.getSelectedItem() + " " +

comboTahun.getSelectedItem();

tfInfo.setText(s);

}

}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new

Runnable() {

public void run() {

DemoComboBox app = new

DemoComboBox();

app.createAndShowGUI();

}

});

}

}

//PROGRAM 13-3

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class DemoFrame {

private JLabel label;

public DemoFrame() {

label = new JLabel("Hallo Dunia...");

}

public void createAndShowGUI() {

JFrame.setDefaultLookAndFeelDecorated(true);

JFrame frame = new JFrame("Demo Frame");

frame.setDefaultCloseOperation(JFrame.EXIT_ON

_CLOSE);

frame.getContentPane().add(label,

BorderLayout.CENTER);

//frame.pack();

frame.setBounds(0, 0, 300, 200); //ukuran

frame

frame.setLocationRelativeTo(null); //frame di

tengah layar

frame.setVisible(true);

}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new

Runnable() {

public void run() {

DemoFrame app = new DemoFrame();

app.createAndShowGUI();

}

});

}

Page 6: PROGRAM 13-2 import java.awt.*; //PROGRAM 13-4 · import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DemoAppletSwing extends JApplet implements ActionListener

}

//PROGRAM 13-10

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class DemoList implements ActionListener {

public JFrame frame;

public JList list;

public JButton btnTambah, btnHapus;

public JTextField tfItem;

public JScrollPane scroller;

public DemoList() {

tfItem = new JTextField(12);

tfItem.setLocation(10,20);

tfItem.setSize(tfItem.getPreferredSize());

DefaultListModel lm;

lm = new DefaultListModel();

lm.addElement("Java");

lm.addElement("C");

lm.addElement("C++");

list = new JList(lm);

list.setSelectionMode(ListSelectionModel.SINGLE

_SELECTION);

scroller = new JScrollPane(list);

scroller.setLocation(10, 43);

scroller.setSize(new Dimension(135, 170));

btnTambah = new JButton("Tambah");

btnTambah.setLocation(150, 20);

btnTambah.setSize(btnTambah.getPreferredSize(

));

btnTambah.addActionListener(this);

btnTambah.setMnemonic('T');

btnHapus = new JButton("Hapus");

btnHapus.setLocation(150, 50);

btnHapus.setSize(btnTambah.getPreferredSize());

btnHapus.addActionListener(this);

btnHapus.setMnemonic('H');

}

public void createAndShowGUI() {

JFrame.setDefaultLookAndFeelDecorated(true);

frame = new JFrame("Demo JList");

frame.setLayout(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON

_CLOSE);

frame.getContentPane().add(tfItem);

frame.getContentPane().add(scroller);

frame.getContentPane().add(btnTambah);

frame.getContentPane().add(btnHapus);

frame.setBounds(0, 0, 430, 350);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

public void actionPerformed(ActionEvent event)

{

if (event.getSource() == btnTambah) {

String s = tfItem.getText().trim();

if (!s.equals("")) {

Page 7: PROGRAM 13-2 import java.awt.*; //PROGRAM 13-4 · import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DemoAppletSwing extends JApplet implements ActionListener

((DefaultListModel)

list.getModel()).addElement(s);

}

if (list.getModel().getSize() == 1) {

btnHapus.setEnabled(true);

}

} else {

int index = list.getSelectedIndex();

if (index == -1) {

JOptionPane.showMessageDialog(frame,

"Anda harus memilih salah satu item " +

"terlebih dahulu");

return;

}

((DefaultListModel)list.getModel()).remove(index

);

int size = list.getModel().getSize();

if (size == 0) {

btnHapus.setEnabled(false);

} else {

if (index == list.getModel().getSize()) {

index--;

}

list.setSelectedIndex(index);

list.ensureIndexIsVisible(index);

}

}

}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new

Runnable() {

public void run() {

DemoList app = new DemoList();

app.createAndShowGUI();

}

});

}

}

//PROGRAM 13-14

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class DemoMenu implements ActionListener {

public JMenuBar menuBar;

public JMenu menu;

public JMenuItem menuItem;

public JCheckBoxMenuItem cbMenuItem;

public JRadioButtonMenuItem rbMenuItem;

public DemoMenu() {

menuBar = new JMenuBar();

menuBar.setLocation(0,0);

menuBar.setSize(new Dimension(440, 20));

menu = new JMenu("Menu Ke-1");

menu.setMnemonic('1');

menuBar.add(menu);

menuItem = new JMenuItem("Item menu

berupa teks");

menu.add(menuItem);

Page 8: PROGRAM 13-2 import java.awt.*; //PROGRAM 13-4 · import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DemoAppletSwing extends JApplet implements ActionListener

menuItem = new JMenuItem(

"Item menu berupa gambar dan teks",

new ImageIcon("gambar.jpg"));

menu.add(menuItem);

menuItem = new JMenuItem(new

ImageIcon("gambar.jpg"));

menu.add(menuItem);

menu.addSeparator();

cbMenuItem = new JCheckBoxMenuItem(

"Item menu berupa checkbox");

menu.add(cbMenuItem);

cbMenuItem = new JCheckBoxMenuItem(

"Item menu berupa checkbox dan gambar",

new ImageIcon("gambar.jpg"));

menu.add(cbMenuItem);

menu.addSeparator();

rbMenuItem = new JRadioButtonMenuItem(

"Item menu berupa radiobutton");

menu.add(rbMenuItem);

rbMenuItem = new JRadioButtonMenuItem(

"Item menu berupa radiobutton dan gambar",

new ImageIcon("gambar.jpg"));

menu.add(rbMenuItem);

menu.addSeparator();

menuItem = new JMenuItem("Keluar");

// membuat shortcut dengan ALT-K

menuItem.setAccelerator(KeyStroke.getKeyStrok

e(

KeyEvent.VK_K,

ActionEvent.ALT_MASK));

menuItem.addActionListener(this);

menu.add(menuItem);

menu = new JMenu("Menu Ke-2");

menu.setMnemonic('2');

menuBar.add(menu);

// tambahkan item menu untuk Menu ke-2

pada bagian ini

}

public void actionPerformed(ActionEvent event)

{

JMenuItem item = (JMenuItem)

event.getSource();

if (item.getText().equals("Keluar")) {

System.exit(0); // keluar program

}

}

public void createAndShowGUI() {

JFrame.setDefaultLookAndFeelDecorated(true);

JFrame frame = new JFrame("Demo Menu");

frame.setLayout(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON

_CLOSE);

frame.getContentPane().add(menuBar);

frame.setBounds(0, 0, 450, 350);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new

Runnable() {

public void run() {

Page 9: PROGRAM 13-2 import java.awt.*; //PROGRAM 13-4 · import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DemoAppletSwing extends JApplet implements ActionListener

DemoMenu app = new DemoMenu();

app.createAndShowGUI();

}

});

}

}

//PROGRAM 13-6

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class DemoRadioButton implements ItemListener

{

public JLabel label1, label2;

public JRadioButton radioButton1,

radioButton2, radioButton3;

public ButtonGroup bg;

public DemoRadioButton() {

label1 = new JLabel("IDE Java yang Anda

sukai:");

label1.setLocation(10, 10);

label1.setSize(label1.getPreferredSize());

label2 = new JLabel("Pilihan Anda: (belum ada

pilihan)");

label2.setLocation(10, 115);

label2.setSize(label2.getPreferredSize());

radioButton1 = new JRadioButton("Eclipse");

radioButton1.setLocation(10, 25);

radioButton1.addItemListener(this);

radioButton1.setSize(radioButton1.getPreferredS

ize());

radioButton2 = new JRadioButton("Netbeans");

radioButton2.setLocation(10, 50);

radioButton2.addItemListener(this);

radioButton2.setSize(radioButton2.getPreferredS

ize());

radioButton3 = new JRadioButton("JBuilder");

radioButton3.setLocation(10, 75);

radioButton3.addItemListener(this);

radioButton3.setSize(radioButton3.getPreferredS

ize());

// menentukan group dari semua item

bg = new ButtonGroup();

bg.add(radioButton1);

bg.add(radioButton2);

bg.add(radioButton3);

}

public void createAndShowGUI() {

JFrame.setDefaultLookAndFeelDecorated(true);

JFrame frame = new JFrame("Demo

JRadioButton");

frame.setLayout(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON

_CLOSE);

frame.getContentPane().add(label1);

frame.getContentPane().add(radioButton1);

frame.getContentPane().add(radioButton2);

frame.getContentPane().add(radioButton3);

frame.getContentPane().add(label2);

frame.setBounds(0, 0, 300, 200);

frame.setLocationRelativeTo(null);

Page 10: PROGRAM 13-2 import java.awt.*; //PROGRAM 13-4 · import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DemoAppletSwing extends JApplet implements ActionListener

frame.setVisible(true);

}

public void itemStateChanged(ItemEvent event)

{

JRadioButton rb = (JRadioButton)

event.getSource();

label2.setText("Pilihan Anda: " + rb.getText());

}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new

Runnable() {

public void run() {

DemoRadioButton app = new

DemoRadioButton();

app.createAndShowGUI();

}

});

}

}

//PROGRAM 13-11

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class Page1 extends JPanel implements

ActionListener {

private JButton btn;

public Page1() {

btn = new JButton("Info Page #1");

btn.addActionListener(this);

btn.setMnemonic('1');

add(btn);

}

public void actionPerformed(ActionEvent event)

{

if (event.getSource() == btn) {

JOptionPane.showMessageDialog(null,

"Ini adalah page ke-1");

}

}

}

class Page2 extends JPanel {

private JTextField tf;

public Page2() {

tf = new JTextField("Ini adalah contoh page

kedua...", 20);

add(tf);

}

}

class DemoTabbedPane {

public JTabbedPane tp;

public DemoTabbedPane() {

ImageIcon icon = new

ImageIcon("/images/icon.jpg");

tp = new JTabbedPane();

tp.addTab("Page #1", icon, new Page1(), "Page

pertama");

tp.addTab("Page #2", icon, new Page2(), "Page

kedua");

tp.setLocation(0, 0);

tp.setSize(new Dimension(290, 190));

}

Page 11: PROGRAM 13-2 import java.awt.*; //PROGRAM 13-4 · import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DemoAppletSwing extends JApplet implements ActionListener

public void createAndShowGUI() {

JFrame.setDefaultLookAndFeelDecorated(true);

JFrame frame = new JFrame("Demo

JTabbedPane");

frame.setLayout(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON

_CLOSE);

frame.getContentPane().add(tp);

frame.setBounds(0, 0, 300, 200);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new

Runnable() {

public void run() {

DemoTabbedPane app = new

DemoTabbedPane();

app.createAndShowGUI();

}

});

}

}

//PROGRAM 13-13

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class DemoTable {

public JTable table;

public JScrollPane scroller;

public DemoTable() {

final String[] judulKolom =

{"No.", "Nama", "Alamat", "Kota"};

final Object[][] data = {

{"1", "Totok Triwibowo", "Jl. Melati 12",

"Cimahi"},

{"2", "Desi Mekarsari", "Jl. Bunga 333",

"Bandung"},

{"3", "Malik Hendrawan","Jl. Sukamiskin

444", "Bandung"},

{"4", "Wahyu Suhendi", "Jl. Badak 1",

"Sumedang"},

{"5", "Darmanto", "Jl. Bawang 23",

"Brebes"},

{"6", "Hendarto", "Jl. Pemali 32",

"Brebes"},

{"7", "Kiki Sumantro", "Jl. Cipakoma 1",

"Tegal"},

{"8", "Herman Wijanarko", "Jl.

Macanucul 34", "Tegal"},

{"9", "Slamet", "Jl. Pakulaut 99",

"Tegal"},

{"10", "Dewi Susanti", "Jl. Tukul 88",

"Semarang"},

{"11", "Noni Sumantri", "Jl. A Yani 222",

"Solo"},

{"12", "Tedjo Hermanto", "Jl. Duren 6",

"Yogyakarta"},

{"13", "Sri Sumarsih","Jl. Melati 5",

"Surabaya"},

{"14", "Eko Waluyo", "Jl. Batu 4",

"Malang"},

{"15", "Doni Sandi", "Jl. Jeruk 56",

"Malang"}

};

Page 12: PROGRAM 13-2 import java.awt.*; //PROGRAM 13-4 · import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DemoAppletSwing extends JApplet implements ActionListener

table = new JTable(data, judulKolom);

scroller = new JScrollPane(table,

ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_N

EEDED,

ScrollPaneConstants.HORIZONTAL_SCROLLBAR_A

S_NEEDED);

scroller.setLocation(0,0);

scroller.setSize(new Dimension(440, 300));

}

public void createAndShowGUI() {

JFrame.setDefaultLookAndFeelDecorated(true);

JFrame frame = new JFrame("Demo JTable");

frame.setLayout(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON

_CLOSE);

frame.getContentPane().add(scroller);

frame.setBounds(0, 0, 450, 350);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new

Runnable() {

public void run() {

DemoTable app = new DemoTable();

app.createAndShowGUI();

}

});

}

}

//PROGRAM 13-8

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class DemoTextArea {

public JTextArea textArea;

public JScrollPane scroller;

public DemoTextArea() {

textArea = new JTextArea(345,190);

textArea.setText(

"Ini adalah contoh string panjang " +

"(lebih dari satu baris) " +

"yang dimasukkan ke dalam komponen

JTextArea.");

textArea.setLineWrap(true); //di-wrap atau

dipotong

scroller = new JScrollPane(textArea,

ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_N

EEDED,

ScrollPaneConstants.HORIZONTAL_SCROLLBAR_A

S_NEEDED);

scroller.setLocation(0,0);

scroller.setSize(new Dimension(345, 190));

}

public void createAndShowGUI() {

JFrame.setDefaultLookAndFeelDecorated(true);

JFrame frame = new JFrame("Demo

JTextArea");

frame.setLayout(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON

_CLOSE);

Page 13: PROGRAM 13-2 import java.awt.*; //PROGRAM 13-4 · import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DemoAppletSwing extends JApplet implements ActionListener

frame.getContentPane().add(scroller);

frame.setBounds(0, 0, 355, 200);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new

Runnable() {

public void run() {

DemoTextArea app = new DemoTextArea();

app.createAndShowGUI();

}

});

}

}

//PROGRAM 13-7

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class DemoTextField implements ActionListener {

public JLabel label1, label2, label3;

public JTextField textField1, textField2,

textField3;

public JButton button1, button2, button3,

button4;

public DemoTextField() {

label1 = new JLabel("Nilai A");

label1.setLocation(10, 10);

label1.setSize(label1.getPreferredSize());

textField1 = new JTextField(20);

textField1.setLocation(10, 25);

textField1.setSize(textField1.getPreferredSize());

label2 = new JLabel("Nilai B");

label2.setLocation(10, 45);

label2.setSize(label2.getPreferredSize());

textField2 = new JTextField(20);

textField2.setLocation(10, 60);

textField2.setSize(textField2.getPreferredSize());

label3 = new JLabel("Hasil perhitungan antara

A dan B");

label3.setLocation(10, 90);

label3.setSize(label3.getPreferredSize());

textField3 = new JTextField(20);

textField3.setLocation(10, 105);

textField3.setSize(textField3.getPreferredSize());

textField3.setEditable(false);

button1 = new JButton("Tambah");

button1.setLocation(245, 10);

button1.setSize(button1.getPreferredSize());

button1.addActionListener(this);

button1.setMnemonic('T');

button2 = new JButton("Kurang");

button2.setLocation(245, 40);

button2.setSize(button1.getPreferredSize());

button2.addActionListener(this);

button2.setMnemonic('u');

button3 = new JButton("Kali");

button3.setLocation(245, 70);

Page 14: PROGRAM 13-2 import java.awt.*; //PROGRAM 13-4 · import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DemoAppletSwing extends JApplet implements ActionListener

button3.setSize(button1.getPreferredSize());

button3.addActionListener(this);

button3.setMnemonic('K');

button4 = new JButton("Bagi");

button4.setLocation(245, 100);

button4.setSize(button1.getPreferredSize());

button4.addActionListener(this);

button4.setMnemonic('B');

}

public void createAndShowGUI() {

JFrame.setDefaultLookAndFeelDecorated(true);

JFrame frame = new JFrame("Demo

JTextField");

frame.setLayout(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON

_CLOSE);

frame.getContentPane().add(label1);

frame.getContentPane().add(textField1);

frame.getContentPane().add(label2);

frame.getContentPane().add(textField2);

frame.getContentPane().add(label3);

frame.getContentPane().add(textField3);

frame.getContentPane().add(button1);

frame.getContentPane().add(button2);

frame.getContentPane().add(button3);

frame.getContentPane().add(button4);

frame.setBounds(0, 0, 350, 200);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

public void actionPerformed(ActionEvent event)

{

double a=0.0, b=0.0, c=0.0;

try {

a = Double.parseDouble(textField1.getText());

b = Double.parseDouble(textField2.getText());

} catch (NumberFormatException nfe) {

nfe.printStackTrace();

}

JButton btn = (JButton) event.getSource();

if (btn == button1) {

c = a + b;

} else if (btn == button2) {

c = a - b;

} else if (btn == button3) {

c = a * b;

} else {

c = a / b;

}

textField3.setText(new String().valueOf(c));

}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new

Runnable() {

public void run() {

DemoTextField app = new DemoTextField();

app.createAndShowGUI();

}

});

}

Page 15: PROGRAM 13-2 import java.awt.*; //PROGRAM 13-4 · import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DemoAppletSwing extends JApplet implements ActionListener

}

//PROGRAM 13-12

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.tree.*;

import javax.swing.event.*;

class DemoTree implements

TreeSelectionListener {

public JTree tree;

public JScrollPane scroller;

public JTextField tfInfo;

public DemoTree() {

DefaultMutableTreeNode root = null;

DefaultMutableTreeNode nodeLevel1 = null;

DefaultMutableTreeNode nodeLevel2 = null;

root = new DefaultMutableTreeNode("SDDN");

nodeLevel1 = new

DefaultMutableTreeNode("Betha Sidik");

root.add(nodeLevel1);

nodeLevel2 = new

DefaultMutableTreeNode("Pemrograman

HTML");

nodeLevel1.add(nodeLevel2);

nodeLevel2 = new

DefaultMutableTreeNode("Pemrograman PHP");

nodeLevel1.add(nodeLevel2);

nodeLevel2 = new

DefaultMutableTreeNode("Linux/Unix");

nodeLevel1.add(nodeLevel2);

nodeLevel1 = new

DefaultMutableTreeNode("KS Bahri");

root.add(nodeLevel1);

nodeLevel2 = new

DefaultMutableTreeNode("Pemrograman

Delphi");

nodeLevel1.add(nodeLevel2);

nodeLevel1 = new

DefaultMutableTreeNode("Ruslan Nuryadin");

root.add(nodeLevel1);

nodeLevel2 = new

DefaultMutableTreeNode("Co-Linux");

nodeLevel1.add(nodeLevel2);

nodeLevel2 = new

DefaultMutableTreeNode("MapServer");

nodeLevel1.add(nodeLevel2);

nodeLevel1 = new

DefaultMutableTreeNode("Budi Raharjo");

root.add(nodeLevel1);

nodeLevel2 = new

DefaultMutableTreeNode("Oracle");

nodeLevel1.add(nodeLevel2);

nodeLevel2 = new

DefaultMutableTreeNode("C++");

nodeLevel1.add(nodeLevel2);

nodeLevel2 = new

DefaultMutableTreeNode("Pascal");

nodeLevel1.add(nodeLevel2);

nodeLevel2 = new

DefaultMutableTreeNode("Java");

nodeLevel1.add(nodeLevel2);

tree = new JTree(root);

tree.getSelectionModel().setSelectionMode

Page 16: PROGRAM 13-2 import java.awt.*; //PROGRAM 13-4 · import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DemoAppletSwing extends JApplet implements ActionListener

(TreeSelectionModel.SINGLE_TREE_SELECTION);

tree.addTreeSelectionListener(this);

scroller = new JScrollPane(tree,

ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_N

EEDED,

ScrollPaneConstants.HORIZONTAL_SCROLLBAR_A

S_NEEDED);

scroller.setLocation(0,0);

scroller.setSize(new Dimension(440, 300));

tfInfo = new JTextField(100);

tfInfo.setLocation(0, 300);

tfInfo.setSize(tfInfo.getPreferredSize());

}

public void createAndShowGUI() {

JFrame.setDefaultLookAndFeelDecorated(true);

JFrame frame = new JFrame("Demo JTree");

frame.setLayout(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON

_CLOSE);

frame.getContentPane().add(scroller);

frame.getContentPane().add(tfInfo);

frame.setBounds(0, 0, 450, 350);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

public void valueChanged(TreeSelectionEvent

event) {

DefaultMutableTreeNode node =

(DefaultMutableTreeNode)

tree.getLastSelectedPathComponent();

if (node == null) {

return;

}

//if (node.isLeaf()) { // memeriksa node level

akhir

tfInfo.setText(node.toString());

//}

}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new

Runnable() {

public void run() {

DemoTree app = new DemoTree();

app.createAndShowGUI();

}

});

}

}

//PROGRAM 13-1

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class MySwingApplication extends JFrame

implements ActionListener {

public JLabel label;

public JButton button;

private int numClicks = 0;

public MySwingApplication(String title) {

super(title);

Page 17: PROGRAM 13-2 import java.awt.*; //PROGRAM 13-4 · import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DemoAppletSwing extends JApplet implements ActionListener

setBounds(0, 0, 300, 170);

getContentPane().setLayout(null);

setResizable(false);

setDefaultCloseOperation(EXIT_ON_CLOSE);

label = new JLabel("Jumlah klik yang dilakukan:

0");

label.setLocation(60, 40);

label.setSize(label.getPreferredSize());

button = new JButton("Klik aku!!");

button.setLocation(60, 80);

button.setSize(label.getPreferredSize());

button.addActionListener(this);

getContentPane().add(label);

getContentPane().add(button);

setVisible(true);

}

public void actionPerformed(ActionEvent event)

{

if (event.getSource() == button) {

label.setText("Jumlah klik yang dilakukan: " +

(++numClicks));

}

}

public static void main(String[] args) {

MySwingApplication app =

new MySwingApplication("Contoh Event-

Handling");

}

}