230 likes | 364 Views
Swing Components III. Chapter 12 - Student JFrame, Component Methods. JFrame windows. Window. Mostly used for ____________ Generic type that _______ and Dialog inherit 3 important methods : dispose() – eliminate ___________ when done pack() – __________ to fit added components
E N D
Swing Components III Chapter 12 - Student JFrame, Component Methods
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
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
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
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
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
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
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
Dialogs (extra material)
JOptionPane Examples (c) 2005 by Elizabeth Sugar Boese
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
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
Menu (extra material)
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
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
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
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
Component Methods - JLabel • JLabel • setIcon ( ImageIcon ) • setText ( String ) (c) 2005 by Elizabeth Sugar Boese
Component Methods - text • JTextField • getText ( ) • setText ( ) • JTextArea • append ( String ) • getText ( ) • setLineWrap ( boolean ) • setText ( String ) • setWrapStyleWord ( boolean ) (c) 2005 by Elizabeth Sugar Boese
Summary • JFrame • Dialogs • Menu • Component methods (c) 2005 by Elizabeth Sugar Boese