//PROGRAM 13-2 import java.awt.*;
//PROGRAM 13-4
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class DemoAppletSwing extends JApplet
import javax.swing.*;
implements ActionListener {
class DemoButton implements ActionListener {
public JTextField textField;
public JLabel label;
public JButton button;
public JButton btn1, btn2;
public void init() {
public DemoButton() { ImageIcon icon = new
// mendapatkan container
ImageIcon("images/tunjuk.jpg"); Container cont = getContentPane(); label = new JLabel("Belum ada button yang cont.setLayout(new FlowLayout());
diklik");
// menambahkan komponen JTextField
label.setLocation(60, 40);
textField = new JTextField(25);
label.setSize(label.getPreferredSize());
cont.add(textField, BorderLayout.CENTER);
btn1 = new JButton("Button #1");
// menambahkan komponen JButton
btn1.setLocation(40, 70);
button = new JButton("Tombol Applet");
btn1.setSize(btn1.getPreferredSize());
button.addActionListener(this);
btn1.addActionListener(this);
cont.add(button, BorderLayout.CENTER);
btn2 = new JButton("Button #2", icon);
}
btn2.setLocation(135, 70);
// menangani event klik pada komponen
btn2.setSize(btn2.getPreferredSize());
JButton btn2.addActionListener(this); public void actionPerformed(ActionEvent event) {
} if (event.getSource() == button) {
textField.setText("Anda telah melakukan klik " + "pada tombol Applet"); }
public void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("Demo JButton"); frame.setLayout(null);
} }
frame.setDefaultCloseOperation(JFrame.EXIT_ON _CLOSE);
frame.getContentPane().add(label,
import javax.swing.*;
BorderLayout.CENTER); class DemoCheckBox implements ItemListener { frame.getContentPane().add(btn1); public JLabel label1; frame.getContentPane().add(btn2); frame.setBounds(0, 0, 300, 200);
public JCheckBox checkBox1, checkBox2, checkBox3;
frame.setLocationRelativeTo(null);
private JFrame frame;
frame.setVisible(true);
public DemoCheckBox() {
}
label1 = new JLabel("Pilihan:");
public void actionPerformed(ActionEvent event)
label1.setLocation(10, 10);
{ label1.setSize(label1.getPreferredSize()); if (event.getSource() == btn1) { checkBox1 = new JCheckBox("C", true); label.setText("Button #1 telah diklik"); }
checkBox1.setLocation(10, 25);
else if (event.getSource() == btn2) {
checkBox1.addItemListener(this);
label.setText("Button #2 telah diklik"); checkBox1.setSize(checkBox1 .getPreferredSize()); } checkBox2 = new JCheckBox("C++", true); } checkBox2.setLocation(10, 50); public static void main(String[] args) { checkBox2.addItemListener(this); javax.swing.SwingUtilities.invokeLater(new
checkBox2.setSize(checkBox2 .getPreferredSize());
Runnable() {
checkBox3 = new JCheckBox("Java", true);
public void run() {
checkBox3.setLocation(10, 75);
DemoFrame app = new DemoFrame(); app.createAndShowGUI(); } }); } }
checkBox3.addItemListener(this); checkBox3.setSize(checkBox3 .getPreferredSize()); } public void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true); frame = new JFrame("Demo JCheckBox"); frame.setLayout(null);
//PROGRAM 13-5 import java.awt.*; import java.awt.event.*;
frame.setDefaultCloseOperation(JFrame.EXIT_ON _CLOSE); frame.getContentPane().add(label1);
frame.getContentPane().add(checkBox1); frame.getContentPane().add(checkBox2);
//PROGRAM 13-9
frame.getContentPane().add(checkBox3);
import java.awt.*;
frame.setBounds(0, 0, 300, 200);
import java.awt.event.*;
frame.setLocationRelativeTo(null);
import javax.swing.*;
frame.setVisible(true);
class DemoComboBox implements ActionListener {
} public JLabel labelNama, labelLahir,
public void itemStateChanged(ItemEvent event) {
labelTanggal, labelBulan, labelTahun; JCheckBox cb = (JCheckBox) event.getSource();
public JTextField tfNama, tfInfo;
if (event.getStateChange() == ItemEvent.SELECTED) {
public JComboBox comboTanggal, comboBulan, comboTahun;
JOptionPane.showMessageDialog(frame,
public JButton btnProses;
"Anda telah memilih: " + cb.getText());
public DemoComboBox() { String s1 = "Nama:"; labelNama = new JLabel(s1);
"Anda telah telah melepas pilihan: " + cb.getText());
labelNama.setLocation(10, 10); labelNama.setSize(labelNama.getPreferredSize())
}
; } public static void main(String[] args) {
tfNama = new JTextField(25); tfNama.setLocation(10, 30);
javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { DemoCheckBox app = new DemoCheckBox(); app.createAndShowGUI(); } }); } }
tfNama.setSize(tfNama.getPreferredSize()); String s2 = "Tanggal Lahir:"; labelLahir = new JLabel(s2); labelLahir.setLocation(10, 55); labelLahir.setSize(labelLahir.getPreferredSize()); labelTanggal = new JLabel("Hari ke-"); labelTanggal.setLocation(35, 75);
comboTahun.setSize(labelLahir .getPreferredSize() labelTanggal.setSize(labelTanggal.getPreferredSiz e());
); for (int i=1960; i