DEMO ACTION;
Contoh program demo action;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class DemoAction extends JFrame {
private JButton BTNsimpan =new JButton("Simpan"),
BTNclear =new JButton("Clear");
private JLabel LBLsimpan =new JLabel("jihan nabela"),
LBLclear =new JLabel("Data Anda Terhapus");
DemoAction(){
super("Demo Action Listener");
JPanel Panel1 =new JPanel();
Panel1.setLayout(new FlowLayout());
Panel1.add(BTNsimpan);
Panel1.add(BTNclear);
getContentPane().add(Panel1);
setSize(200,150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
BTNclear.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent evn){
BTNsimpan.setVisible(false);
BTNclear.setVisible(false);
LBLclear.setVisible(true);
Panel1.add(LBLclear);
BTNsimpan.setEnabled(false);
}
});
BTNsimpan.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent evn){
BTNsimpan.setVisible(false);
BTNclear.setVisible(false);
LBLsimpan.setVisible(true);
Panel1.add(LBLsimpan);
BTNclear.setEnabled(false);
}
});
setVisible(true);
}
public static void main(String[] args){
DemoAction Demoaksi =new DemoAction();
} }
maka ketika program di Run akan menampilkan;
DEMO KEY;
Contoh program demo key;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DemoKey extends JFrame implements KeyListener{
private String baris1="";
private JTextArea textArea;
public DemoKey(){
super("Mencoba Key Event");
textArea =new JTextArea (10,15);
textArea.setText("Tekan sembarang tombol di keyboard...");
textArea.setEnabled(false);
textArea.setDisabledTextColor(Color.BLACK);
getContentPane().add(textArea);
addKeyListener (this);
setSize (300,150);
setLocationRelativeTo(null);
setVisible(true);
}
public void keyPressed (KeyEvent e){
textArea.setText("Tombol yang ditekan : "+
e.getKeyText(e.getKeyCode()));
}
public void keyReleased (KeyEvent e){
textArea.setText("Tombol yang dilepas : "+
e.getKeyText(e.getKeyCode()));
}
public void keyTyped (KeyEvent e){
textArea.setText("Tombol yang ditulis : "+
e.getKeyChar());
}
public static void main (String args[]){
DemoKey test =new DemoKey();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
maka ketika program di Run akan menampilkan;
DEMO ACTIONKEY;
contoh program demo ActionKey(gabungan);
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DemoAction2 extends JFrame{
private String baris1="";
private JTextArea textArea;
private JButton BTNsimpan =new JButton("simpan"),
BTNclear =new JButton("Clear");
private JLabel LBLsimpan =new JLabel("Jihan nabela"),
LBLclear =new JLabel("Data telah terhapus");
DemoAction2(){
super("Gabungan demo Action demo key");
textArea =new JTextArea (10,15);
textArea.setText("Silahkan masukkan nama anda....");
textArea.setEnabled(false);
textArea.setDisabledTextColor(Color.BLACK);
getContentPane().add(textArea);
setSize (300,150);
setLocationRelativeTo(null);
setVisible(true);
JPanel Panel1 =new JPanel();
Panel1.setLayout(new FlowLayout());
Panel1.add(BTNsimpan);
Panel1.add(BTNclear);
getContentPane().add(Panel1);
setSize(200,150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
BTNclear.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent evn){
BTNsimpan.setVisible(false);
BTNclear.setVisible(false);
LBLclear.setVisible(true);
Panel1.add(LBLclear);
BTNsimpan.setEnabled(false);
}
});
BTNsimpan.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent evn){
BTNsimpan.setVisible(false);
BTNclear.setVisible(false);
LBLsimpan.setVisible(true);
Panel1.add(LBLsimpan);
BTNclear.setEnabled(false);
}
});
setVisible(true);
}
public static void main(String[]args){
DemoAction2 Demoaksi =new DemoAction2();
}
}
maka jika program dirun akan menampilkan;