PEMROGRAMAN JAVA - Teknik Informatika

advertisement
PEMROGRAMAN JAVA |Software Engineering Group
JAVA Pertemuan 4 Tugas 2
Membahas Event Handling pada Tombol (Button)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class EventDemo {
JLabel label1;
EventDemo(){
//create a new JFrame container.
1
Teknik Informatika Universitas Nasional |Quiz of Software Engineering Group
PEMROGRAMAN JAVA |Software Engineering Group
JFrame jfrm=new JFrame(" Sebuah contoh");
//specify FlowLayout for the layout manager.
jfrm.setLayout(new FlowLayout());
//give the frame an initial size.
jfrm.setSize(220, 100);
//terminate the program ehwn the user closes the app
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//make two buttons
ImageIcon save=new ImageIcon("C:/Save.gif");
JButton button1= new JButton(save);
ImageIcon linux=new ImageIcon("C:/Linux.gif");
JButton button2= new JButton(linux);
//add action listener for alif
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
label1.setText("Save was pressed");
}
});
2
Teknik Informatika Universitas Nasional |Quiz of Software Engineering Group
PEMROGRAMAN JAVA |Software Engineering Group
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
label1.setText("Linux was pressed");
}
});
jfrm.add(button1);
jfrm.add(button2);
label1= new JLabel("Press a button");
jfrm.add(label1);
jfrm.setVisible(true);
int windowWidth=400;
int windowHeight= 150;
jfrm.setBounds(50, 100, windowWidth, windowHeight);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new EventDemo();
}
}
3
Teknik Informatika Universitas Nasional |Quiz of Software Engineering Group
PEMROGRAMAN JAVA |Software Engineering Group
);
}
}
4
Teknik Informatika Universitas Nasional |Quiz of Software Engineering Group
Download