04 Juli 2009

Pemrograman Object Oriented-Java

Dosen : Dr. Rufman Iman A, M.Kom.

//Contoh Program membuat event pada button

import java.awt.*;
import java.awt.event. *;


public class event1 {
private Frame fr1;
private Button btn1;
private Button btn2;
private Button btn3;
private Label lbl1;
private TextField text1;

public event1() {
fr1 = new Frame("Tugas OOP - Java Programming");
btn1 = new Button("BIRU");
btn2 = new Button("HIJAU");
btn3 = new Button("MERAH");
lbl1 = new Label("Label1" );
}

public void launchFrame( ){
btn1.addActionListener (new ActionListener( ){

public void actionPerformed( ActionEvent e) {
lbl1.setText("Tulisan Ini Berwarna BIRU");
lbl1.setForeground(Color.blue);
lbl1.getText();
}
});
btn2.addActionListener (new ActionListener( ){
public void actionPerformed( ActionEvent e) {
lbl1.setText("Tulisan ini berwarna HIJAU");
lbl1.setForeground(Color.GREEN);
lbl1.getText();
}
});

btn3.addActionListener (new ActionListener( ){
public void actionPerformed( ActionEvent e) {
lbl1.setText("Tulisan ini berwarna MERAH");
lbl1.setForeground(Color.RED);
lbl1.getText();
}
});

btn1.setSize(50, 30);
btn2.setSize(50, 30);
btn3.setSize(50, 30);
lbl1.setSize(50, 30);

fr1.add(btn1).setBounds( 100, 50, 100, 50);
fr1.add(btn2).setBounds( 220, 50, 100, 50);
fr1.add(btn3).setBounds( 340, 50, 100, 50);
fr1.add(lbl1).setBounds( 360, 50, 100, 50);

fr1.pack();
fr1.setSize(500, 300);
fr1.setVisible( true);
}

public static void main(String[ ] args){
event1 guiApp = new event1() ;
guiApp.launchFrame( );
}
}

Output :

Tidak ada komentar:

Posting Komentar