Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Swing Components III Chapter 12 - Student JFrame, Component Methods."— Presentation transcript:

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

2 JFrame windows

3 (c) 2005 by Elizabeth Sugar Boese 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

4 (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 )

5 (c) 2005 by Elizabeth Sugar Boese 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(); ORframe.setSize( 200, 500 ); 5. Show it. frame.setVisible(true); text is the title on the JFrame Calling the.pack( ) method will size the frame based on the minimum size to fit all the components added to the frame.

6 (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 ); }

7 (c) 2005 by Elizabeth Sugar Boese JFrame – Example with components import java.awt.*; import javax.swing.*; public class JFrameStuff extends JApplet { 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( " My 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); }

8 (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

9 (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

10 Dialogs (extra material)

11 (c) 2005 by Elizabeth Sugar Boese JOptionPane Examples

12 (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( )

13 (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 );

14 Menu (extra material)

15 (c) 2005 by Elizabeth Sugar Boese 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

16 Component Methods

17 (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 )

18 (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

19 (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

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

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

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


Download ppt "Swing Components III Chapter 12 - Student JFrame, Component Methods."

Similar presentations


Ads by Google