AWTandSwing

Gif label2 new jlabel label with text and icon bug

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: s Icon bug = new ImageIcon( "bug1.gif" ); label2 = new JLabel( "Label with text and icon", bug, SwingConstants.LEFT ); label2.setToolTipText( "This is label2" ); c.add( label2 ); Set the tool tip text, and attach component to Container c. Create a new ImageIcon (assumed to be and in same directory as program). Set ImageIcon and alignment of text in JLabel constructor. JButton Methods of class JButton Constructors JButton myButton = new JButton( "Label" ); JButton myButton = new JButton( "Label", myIcon ); setRolloverIcon( myIcon ) Sets image to display when mouse over button Class ActionEvent getActionCommand returns label of button that generated event Icon bug1 = new ImageIcon( "bug1.gif" ); fancyButton = new JButton( "Fancy Button", bug1 ); fancyButton.setRolloverIcon( bug2 ); JCheckBox When JCheckBox changes ItemEvent generated Handled by an ItemListener, which must define itemStateChanged Register handlers with with addItemListener private class CheckBoxHandler implements ItemListener { public void itemStateChanged( ItemEvent e ) Class ItemEvent getStateChange Returns ItemEvent.SELECTED or ItemEvent.DESELECTED 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 // Fig. 12.12: CheckBoxTest.java // Creating Checkbox buttons. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CheckBoxTest extends JFrame { private JTextField t; private JCheckBox bold, italic; public CheckBoxTest() { super( "JCheckBox Test" ); Container c = getContentPane(); c.setLayout(new FlowLayout()); t = new JTextField( "Watch the font style change", 20 ); t.setFont( new Font( "TimesRoman", Font.PLAIN, 14 ) ); c.add( t ); // create checkbox objects bold = new JCheckBox( "Bold" ); c.add( bold ); 1. import JCheckBoxes Create italic = new JCheckBox( "Italic" ); c.add( italic ); 1.1 Declarations CheckBoxHandler handler = new CheckBoxHandler(); bold.add...
View Full Document

Ask a homework question - tutors are online