Swing Components III - PowerPoint PPT Presentation

swing components iii n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Swing Components III PowerPoint Presentation
Download Presentation
Swing Components III

play fullscreen
1 / 22
Swing Components III
104 Views
Download Presentation
richard-gentry
Download Presentation

Swing Components III

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Swing Components III Chapter 12 - Student JFrame, Component Methods

  2. JFrame windows

  3. Window • Mostly used for ____________ • Generic type that _______ and Dialog inherit • 3 important methods: • dispose() – eliminate ___________ when done • pack() – __________ to fit added components • setVisible(boolean) – make the window appear (c) 2005 by Elizabeth Sugar Boese

  4. JFrame • Uses ____________ by default • Possible ornaments: • Title • ____________ • Menu • ____________ • Widgets to hide/minimize/grow • Methods: • Constructors: • JFrame() • JFrame( String title) • setBounds( w, h ) • setSize( w, h ) (c) 2005 by Elizabeth Sugar Boese

  5. JFrame text is the title on the JFrame • Create a JFrame window JFrame frame; frame = new JFrame( text ); • Set the ____________ on the screen (optional) frame.setLocation( 500, 600 ); • Major Steps 1. ______________________. JFrame frame; frame = new JFrame("FrameDemo"); 2. Set ________________ frame.setLayout( new BorderLayout( ) ); 3. Create components and _________________________ JButton go = new JButton( "Go for it!" ); frame.add(universe, BorderLayout.SOUTH); 4. _________ the frame. frame.pack(); OR frame.setSize( 200, 500 ); 5. Show it. frame.setVisible(true); Calling the .pack( ) method will size the frame based on the minimum size to fit all the components added to the frame. (c) 2005 by Elizabeth Sugar Boese

  6. JFrame – Simple Example import javax.swing.*; public class JFrameDefault extends JApplet { JFrame frame; public void init( ) { frame = new JFrame( "Testing JFrame stuff" ); frame.setSize( 200,100 ); frame.setVisible( true ); } } (c) 2005 by Elizabeth Sugar Boese

  7. JFrame – Example with components import java.awt.*; import javax.swing.*; public class JFrameStuff extendsJApplet { JFrame frame; JLabel myname, universe; JButton go, skiing; public void init( ) { //1. Create the frame. frame = new JFrame("FrameDemo"); //2. Set layout manager frame.setLayout( new BorderLayout( ) ); //3. Create components and put them in the frame. myname = new JLabel( "<HTML>My <BR>Stuff", JLabel.CENTER ); go = new JButton( "Go for it!" ); skiing = new JButton( "Do you like skiing too?" ); universe = new JLabel( "I live at the borders of the universe where fact and fiction collide" ); frame.add(myname, BorderLayout.NORTH); frame.add(go, BorderLayout.WEST); frame.add(skiing, BorderLayout.EAST); frame.add(universe, BorderLayout.SOUTH); //4. Size the frame. frame.pack( ); //5. Show it. frame.setVisible(true); } } (c) 2005 by Elizabeth Sugar Boese

  8. JFrame Closing Behaviors • When user selects window manager Close option for JFrame, has default behavior • JFrame did nothing • JFrame hides itself • setDefaultCloseOperation (operation) • DO_NOTHING_ON_CLOSE • HIDE_ON_CLOSE • DISPOSE_ON_CLOSE • No EXIT_ON_CLOSE operation (c) 2005 by Elizabeth Sugar Boese

  9. JFrame - Events • usually want to pop open a JFrame based on an event (e.g. a button click) • Example (c) 2005 by Elizabeth Sugar Boese

  10. Dialogs (extra material)

  11. JOptionPane Examples (c) 2005 by Elizabeth Sugar Boese

  12. Dialog • For simple _______________ with user • Uses _______________ by default • Are _____ by default (can change to modeless setModal(false) • Constructors: • Dialog( Frame parent ) • Dialog( Frame parent, String title ) • Dialog( Frame parent, boolean isModal ) • Call to super( ) (c) 2005 by Elizabeth Sugar Boese

  13. Dialog Example Frame frame = new Frame( ); Dialog d = new Dialog( frame, “Hi there”, true ); JButton button = new JButton( “Okay?” ); JPanel panel = new JPanel(); p.add( button ); d.add( panel ); d.setSize( 400,100 ); d.setVisible( true ); (c) 2005 by Elizabeth Sugar Boese

  14. Menu (extra material)

  15. Menu • MenuBar • Collection of Menus • Associated with a Frame • Menu • Collection of MenuItems and separators • Can be a MenuItem for hierarchical menus • MenuItem • Enabled or disabled • May be checkable (c) 2005 by Elizabeth Sugar Boese

  16. Component Methods

  17. Component Methods • Common methods works on most components (e.g. JLabel, JButton, JPanel, etc.) • setBackground ( Color ) • setForeground ( Color ) • setBorder ( Border ) • setFont ( Font ) • setToolTipText( String ) • setVisible ( boolean ) (c) 2005 by Elizabeth Sugar Boese

  18. Component Methods - JButton • JButton • addActionListener ( this ) • setBorderPainted ( boolean ) • setContentAreaFilled ( boolean ) • setDisabledIcon ( ImageIcon ) • setIcon ( ImageIcon ) • setHorizontalAlignment ( int ) • setHorizontalTextPosition ( int ) • setRolloverIcon ( ImageIcon ) • setText ( String ) Parameter int one of: JButton.LEFT JButton.CENTER JButton.RIGHT (c) 2005 by Elizabeth Sugar Boese

  19. Component Methods – JCheckbox, JRadioButton • JCheckbox and JRadioButton • addItemListener ( this ) • isSelected ( ) Returns true if currently selected, false if not selected (c) 2005 by Elizabeth Sugar Boese

  20. Component Methods - JLabel • JLabel • setIcon ( ImageIcon ) • setText ( String ) (c) 2005 by Elizabeth Sugar Boese

  21. Component Methods - text • JTextField • getText ( ) • setText ( ) • JTextArea • append ( String ) • getText ( ) • setLineWrap ( boolean ) • setText ( String ) • setWrapStyleWord ( boolean ) (c) 2005 by Elizabeth Sugar Boese

  22. Summary • JFrame • Dialogs • Menu • Component methods (c) 2005 by Elizabeth Sugar Boese