Tugas 1.
Membuat tampilan seperti berikut :
Kode program sebagai berikut :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class CheckBoxHandling extends JFrame
{
JMenuBar menuBar;
JMenu file,prodi;
JMenuItem about,exit,mi,ka;
private JTextField teks;
private JPanel panel;
JCheckBox cBMI = new JCheckBox ("MI");
JCheckBox cBKA = new JCheckBox ("KA");
public CheckBoxHandling() {
setTitle("Coba
CheckBox Handling");
teks = new
JTextField("Program studi yang Anda minati");
teks.setHorizontalAlignment(SwingConstants.CENTER);
panel = new
JPanel(new FlowLayout());
add(cBMI);
add(cBKA);
setLayout(new
BorderLayout());
add(teks,
"North");
add(panel,"West");
setTitle("Menu");
setSize(200,200);
file = new
JMenu("File");
file.setMnemonic('F');
prodi = new
JMenu("Prodi");
prodi.setMnemonic('P');
about = new
JMenuItem("About");
about.setMnemonic('A');
exit = new
JMenuItem("Exit");
exit.setMnemonic('E');
mi = new
JMenuItem("MI");
mi.setMnemonic('M');
ka = new JMenuItem("KA");
ka.setMnemonic('K');
menuBar = new
JMenuBar();
setJMenuBar(menuBar);
menuBar.add(file);
file.add(about);
file.add(exit);
menuBar.add(prodi);
prodi.add(mi);
prodi.add(ka);
cBMI.setBounds(130,85,100,20);
cBKA.setBounds(230,85,150,20);
pack();
setVisible(true);
setLocation(200,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
public class Tugas {
public static void
main (String [] args) {
new
CheckBoxHandling();
}
}
Tugas2
Membuat tampilan seperti berikut : gmbr1,2,3
Kode Program sebagai berikut :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class CheckBoxHandling extends JFrame implements ItemListener {
private JTextField teks;
private JPanel panel;
private JCheckBox chk1,chk2, chk3;
public CheckBoxHandling() {
setTitle("Coba
CheckBox Handling");
teks = new
JTextField("Jenis Kelamin : Pilihan");
teks.setHorizontalAlignment(SwingConstants.CENTER);
panel = new
JPanel(new FlowLayout());
chk1= new
JCheckBox("Pria");
chk2= new
JCheckBox("Wanita");
chk3= new
JCheckBox("Banci");
ButtonGroup group
= new ButtonGroup();
group.add(chk1);
group.add(chk2);
group.add(chk3);
chk1.addItemListener(this);
chk2.addItemListener(this);
chk3.addItemListener(this);
setLayout(new
BorderLayout());
add(teks,
"North");
add(panel,"West");
panel.add(chk1);
panel.add(chk2);
panel.add(chk3);
pack();
setVisible(true);
setLocation(200,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void itemStateChanged(ItemEvent event) {
if(event.getSource()= =chk1)
{
if(event.getStateChange() = =ItemEvent.SELECTED)
teks.setText("Jenis Kelamin : Pria");
}
if(event.getSource()= =chk2) {
if(event.getStateChange() = =ItemEvent.SELECTED)
teks.setText("Jenis Kelamin : Wanita");
}
if(event.getSource()= =chk3) {
if(event.getStateChange() = =ItemEvent.SELECTED)
teks.setText("Jenis Kelamin : Banci");
}
}
}
public class EventCheckBox {
public static void
main (String [] args) {
new
CheckBoxHandling();
}
}
SEKIAN...........
Tidak ada komentar:
Posting Komentar