1. Source Code :
package
pendahuluan8;
import
java.awt.*;
public
class nomer1 extends Frame {
Button
bAktif= new Button();
Button
btdkAktif= new Button();
List
list = new List(2, true);
Choice
chooser = new Choice();
public
nomer1(){
this.setLayout(new
FlowLayout());
this.add(bAktif,
null);
this.add(btdkAktif,
null);
bAktif.setLabel(“Tombol
aktif”);
btdkAktif.setLabel(“Tombol
tidak aktif”);
btdkAktif.setEnabled(false);
list.add(“tombol
aktif”);
list.add(“tombol
tidak aktif”);
this.add(list);
chooser.add(“tombol
aktif”);
chooser.add(“tombol
tidak aktif”);
this.add(chooser);
}
public
static void main (String[]args){
nomer1
tombol=new nomer1();
tombol.pack();
tombol.setTitle(“Tombol
Coba AWT”);
tombol.setVisible(true);
}}
1. Source Code :
package
pendahuluan8;
import
javax.swing.*;
public
class nomer2 extends JFrame {
public
nomer2 (){
setVisible(true);
setSize(200,100);
setTitle(“frame”);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(100,
50);}
public
static void main(String[] args) {
nomer2
msf = new nomer2();
}}
1. Flow Layout
Source Code :
package
pendahuluan8;
import
java.awt.*;
import
java.awt.event.*;
public
class nomer3flowlayout extends Frame implements ActionListener {
TextField
txtPanjang, txtLebar, txtHasil;
Button
btnHitung;
Label
lblPanjang, lblLebar, lblHasil;
double
hasil;
public
nomer3flowlayout(String title) {
super(title);
this.addWindowListener(new
WindowAdapter() {
public
void window(WindowEvent e) {
dispose();
System.exit(1);
}});
Panel
panelWest = new Panel();
Panel
panelCenter = new Panel();
panelWest.setLayout(new
GridLayout(4, 1));
panelWest.add(lblPanjang
= new Label(“Panjang: “));
panelWest.add(lblLebar
= new Label(“Lebar: “));
panelWest.add(new
Label());
panelWest.add(lblHasil
= new Label(“Luas: “));
panelCenter.setLayout(new
GridLayout(4, 1));
panelCenter.add(txtPanjang
= new TextField(“”));
panelCenter.add(txtLebar
= new TextField());
panelCenter.add(btnHitung
= new Button(“Hitung”));
panelCenter.add(txtHasil
= new TextField());
btnHitung.addActionListener(this);
add(panelWest,
BorderLayout.WEST);
add(panelCenter,
BorderLayout.CENTER);
setSize(300,
150);
setVisible(true);
}
public
void actionPerformed(ActionEvent e) {
if
(e.getSource().equals(btnHitung)) {
hasil
= Double.parseDouble(txtPanjang.getText()) *
Double.parseDouble(txtLebar.getText());
txtHasil.setText(“”+hasil);
}}
public
static void main(String[] args) {
nomer3flowlayout
frm = new nomer3flowlayout(“Luas Persegi Panjang”);
}}
Border
Layout
Source Code :
package
pendahuluan8;
import
java.awt.*;
import
javax.swing.*;
public
class nomer3borderlayout extends JFrame {
public
nomer3borderlayout (String title) {
super(title);
Container
contentPane = getContentPane();
contentPane.setLayout(new
BorderLayout(2,2));
contentPane.add(BorderLayout.NORTH,
new JButton(“Utara”));
contentPane.add(BorderLayout.SOUTH,
new JButton(“Selatan”));
contentPane.add(BorderLayout.EAST,
new JButton(“Timur”));
contentPane.add(BorderLayout.WEST,
new JButton(“Barat”));
contentPane.add(BorderLayout.CENTER,
new JButton(“Tengah”));
}
public
static void main(String args[]) {
nomer3borderlayout
frame = new nomer3borderlayout(“Arah Mata Angin”);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(500,
300);
frame.setVisible(true);
}}
1. Menampilkan gambar
Source Code :
package
pendahuluan8;
import
java.awt.*;
import
java.awt.event.*;
import
javax.swing.*;
import
java.io.*;
public
class nomer4 extends JFrame{
String
gambar[] ={“No picture”,”a.jpg”, “b.jpg”, “c.jpg”, “bunga.gif”};
Icon
icons[] = { new ImageIcon( gambar[0] ),new ImageIcon( gambar[1] ), new
ImageIcon( gambar[2] ),new ImageIcon( gambar[3] ),new ImageIcon( gambar[4] ) };
JLabel
label;
JComboBox
rafles;
public
static void main(String[] args){
nomer4
program = new nomer4();}
public
nomer4(){
super(“Menampilkan
Gambar”);
Container
container = getContentPane();
container.setLayout(new
FlowLayout(FlowLayout.CENTER, 0, 0));
rafles
= new JComboBox( gambar );
rafles.setForeground(Color.BLUE);
rafles.setMaximumRowCount(2);
rafles.addItemListener(
new
ItemListener() {
public
void itemStateChanged(ItemEvent event ){
if(
event.getStateChange() ==ItemEvent.SELECTED)
label.setIcon(
icons[ rafles.getSelectedIndex() ] );
}});
container.add(“North”,rafles);
label
= new JLabel( icons[0] );
container.add(
label );
getContentPane().setBackground(
Color.WHITE);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,400);
setLocation(100,50);
setVisible(true);
}}
Memberi
warna
Source Code :
package
pendahuluan8;
import
java.awt.*;
public
class nomer4ke2 extends Frame {
Button
merah= new Button(“Tombol merah”);
Button
hijau= new Button(“Tombol hijau”);
Button
biru= new Button(“Tombol biru”);
public
nomer4ke2(){
this.setLayout(new
FlowLayout());
this.add(merah,
null);
this.add(hijau,
null);
this.add(biru,
null);
merah.setBackground(Color.red);
hijau.setBackground(Color.green);
biru.setBackground(Color.blue);
}
public
static void main(String args []){
nomer4ke2
tombol=new nomer4ke2();
tombol.pack();
tombol.setTitle(“Tombol
Coba AWT”);
tombol.setVisible(true);
}}