TCU CoSc 10403 Programming with Java - PowerPoint PPT Presentation

About This Presentation
Title:

TCU CoSc 10403 Programming with Java

Description:

TCU CoSc 10403 Programming with Java The JFrame Class The JFrame Class A Frame is a top-level window with a title and a border. The size of the frame includes any ... – PowerPoint PPT presentation

Number of Views:100
Avg rating:3.0/5.0
Slides: 9
Provided by: Dr231846
Learn more at: http://csfaculty.tcu.edu
Category:
Tags: cosc | tcu | java | programming

less

Transcript and Presenter's Notes

Title: TCU CoSc 10403 Programming with Java


1
TCU CoSc 10403 Programming with Java
  • The JFrame Class

2
The JFrame Class
  • A Frame is a top-level window with a title and a
    border. The size of the frame includes any area
    designated for the border.
  • Calling setVisible(true) makes the frame appear
    onscreen. Sometimes you might see the show()
    method used instead. The two usages are
    equivalent.
  • By default, the frame window will appear in the
    top left corner of the screen. Another location
    can be specified using the setlocation() method
    on the frame.
  • The default layout manager is BorderLayout.
  • Five steps to get a frame to work
  • Create the frame JFrame frame new
    Jframe( some title )
  • Set the layout frame.setLayout(new
    FlowLayout())
  • Instantiate the components and add them to the
    frame. frame.add(button1)
  • Size the frame frame.setSize(200,500)
  • Set the location frame.setLocation(100,200)
  • Show the frame frame.setVisible(true)

3
The JFrame Class
  • Two Constructors that are of interest to us
  • JFrame()?Constructs a new frame that is initially
    invisible.
  • JFrame(String title)?Creates a new, initially
    invisible Frame with the specified title.
  • Sizing a JFrame??JFrame frame1 new
    JFrame(CoSc 10403) ?. . .
  • frame1.setSize(100,100) //will appear in upper
    left area of screen
  • frame1.setSize(100,100) //frame of size 100x100
    pixels
  • frame1.setLocation(100,50) //placement 100
    pixels over, 50 pixels down
  • frame1.setBounds(100,50,100,100) //same as
    above?

4
The JFrame Class
  • Useful methods that are of interest to us
  • void pack()?Size the window on the basis of the
    minimum size to fit all components added to the
    frame (i.e., just wide enough to fit the text in
    the bottom of the frame and high enough for each
    component to display properly).
  • void setSize(int, int)?Set the total size of the
    window
  • void setBounds(int, int, int, int)?Set the size
    and position of the window.
  • void setLocation(int, int)?Set the location of
    the upper left corner of the window
  • void remove(Component comp)
  • Removes the specified component from this
    container.
  • void removeAll()
  • Removes all components from the JFrame.
  • void setLayout(LayoutManager manager)
  • Sets the layout of this component.

5
The JFrame Class
  • import javax.swing.
  • import java.awt.event.
  • import java.awt.
  • public class JFrameDemo extends JApplet
    implements ActionListener
  • JLabel l1 new JLabel("Welcome to my JFrame")
  • JButton b1 new JButton("Display Frame")
  • JFrame frame
  • public void init()
  • setLayout(new FlowLayout())
  • add(b1)
  • b1.addActionListener(this)
  • public void actionPerformed(ActionEvent e)

6
JFrame Details
  • How do you close a JFrame?
  • You can
  • Get rid of the JFrame object entirely.
  • Just make the JFrame invisible, but ready to be
    made visible again an instant later.
  • Make the JFrame invisible, and put in into cold
    storage, ready to be made visible again with a
    little thawing.
  • System.exit( 0 ) the program immediately
  • You can close the JFrame
  • when the user clicks the closing X.
  • when the user clicks a close menu item.
  • programmatically when you decide to close the
    JFrame.
  • programmatically when you decide to shut down the
    entire program
  • When the user clicks the close X, that does not
    make your JFrame object mysteriously disappear or
    force it to close, or go invisible. All it does
    is send your JFrame a WindowClosing event. Your
    JFrame is free to ignore the event, close the
    JFrame in any of the four usual ways, or do
    something else entirely.The event in no way
    closes your JFrame or changes its state. It is
    totally up to you what happens. Receiving a
    WindowClosingEvent is not an eviction notice it
    is just notification the user idly clicked an X.
    It does not mean the JFrame is closing or that it
    has to close, just that the user would like it to.

7
JFrame Details
  • Shorthand WindowClosingEvent Handlers
  • There are three shortcuts to writing
    WindowClosing event handlers. With these
    shortcuts, you have no opportunity to add any of
    your own application code.
  • If all you want to do is call dispose() you can
    use this, instead of setting up an event handler.
  • this.setDefaultCloseOperation (
    JFrame.DISPOSE_ON_CLOSE )
  • If all you want to do is call setVisible( false )
    you can use this, instead of setting up an event
    handler.
  • this.setDefaultCloseOperation (
    JFrame.HIDE_ON_CLOSE )?Believe it or not, this
    is the default. Beware!
  • If all you want to do is call System.exit( 0 )
    you can use this, instead of setting up an event
    handler.
  • this.setDefaultCloseOperation (
    JFrame.EXIT_ON_CLOSE )
  • If all you want to write your own custom event
    handler, you need to use this, as well as setting
    up an event handler.
  • this.setDefaultCloseOperation( JFrame.DO_NOTHING_O
    N_CLOSE )

8
JFrame Details
  • this.setDefaultCloseOperation ( JFrame.
    DO_NOTHING_ON_CLOSE )
  • // what happens when user closes the JFrame.
  • WindowListener windowListener new
    WindowAdapter()
  • // anonymous WindowAdapter class
  • public void windowClosing ( WindowEvent w )
  • // Whatever application code you want to do on
    close, e.g. rememberLocation(
    MyFrame.this.getX(),MyFrame.this.getY() )
  • // Whatever code you want to actually close the
    JFrame, e.g. MyFrame.this.setVisible(
    false ) MyFrame.this.dispose()
  • // end windowClosing
  • // end anonymous class
  • this.addWindowListener( windowListener )
Write a Comment
User Comments (0)
About PowerShow.com