1 / 44

Swing! Components and Images

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.)

khuong
Download Presentation

Swing! Components and Images

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Swing! Components and Images

  2. Example Swing Components (also called “widgets”) Graphic from sun.com

  3. 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, JRadioButton, etc. • Need toimport javax.swing.*;

  4. JLabel • Declare a JLabel 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 ImageIcon object – see later notes) JLabel label = new JLabel( ImageIcon ); • With text and image JLabel label = new JLabel( “text”, ImageIcon, 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.

  5. JLabel Example import javax.swing.*; import java.awt.*; public class JLabelEx extends JApplet { JLabel mylabel; public void init( ) { setLayout( new FlowLayout( ) ); mylabel = new JLabel( "Lots of text can go on one line" ); add( mylabel ); } }

  6. JLabel across multiple lines import javax.swing.*; import java.awt.*; public class JLabelExMultiLine extends JApplet { JLabel mylabel; public void init( ) { setLayout( new FlowLayout( ) ); mylabel = new JLabel( "<HTML>Lots of text<P>on<P>separate lines" ); add( mylabel ); } }

  7. 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

  8. JLabel Example with HTML tags import javax.swing.*; import java.awt.*; public class JLabelExWithHTML extends JApplet { JLabel label; public void init( ) { setLayout( new FlowLayout( ) ); label = new JLabel( "<HTML>Hi <FONT SIZE=+4 COLOR=RED>there </FONT> <P>world" ); add ( label ); } }

  9. Images Image ImageIcon JLabel

  10. Images • Java can handle the following image types • .JPG • .GIF • .PNG

  11. Images getCodeBase( ) • Add images to applet following 4 steps: • Call to image file Image img = getImage( getCodeBase( ), "afraid.gif" ); • Create an ImageIcon ImageIcon ic = new ImageIcon( img ); • Create a JLabel with the ImageIcon JLabel label = new JLabel(ic ); • Add label to applet setLayout( new FlowLayout( ) ); add( label );

  12. Image Example import javax.swing.*; import java.awt.*; public class ImageExx extends JApplet { public void init ( ) { Image img = getImage( getCodeBase( ), "afraid.gif" ); // CANNOT do this:: add( img ); ImageIcon ic = new ImageIcon( img ); // CANNOT DO THIS:: add( ic ); JLabel label = new JLabel( ic );// have to add to a JLabel // then add to the applet setLayout( new FlowLayout( ) ); add( label ); } } can not add Image objects directly to the applet can not add ImageIcon objects directly to the applet

  13. Buttons JButton

  14. Buttons • Swing component: JButton • can have • Text • Images • Text & Images

  15. Buttons • Swing component: JButton • can have • Text JButton mybutton = new JButton( “Click Me” ); • Image JButton mybutton = new JButton( ImageIcon ); • Text & Image JButton mybutton = new JButton( “text”, ImageIcon ); text overlaid on top of image JButton mybutton = new JButton( “text”, ImageIcon ); mybutton.setHorizontalTextPosition(JButton.CENTER);

  16. Image Buttons JButton mybutton = new JButton( imgIcon ); • mybutton.setBorderPainted( false ); • mybutton.setContentAreaFilled( false ); • (both lines above) • mybutton.setMargin( 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

  17. Text Components JTextField

  18. JTextField • Box where users can enter one line of text JTextField street = new JTextField(); JTextField city = new JTextField( 50 ); JTextField state = new JTextField( “CO”, 2 ); JTextField zip = new JTextField( “80521” ); • JPasswordField • for entering passwords • displays ****** while user types,otherwise is just like JTextField

  19. JTextField • Constructors • JTextField tf = new JTextField( ); • creates a text field with a default number 0 of columns • JTextField tf = new JTextField( 2 ); • creates a text field with 2 columns (good for states: NC, IL ) • JTextField tf = new JTextField( “I love JaVa” ); • creates a text field with the text “I love JaVa” inside the text box – box size adjusts to text • JTextField tf = new JTextField( “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

  20. JTextField • Useful methods: • String getText( ) • Returns the text that is inside the box String theText = textField.getText( ); • void setFont( Font f ) • Set the font for the text box Font fnt = new Font( “Serif”, Font.BOLD, 18 ); textField.setFont( fnt ); • void setText( String t ) • Enters the text in the string t into the text boxtextField.setText( “I like to learn java” );

  21. Text Components JTextArea

  22. JTextArea • Box for users to enter multiple lines of text • Specify number of rows and columns

  23. JTextArea • Constructors • JTextArea ta = new JTextArea ( ); • creates a text area with a default number of columns • JTextArea ta = new JTextArea ( 5, 60 ); • creates a text area with 5 rows and 60 columns • JTextArea ta = new JTextArea ( “I love JaVa” ); • creates a text area with the text “I love JaVa” inside the text box • JTextArea ta = new JTextArea ( “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)

  24. JTextArea • Useful methods: • String getText( ) • returns the text that is inside the box String theText = textField.getText( ); • void setFont( Font f ) • set the font for the text box Font fnt = new Font( “Serif”, Font.BOLD, 18 ); textField.setFont( fnt ); • void setText( String t ) • enters the text in the string t into the text boxtextField.setText( “I like to learn java” );

  25. Choices JCheckBox

  26. JCheckBox • A check box can be toggled checked or unchecked • Can have one or more checkboxes selected(as opposed to radio buttons)

  27. JCheckBox - constructors • Constructors • JCheckBox cb = new JCheckBox( ); • creates a new checkbox with no text • JCheckBox cb = new JCheckBox( imgIcon ); • creates a new checkbox with an image • JCheckBox cb = new JCheckBox( “happy” ); • creates a new checkbox with the text “happy” • JCheckBox cb = new JCheckBox( “joyful”, true ); • creates a new checkbox with the text “happy” and checked • JCheckBox cb = new JCheckBox( “serene”, imgicon ); • creates a new checkbox with the text “happy” and an image } } }

  28. JCheckBox - methods • Useful Methods • String getText( ) • returns the checkbox’s text • String theText = cbox.getText( ); • void setEnabled( boolean b ) • enables or disables the checkbox • cbox.setEnabled( false ); • cbox.setEnabled( true );

  29. JCheckBox Example /** Example showing use of JCheckBox * @author : E.S.Boese (c) Fall 2005 */ import javax.swing.*; import java.awt.*; public class JCheckBoxEx extends JApplet { JCheckBox cb1, cb2, cb3; public void init( ) { setLayout(new FlowLayout( ) ); cb1 = new JCheckBox( "red" ); cb2 = new JCheckBox( "blue" ); cb3 = new JCheckBox( "pink" ); add( cb1 ); add( cb2 ); add( cb3 ); } }

  30. Choices JRadioButton

  31. 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 ButtonGroup object

  32. JRadioButton - Example import javax.swing.*; import java.awt.*; public class JRadioButtonEx extends JApplet { public void init( ) { JRadioButton jb1 = new JRadioButton("red" ); JRadioButton jb2 = new JRadioButton("blue" ); JRadioButton jb3 = new JRadioButton("pink" ); ButtonGroup group = new ButtonGroup( ); group.add( jb1 ); group.add( jb2 ); group.add( jb3 ); setLayout(new FlowLayout()); add( jb1 ); add( jb2 ); add( jb3 ); } } How can you add another set of selections for “Favorite Beverage”, such that user can select one color and one beverage?

  33. JRadioButton - constructors • Constructors • JRadioButton rb = new JRadioButton( imgIcon); • 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!) • JRadioButton rb = new JRadioButton( “red” ); • Creates a radio button with the text “red” • JRadioButton rb = new JRadioButton( “blue”, true ); • Creates a radio button with the text “blue” and checked • ButtonGroup group = new ButtonGroup( ); • Creates a group that radio buttons can be associated together

  34. JRadioButton - methods • Useful Methods • String getText( ) • returns the text for the radio buttonString text = radioBut.getText( ); • void setEnabled( boolean b ) • enables or disables the radio buttonradioBut.setEnabled( false );

  35. List Choices JComboBox

  36. JComboBox • Drop-down box • Can be: • User-editable: • allows user to type in a value in the field similar to a JTextField • list.setEditable( true ); • User-un-editable: • user must select an entry from the drop-down list • default

  37. JComboBox - example import javax.swing.*; import java.awt.*; public class JComboEx extends JApplet { JComboBox majors = new JComboBox( ); public void init( ) { majors.addItem( "CS" ); majors.addItem( "Math" ); majors.addItem( "History" ); majors.addItem( "Leisure Studies" ); majors.addItem( "Psych" ); setLayout( new FlowLayout( ) ); add( majors ); } }

  38. JComboBox - constructors • Constructors • JComboBox droplist = new JComboBox( ); • creates an optional combo box

  39. JComboBox - methods • Useful Methods • int getItemCount( ) • returns the number of items in the list • int numItemsInList = combolist.getItemCount( ); • int getSelectedIndex( ) • returns the index of the selected item • int selectedIndex = combolist.getSelectedIndex( ); • int getSelectedItem( ) • returns the selected item • String selectedItem = combolist.getSelectedItem( ); • void removeItem( Object obj ) • removes the object from the list • combolist.remove( “sad” ); • void removeItemAt( int index ) • removes the object at the specified index • combolist.remove( 2 );

  40. JComboBox – methods (con’t) • useful methods con’t • void setEditable( boolean flag ) • determines whether the combo box is editable • combolist.setEditable( true ); • void setEnabled( boolean flag ) • enables or disables the combo box • combolist.setEnabled( false );

  41. Components

  42. Component • These widgets inherit from the Component class

  43. Components Manipulation • The Component class has additional methods we can use on most components: • setForeground( Color ) • setBackground( Color ) • setFont( Font ) • setEnabled( boolean ) • setBorder( Border ) • setToolTipText( String ) • setBounds

  44. setBackground, setOpaque import java.awt.*; import javax.swing.*; public class OpaqueEx extends JApplet { JCheckBox cb1, cb2, cb3; JPanel pane; public void init( ) { setLayout( new FlowLayout( ) ); pane = new JPanel( ); pane.setBackground( Color.ORANGE ); cb1 = new JCheckBox( "periwinkle" ); cb2 = new JCheckBox( "snowflake" ); cb2.setBackground( Color.ORANGE ); cb3 = new JCheckBox( "magenta" ); cb3.setOpaque(false); pane.add( cb1 ); pane.add( cb2 ); pane.add( cb3 ); add( pane ); } } Default background color of components is grey, like cb1. We set cb2’s background to orange to match the color of the JPanel. The other way is to setOpaque(false) like cb3, which allows the component to be transparent and allow the background color of the component below to show through.

More Related