lukmanunindra

Home » Pemograman Visual » Pertemuan 5 Visual

Pertemuan 5 Visual

Contoh Penggunaan Event
package pertemuan5;

import javax.swing.*;
import java.awt.event.*;

class segitiga extends JFrame implements ActionListener{
JLabel l1=new JLabel(“Masukan Alas :”);
JLabel l3=new JLabel(“Masukan Tinggi :”);
JLabel l2=new JLabel();
JTextField t1=new JTextField();
JTextField t2=new JTextField();
JButton b1=new JButton(“Hitung”);

segitiga(){
l1.setBounds(10,10,100,30);
l3.setBounds(10,70,100,30);
t1.setBounds(10,50,250,30);
t2.setBounds(10,100,250,30);
b1.setBounds(50,130,80,30);
l2.setBounds(10,150,250,30);

add(l1);
add(l2);
add(l3);
add(t1);
add(t2);
add(b1);
b1.addActionListener(this);

setSize(280,250);
setLayout(null);
setVisible(true);
}

public void actionPerformed(ActionEvent e){
if (e.getSource()==b1){
int a= Integer.parseInt(t1.getText());
int t= Integer.parseInt(t2.getText());
String z = String.valueOf(a * t/2);
l2.setText(z);
}
}

public static void main (String [] args){
segitiga f3=new segitiga();
}
}

Contoh Penerapan Event

 

package pertemuan5;

import javax.swing.*;
import java.awt.event.*;

class fun3 extends JFrame implements ActionListener{
JLabel l1=new JLabel(“Tuliskan Teks :”);
JLabel l2=new JLabel();
JTextField t1=new JTextField();
JButton b1=new JButton(“Tampil”);

fun3(){
l1.setBounds(10,10,100,30);
t1.setBounds(10,50,250,30);
b1.setBounds(50,90,80,30);
l2.setBounds(10,130,250,30);

add(l1);
add(l2);
add(t1);
add(b1);
b1.addActionListener(this);

setSize(280,250);
setLayout(null);
setVisible(true);
}

public void actionPerformed(ActionEvent e){
if (e.getSource()==b1){
String isi=t1.getText();
l2.setText(isi);
}
}

public static void main (String [] args){
fun3 f3=new fun3();
}
}

Advertisements
%d bloggers like this: