Langobject javaawtcomponent javaawtcontainer

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: ritance hierarchy java.lang.Object java.awt.Component java.awt.Container javax.swing.JComponent Component defines methods used in its subclasses (for example, paint and repaint) Container - collection of related components When using JFrame, add components to content pane (a Container) JComponent - superclass to most Swing components Jcomponent features Pluggable look and feel Can look like different platforms, at run-time Direct access to components through keyboard If several components perform same actions Describe component when mouse rolls over it Shortcut keys (mnemonics) Common event handling Tool tips JLabel Labels Provide text instructions on a GUI Read-only text Programs rarely change a label's contents Class JLabel (subclass of JComponent) Methods Can declare label text in constructor myLabel.setToolTipText( "Text" ) Displays "Text" in a tool tip when mouse over label myLabel.setText( "Text" ) myLabel.getText() JLabel Icon Object that implements interface Icon One class is ImageIcon (.gif and .jpeg images) 24 Icon bug = new ImageIcon( "bug1.gif" ); Assumed same directory as program Display an icon with JLabel's setIcon method 33 label3.setIcon( bug ); myLabel.setIcon( myIcon ); myLabel.getIcon //returns current Icon 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 // Fig. 12.4: LabelTest.java // Demonstrating the JLabel class. import javax.swing.*; import java.awt.*; import java.awt.event.*; public class LabelTest extends JFrame { private JLabel label1, label2, label3; public LabelTest() { super( "Testing JLabel" ); Create a Container object, to which we attach JLabel objects (subclass of JComponent). Container c = getContentPane(); c.setLayout( new FlowLayout() ); Initialize text in JLabel constructor. // JLabel constructor with a string argument label1 = new JLabel( "Label with text" ); label1.setToolTipText( "This is label1" ); c.add( label1 ); // JLabel constructor with string, Icon // alignment argument...
View Full Document

This note was uploaded on 08/06/2008 for the course CSE 432 taught by Professor Blank during the Fall '08 term at Lehigh University .

Ask a homework question - tutors are online