~:Catatan Belajar MazHel:~

nyoba ActionListener As Inner Class bro..

Posted in java by m42h31 on 20 Juni 2008

// Percobaan satu Geser Frame ke kanan,kiri, atas, dan bawah

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Tombol01 extends JFrame{
private Container con=getContentPane();
private JButton ba=new JButton(“Add”);
public Tombol01(String t,int w,int h,int x,int y){
super(t);
setSize(w,h);
setLocation(x,y);
con.setLayout(new BorderLayout());
con.add(ba,BorderLayout.NORTH);
ActionListener bl=new ActionListener(){ //inner class

JButton lf=new JButton(“Left”);
JButton rg=new JButton(“Right”);
JButton ct=new JButton(“Center”);
JButton up=new JButton(“up”);
JButton dw=new JButton(“down”);
public void actionPerformed(ActionEvent e) {
if (e.getSource()==ba) {
up.addActionListener(this);
dw.addActionListener(this);
lf.addActionListener(this);
rg.addActionListener(this);
ct.addActionListener(this);

con.remove(ba);
con.add(lf,BorderLayout.WEST);
con.add(rg,BorderLayout.EAST);
con.add(ct,BorderLayout.CENTER);
con.add(up,BorderLayout.NORTH);
con.add(dw,BorderLayout.SOUTH);

pack();
repaint();
}
else if (e.getSource()==lf)
{
setLocation(getX()-50,getY()); //geser ke kiri
}
else if (e.getSource()==rg)
{
setLocation(getX()+50,getY()); //geser ke kanan
}
else if (e.getSource()==up)
{
setLocation(getX(),getY()-50); //geser ke atas
}
else if (e.getSource()==dw)
{
setLocation(getX(),getY()+50); //geser ke bawah
}
else setLocationRelativeTo(null); //kembali ke tengah

}
};

ba.addActionListener(bl);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String []args){
Tombol01 bt=new Tombol01(“Tombol Bergeser”,300,450,450,450);

}
}

// Percobaan dua Remove and Restore Button

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Tombol03 extends JFrame{
private Container con=getContentPane();
private JButton ba=new JButton(“Tambah”);
public Tombol03(String t,int w,int h,int x,int y){
super(t);
setSize(w,h);
setLocation(x,y);
con.setLayout(new BorderLayout());
con.add(ba,BorderLayout.NORTH);
ActionListener bl=new ActionListener(){  //inner class
JButton be=new JButton(“Keluar”);
JButton tb=new JButton(“Kembalikan”);
JButton rm=new JButton(“Hilangkan”);
JTextField tf=new JTextField(13);

public void actionPerformed(ActionEvent e) {

if (e.getSource()==ba) {

be.addActionListener(this);
tb.addActionListener(this);
rm.addActionListener(this);

con.add(be,BorderLayout.SOUTH);
con.add(tb,BorderLayout.WEST);
con.add(rm,BorderLayout.EAST);
con.add(tf,BorderLayout.CENTER);
tf.setText(“Tampilkan”);
pack();
repaint();

}
else if (e.getSource()==rm)
{
con.remove(rm); //Button Remove
con.remove(be);
tf.setText(“Button Removed”);
pack();
repaint();

}

else if (e.getSource()==tb)
{
con.add(be,BorderLayout.SOUTH); //Button Restored
con.add(rm,BorderLayout.EAST);
tf.setText(“Tampil lagi”);
pack();
repaint();

/*
e.setSource(ba); //bisa jg pake ini, perhatikan bedanya..hehehe..
*/
}

else

System.exit(1);

}

};
ba.addActionListener(bl);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String []args){
Tombol03 bt1=new Tombol03(“Tombol Removed”,800,450,300,200);

}
}

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: