Live Traffic

Our Story

Ceremony

Search

Just Married

Swing: Button dengan Icon

by - February 04, 2010

JButton button1;
JButton button2;
public CreateButtonIcon(){
super();
init();
}
private void init(){
this.setSize(400,400);
this.setTitle("Membuat Button Yang Memiliki Icon");
//mengatur layout
FlowLayout alayout = new FlowLayout(FlowLayout.LEFT,1,1);
this.getContentPane().setLayout(new FlowLayout());
//membuat button dengan method createButton- lihat
//di method createButton
button1 = createButton("icons/more.gif","test1");
button1.setMaximumSize(new Dimension(10,10) );
button2 = createButton("icons/hurufA.gif","test2");
//menambahkan button pada container.
this.getContentPane().add(button1);
this.getContentPane().add(button2);

}
//method membuat button, mengurangi peng-kodean yang berulang-ulang
public JButton createButton(String respath,String tooltip){
//mengambil resource path dari file icon
//format icon dalam bentuk raster (gif,jpeg,tif,bmp)
String filepath=this.getClass().getClassLoader().
getResource(respath).getPath();
//mendefinisikan icon dengan interface Icon dan subclassnya ImageIcon;
Icon aimgIcon = new ImageIcon(filepath);
JButton button = new JButton(aimgIcon);
//menambahkan tooltip pada button;
button.setToolTipText(tooltip);
return button;
}
public static void main(String[] args) {
CreateButtonIcon aframe = new CreateButtonIcon();
aframe.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);}
});
aframe.pack();
aframe.show();
}

sumber: Java Programming Tips

You May Also Like

0 comments