Dynamic application, run to test buttons and mouse rollover effect on button. Resize application to see flow layout at work.
ButtonTest.java// Fig. 12.16: ButtonTest.java
// Command buttons and action events.
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
class ButtonFrame extends JFrame
{
private final JButton plainJButton; // button with just text
private final JButton fancyJButton; // button with icons
// ButtonFrame adds JButtons to JFrame
public ButtonFrame()
{
super("Testing Buttons");
setLayout(new FlowLayout());
plainJButton = new JButton("Plain Button"); // button with text
add(plainJButton); // add plainJButton to JFrame
Icon bug1 = new ImageIcon(getClass().getResource("bug1.gif"));
Icon bug2 = new ImageIcon(getClass().getResource("bug2.gif"));
fancyJButton = new JButton("Fancy Button", bug1); // set image
fancyJButton.setRolloverIcon(bug2); // set rollover image
add(fancyJButton); // add fancyJButton to JFrame
// create new ButtonHandler for button event handling
ButtonHandler handler = new ButtonHandler();
fancyJButton.addActionListener(handler);
plainJButton.addActionListener(handler);
}
// inner class for button event handling
private class ButtonHandler implements ActionListener
{
// handle button event
@Override
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(ButtonFrame.this, String.format(
"You pressed: %s", event.getActionCommand()));
}
}
} // end class ButtonFrame
public class ButtonTest
{
public static void main(String[] args)
{
ButtonFrame buttonFrame = new ButtonFrame();
buttonFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttonFrame.setSize(280, 120);
buttonFrame.setVisible(true);
}
} // end class ButtonTest
Maintained by John Loomis, updated Thu Feb 09 21:05:53 2017