Selasa, 14 Agustus 2012
Program Button
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class DemoButton implements ActionListener {
public JLabel label;
public JButton btn1, btn2;
public DemoButton() {
ImageIcon icon = new ImageIcon("images/tunjuk.jpg");
label = new JLabel("Belum ada button yang diklik");
label.setLocation(60, 40);
label.setSize(label.getPreferredSize());
btn1 = new JButton("Button #1");
btn1.setLocation(40, 70);
btn1.setSize(btn1.getPreferredSize());
btn1.addActionListener(this);
btn2 = new JButton("Button #2", icon);
btn2.setLocation(135, 70);
btn2.setSize(btn2.getPreferredSize());
btn2.addActionListener(this);
}
public void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Demo JButton");
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.getContentPane().add(btn1);
frame.getContentPane().add(btn2);
frame.setBounds(0, 0, 300, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == btn1) {
label.setText("Button #1 telah diklik");
} else if (event.getSource() == btn2) {
label.setText("Button #2 telah diklik");
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
DemoFrame app = new DemoFrame();
app.createAndShowGUI();
}
});
}
}
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar