Swing Components and Images Example Swing Components also
- Slides: 44
Swing! Components and Images
Example Swing Components (also called “widgets”) Graphic from sun. com
Swing • Swing is a text component package • Enhancement of the AWT package – + Swing has extra functionality (tooltips, double buffering keyboard shortcuts, etc. ) – + You can make Swing components look the same on all platforms, whereas awt components will vary on different platforms – - Swing is bigger and more complicated – - Swing is not supported by earlier JVMs – - Awt components tend to be faster than Swing components • Most class names begin with the capital letter J – JLabel, JButton, JCheckbox, JRadio. Button, etc. • Need to import javax. swing. *;
JLabel • Declare a JLabel label; • Create a JLabel multiple ways: – With just text: label = new JLabel( “text” ); OR label = new JLabel( “text”, alignment ); – With just an image (image inside an Image. Icon object – see later notes) JLabel label = new JLabel( Image. Icon ); – With text and image JLabel label = new JLabel( “text”, Image. Icon, alignment ); • where alignment is either – JLabel. LEFT – JLabel. CENTER – JLabel. RIGHT alignment becomes important when we work with layout managers, but the effect won’t be apparent right now.
JLabel Example import javax. swing. *; import java. awt. *; public class JLabel. Ex extends JApplet { JLabel mylabel; public void init( ) { set. Layout( new Flow. Layout( ) ); mylabel = new JLabel( "Lots of text can go on one line" ); add( mylabel ); } }
JLabel across multiple lines import javax. swing. *; import java. awt. *; public class JLabel. Ex. Multi. Line extends JApplet { JLabel mylabel; public void init( ) { set. Layout( new Flow. Layout( ) ); mylabel = new JLabel( "<HTML>Lots of text<P>on<P>separate lines" ); add( mylabel ); } }
JLabel with funky text • Lots of flexibility, but need to use HTML tags • HTML tags are descriptions of how the text should be displayed • Each tag is enclosed in brackets < and > • To use these tags, you need to customize the JLabel string with the <HTML> tag
JLabel Example with HTML tags import javax. swing. *; import java. awt. *; public class JLabel. Ex. With. HTML extends JApplet { JLabel label; public void init( ) { set. Layout( new Flow. Layout( ) ); label = new JLabel( "<HTML>Hi <FONT SIZE=+4 COLOR=RED>there </FONT> <P>world" ); add ( label ); } }
Images Image. Icon JLabel
Images • Java can handle the following image types –. JPG –. GIF –. PNG
Images get. Code. Base( ) • Add images to applet following 4 steps: 1) Call to image file Image img = get. Image( get. Code. Base( ), "afraid. gif" ); 2) Create an Image. Icon ic = new Image. Icon( img ); 3) Create a JLabel with the Image. Icon JLabel label = new JLabel(ic ); 4) Add label to applet set. Layout( new Flow. Layout( ) ); add( label );
Image Example import javax. swing. *; import java. awt. *; public class Image. Exx extends JApplet { public void init ( ) { Image img = get. Image( get. Code. Base( ), "afraid. gif" ); // CANNOT do this: : add( img ); Image. Icon ic = new Image. Icon( img ); // CANNOT DO THIS: : add( ic ); JLabel label = new JLabel( ic ); // then add to the applet set. Layout( new Flow. Layout( ) ); add( label ); } } can not add Image objects directly to the applet can not add Image. Icon objects directly to the applet // have to add to a JLabel
Buttons JButton
Buttons • Swing component: JButton • can have – Text – Images – Text & Images
Buttons • Swing component: JButton • can have – Text JButton mybutton = new JButton( “Click Me” ); – Image JButton mybutton = new JButton( Image. Icon ); – Text & Image JButton mybutton = new JButton( “text”, Image. Icon ); text overlaid on top of image JButton mybutton = new JButton( “text”, Image. Icon ); mybutton. set. Horizontal. Text. Position(JButton. CENTER);
Image Buttons JButton mybutton = new JButton( img. Icon ); • – mybutton. set. Border. Painted( false ); • – mybutton. set. Content. Area. Filled( false ); • – (both lines above) • – mybutton. set. Margin( new Insets(0, 0, 0, 0) ); where first 0 is top margin, second 0 is left, third 0 is bottom margin and last 0 is right
Text Components JText. Field
JText. Field • Box where users can enter one line of text JText. Field street = new JText. Field(); city = new JText. Field( 50 ); state = new JText. Field( “CO”, 2 ); zip = new JText. Field( “ 80521” ); • JPassword. Field – for entering passwords – displays ****** while user types, otherwise is just like JText. Field
JText. Field • Constructors – JText. Field tf = new JText. Field( ); • creates a text field with a default number 0 of columns – JText. Field tf = new JText. Field( 2 ); • creates a text field with 2 columns (good for states: NC, IL ) – JText. Field tf = new JText. Field( “I love Ja. Va” ); • creates a text field with the text “I love Ja. Va” inside the text box – box size adjusts to text – JText. Field tf = new JText. Field( “Java rocks”, 10 ); • creates a text field with the text “Java rocks” inside the text box which has 10 columns visible • Note: 1 column = width of ‘M’ character in current font
JText. Field • Useful methods: – String get. Text( ) • Returns the text that is inside the box String the. Text = text. Field. get. Text( ); – void set. Font( Font f ) • Set the font for the text box Font fnt = new Font( “Serif”, Font. BOLD, 18 ); text. Field. set. Font( fnt ); – void set. Text( String t ) • Enters the text in the string t into the text box text. Field. set. Text( “I like to learn java” );
Text Components JText. Area
JText. Area • Box for users to enter multiple lines of text • Specify number of rows and columns
JText. Area • Constructors – JText. Area ta = new JText. Area ( ); • creates a text area with a default number of columns – JText. Area ta = new JText. Area ( 5, 60 ); • creates a text area with 5 rows and 60 columns – JText. Area ta = new JText. Area ( “I love Ja. Va” ); • creates a text area with the text “I love Ja. Va” inside the text box – JText. Area ta = new JText. Area ( “Java rocks”, 4, 10 ); • creates a text area with the text “Java rocks” inside the text box which has 4 rows visible (height) and 10 columns visible (width)
JText. Area • Useful methods: – String get. Text( ) • returns the text that is inside the box String the. Text = text. Field. get. Text( ); – void set. Font( Font f ) • set the font for the text box Font fnt = new Font( “Serif”, Font. BOLD, 18 ); text. Field. set. Font( fnt ); – void set. Text( String t ) • enters the text in the string t into the text box text. Field. set. Text( “I like to learn java” );
Choices JCheck. Box
JCheck. Box • A check box can be toggled checked or unchecked • Can have one or more checkboxes selected (as opposed to radio buttons)
JCheck. Box constructors • Constructors • creates a new checkbox with no text – JCheck. Box cb = new JCheck. Box( img. Icon ); • creates a new checkbox with an image – JCheck. Box cb = new JCheck. Box( “happy” ); • creates a new checkbox with the text “happy” – JCheck. Box cb = new JCheck. Box( “joyful”, true ); • creates a new checkbox with the text “happy” and checked – JCheck. Box cb = new JCheck. Box( “serene”, imgicon ); • creates a new checkbox with the text “happy” and an image } } } – JCheck. Box cb = new JCheck. Box( );
JCheck. Box - methods • Useful Methods – String get. Text( ) • returns the checkbox’s text • String the. Text = cbox. get. Text( ); – void set. Enabled( boolean b ) • enables or disables the checkbox • cbox. set. Enabled( false ); • cbox. set. Enabled( true );
JCheck. Box Example /** Example showing use of JCheck. Box * @author : E. S. Boese (c) Fall 2005 */ import javax. swing. *; import java. awt. *; public class JCheck. Box. Ex extends JApplet { JCheck. Box cb 1, cb 2, cb 3; public void init( ) { set. Layout(new Flow. Layout( ) ); cb 1 = new JCheck. Box( "red" ); cb 2 = new JCheck. Box( "blue" ); cb 3 = new JCheck. Box( "pink" ); add( cb 1 ); add( cb 2 ); add( cb 3 ); } }
Choices JRadio. Button
Radio Buttons • A group of radio buttons represents a set of mutually exclusive options – only one can be selected • When a radio button from a group is selected, the button that is currently checked in the group is automatically toggled off • To define the group of radio buttons that will work together, each radio button is added to a Button. Group object
JRadio. Button - Example import javax. swing. *; import java. awt. *; public class JRadio. Button. Ex extends JApplet { public void init( ) { JRadio. Button jb 1 = new JRadio. Button("red" ); JRadio. Button jb 2 = new JRadio. Button("blue" ); JRadio. Button jb 3 = new JRadio. Button("pink" ); Button. Group group = new Button. Group( ); group. add( jb 1 ); group. add( jb 2 ); group. add( jb 3 ); set. Layout(new Flow. Layout()); add( jb 1 ); add( jb 2 ); add( jb 3 ); } } How can you add another set of selections for “Favorite Beverage”, such that user can select one color and one beverage?
JRadio. Button - constructors • Constructors – JRadio. Button rb = new JRadio. Button( img. Icon); • Creates a radio button with an image • Useful if you set two images: one for selected, one for unselected (otherwise can’t tell the difference!) – JRadio. Button rb = new JRadio. Button( “red” ); • Creates a radio button with the text “red” – JRadio. Button rb = new JRadio. Button( “blue”, true ); • Creates a radio button with the text “blue” and checked – Button. Group group = new Button. Group( ); • Creates a group that radio buttons can be associated together
JRadio. Button - methods • Useful Methods – String get. Text( ) • returns the text for the radio button String text = radio. But. get. Text( ); – void set. Enabled( boolean b ) • enables or disables the radio button radio. But. set. Enabled( false );
List Choices JCombo. Box
JCombo. Box • Drop-down box • Can be: – User-editable: • allows user to type in a value in the field similar to a JText. Field • list. set. Editable( true ); – User-un-editable: • user must select an entry from the drop-down list • default
JCombo. Box - example import javax. swing. *; import java. awt. *; public class JCombo. Ex extends JApplet { JCombo. Box majors = new JCombo. Box( ); public void init( ) { majors. add. Item( "CS" ); majors. add. Item( "Math" ); majors. add. Item( "History" ); majors. add. Item( "Leisure Studies" ); majors. add. Item( "Psych" ); set. Layout( new Flow. Layout( ) ); add( majors ); } }
JCombo. Box - constructors • Constructors – JCombo. Box droplist = new JCombo. Box( ); • creates an optional combo box
JCombo. Box - methods • Useful Methods – int get. Item. Count( ) • returns the number of items in the list • int num. Items. In. List = combolist. get. Item. Count( ); – int get. Selected. Index( ) • returns the index of the selected item • int selected. Index = combolist. get. Selected. Index( ); – int get. Selected. Item( ) • returns the selected item • String selected. Item = combolist. get. Selected. Item( ); – void remove. Item( Object obj ) • removes the object from the list • combolist. remove( “sad” ); – void remove. Item. At( int index ) • removes the object at the specified index • combolist. remove( 2 );
JCombo. Box – methods (con’t) • useful methods con’t – void set. Editable( boolean flag ) • determines whether the combo box is editable • combolist. set. Editable( true ); – void set. Enabled( boolean flag ) • enables or disables the combo box • combolist. set. Enabled( false );
Components
Component • These widgets inherit from the Component class
Components Manipulation • The Component class has additional methods we can use on most components: – set. Foreground( Color ) – set. Background( Color ) – set. Font( Font ) – set. Enabled( boolean ) – set. Border( Border ) – set. Tool. Tip. Text( String ) – set. Bounds
set. Background, set. Opaque import java. awt. *; import javax. swing. *; public class Opaque. Ex extends JApplet { JCheck. Box cb 1, cb 2, cb 3; JPanel pane; public void init( ) { set. Layout( new Flow. Layout( ) ); pane = new JPanel( ); pane. set. Background( Color. ORANGE ); cb 1 = new JCheck. Box( "periwinkle" ); cb 2 = new JCheck. Box( "snowflake" ); cb 2. set. Background( Color. ORANGE ); cb 3 = new JCheck. Box( "magenta" ); cb 3. set. Opaque(false); pane. add( cb 1 ); pane. add( cb 2 ); pane. add( cb 3 ); add( pane ); } } Default background color of components is grey, like cb 1. We set cb 2’s background to orange to match the color of the JPanel. The other way is to set. Opaque(false) like cb 3, which allows the component to be transparent and allow the background color of the component below to show through.
- Swing components
- Jcheck box
- Java gui libraries
- Virtual and real images
- Real images vs virtual images
- Mail @ malaysia.images.search.yahoo.com
- How to save images on google images
- Yahoo.com.yw
- Swing mvc example
- Scala swing tutorial
- Java swing form example
- Java import java.util.*
- Java swing form example
- Jspinner example
- Jimmy and eric were huddled by the tire swing
- Diff between awt and swing
- She was known for her beauty and generous
- Obvious ways values enter psychology
- Caudal fin shark
- Also in addition furthermore
- Big ed mona multimodal text
- Merkmale des jazz
- Softball ontario rules
- Up in the air and over the wall till i can see so wide
- Swing check valve symbol
- How the customer explained it
- Swing vs awt
- Quadrilateral configuration of clasp
- John pushes hector on a plastic toboggan
- Java focus listener
- Perbedaan antara java swing dan awt adalah
- Java swing responsive gui
- Jazz a swing v české hudbě
- Jazz prezentace
- Containment hierarchy
- Import javax.swing.*
- Java swing
- Swing import
- Guiding planes rpd
- Swing hierarchy
- Electro swing history
- Import javax.swing.*
- Swing equation
- Voltage output swing from rail
- The jazz style called swing flourished in america from