Buttons and Labels - Con't
Swing buttons can have an image in addition to a label. The
JButton class includes constructors that accept an
Icon object, which knows how to draw itself. You can
create buttons with captions, images, or both. A handy class
called ImageIcon takes care of loading an image for
you and can be used to easily add an image to a button. The
following example shows how this works:
//file: PictureButton.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PictureButton extends JFrame {
public PictureButton( ) {
super("PictureButton v1.0");
setSize(200, 200);
setLocation(200, 200);
Icon icon = new ImageIcon("rhino.gif");
JButton button = new JButton(icon);
button.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent ae) {
System.out.println("Urp!");
}
});
Container content = getContentPane( );
content.setLayout(new FlowLayout( ));
content.add(button);
}
public static void main(String[] args) {
JFrame f = new PictureButton( );
f.addWindowListener(new WindowAdapter( ) {
public void windowClosing(WindowEvent we) { System.exit(0); }
});
f.setVisible(true);
}
}
The example creates an ImageIcon from the
rhino.gif file. Then a JButton is created
from the ImageIcon. The whole thing is displayed in
a JFrame. This example also shows the idiom of using
an anonymous inner class as an ActionListener.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.