Docsity

Java GUI Programming: Creating a Frame Window, Study notes of Object Oriented Programming

Toggle sidebar
bg1
java.awt.Frame Class
•When working with Frame objects, there are basically three
steps involved to get a Frame window to appear on the
screen:
1. Instantiate the Frame object in memory.
2. Give the Frame object a size using setSize(), setBounds(), or
pack().
3. Make the Frame appear on the screen by invoking
setVisible(true).
09/25/2019 1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44

Partial preview of the text

Download Java GUI Programming: Creating a Frame Window and more Study notes Object Oriented Programming in PDF only on Docsity!

java.awt.Frame Class

  • (^) When working with Frame objects, there are basically three

steps involved to get a Frame window to appear on the

screen:

1. Instantiate the Frame object in memory.

2. Give the Frame object a size using setSize(), setBounds(), or

pack().

3. Make the Frame appear on the screen by invoking

setVisible(true).

Instantiate the Frame Object

  • (^) The java.awt.Frame class has four constructors:

I. public Frame(). Creates a new frame with no message in the

title bar.

II. public Frame(String title). Creates a new frame with the

given String appearing in the title bar.

III. public Frame(GraphicsConfiguration gc). Creates a frame

with the specified GraphicsConfiguration of a screen device.

IV. public Frame(String title, GraphicsConfiguration gc). Creates

a frame with the specified title and GraphicsConfiguration.

Windows

  • (^) This Frame is not displayed on the screen, and it has an initial size of 0 by 0.
  • (^) We need to give our Frame a size before displaying it, which can be done by invoking one of the following five methods:
  • (^) public void setSize(int width, int height). Sets the size of the Frame to the given width and height, in pixels.
  • (^) public void setSize(java.awt.Dimension d). Sets the size of the Frame to the same width and height as the given Dimension object.
  • (^) public void setBounds(int x, int y, int width, int height). Sets both the size and initial location of the window, where x represents the number of pixels over from the upper-left corner of the screen, and y represents the number of pixels down from the upper-left corner of the screen.
  • (^) public void setBounds(java.awt.Rectangle r). Sets the bounds of the Frame to that of the given Rectangle.
  • (^) public void pack(). Sets the size of the Frame to be just big enough to display all its components with their preferred size.

Create Window

  • (^) After we have
    • (^) instantiated a Frame,
    • (^) given it a size, and
    • (^) laid out the components within it,
    • (^) we display the Frame on the screen by invoking the setVisible() method inherited from the Component class.
  • (^) The signature of setVisible() is: public void setVisible(boolean show)
  • (^) If the boolean passed in is true, the component is made visible. If the value is false, the component is hidden.

Window

We can move, resize, minimize, and maximize the Frame window. However, we can’t close the window because closing a window often implies ending the program. If the user needs to save a document or other settings before ending, your program needs a chance to do this. The closing involves handling the WindowEvent generated by a user attempting to close the window.

Creating Window with Swing

  • (^) Let’s look at the steps involved in creating a JFrame. You start

by instantiating a JFrame using one of the following

constructors:

  • (^) public JFrame(). Creates a new JFrame with no message in

the title bar.

  • (^) public JFrame(String title). Creates a new JFrame with the

given String appearing in the title bar.

  • (^) public JFrame(GraphicsConfiguration gc). Creates a JFrame

with the specified GraphicsConfiguration of a screen device.

  • (^) public JFrame(String title, GraphicsConfiguration gc). Creates

a Jframe with the specified title and GraphicsConfiguration.

8

  • (^) The following JFrameDemo program demonstrates creating and displaying a JFrame object. import javax.swing.*; public class JFrameDemo { public static void main(String [] args) { JFrame f = new JFrame(“My first JFrame”); f.setSize(400, 300); f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); f.setVisible(true); } }

Using Swing

Clicking the X in the title bar of a JFrame causes the window to be hidden by default, but this does not cause your program to stop executing. We need to press Ctrl+c at the command prompt to stop the JVM, even though our JFrame is no longer visible on the screen.

GUI

  • (^) Component is an abstract class that is the parent class of the various GUI components of the AWT: Button, Checkbox, Choice, Label, List, and Text- Component.
  • (^) Container is an abstract class that is the parent class of the containers of the AWT: Window, Panel, and ScrollPane.
  • (^) Child objects of Component are placed within child objects of Container.
  • (^) For example, a Button can be placed within a Panel, or a List can be placed within a Frame.

Adding Components to a Container

  • (^) AComponent is added to a Container using one of the

following add() methods found in the java.awt.Container class:

  • (^) public Component add(Component c). Adds the Component

to the Container and returns a reference to the newly added

Component.

  • (^) public Component add(Component c, int index). Adds the

Component to the Container at the position specified by index

  • (^) public Component add(Component c, Object constraints).

Adds the Component to the Container using the specified

constraints

Notice that the cancel Button is added to the Frame f. Whenever f is displayed, the cancel button is also displayed Notice that the Button consumes the entire interior of the Frame, no matter what size we make the Frame.

Using Jframe

  • (^) Components are added to a JFrame differently from the way they are added to a Frame. When using a Frame, you invoke the add() method directly on the Frame object, adding the components directly to the Frame.
  • (^) When using a JFrame, we still invoke the add() method, but not on the JFrame. Instead, we add the components to the content pane of the JFrame by invoking the add() method on the JFrame’s content pane.
  • (^) We use the getContentPane() method in the JFrame class to obtain a reference to the content pane of a JFrame. For Ex, the following statements add a JButton to the content pane of a JFrame: JFrame f = new JFrame(); JButton b = new JButton(); Container contentPane = f.getContentPane(); contentPane.add(b);
  • (^) Suppose GUI programming using Visual Basic, and all one could visually
place the components exactly where I wanted them in the window using
the Visual Basic IDE. Can you do that in Java?
  • (^) A: You can if you have an IDE like Visual Café or Visual Age. These IDEs
have GUI editors that let you place components exactly where you want
them. You can also organize components by assigning a null layout
manager to your container and specifying the exact location and size of
each component added.
  • (^) Q: So why would you ever use one of the layout managers? Why not just
use the IDE or lay out the components exactly where you want them?
  • (^) A: Two reasons: First, you might not have an IDE, and if you do, there is a
certain complexity to figuring out how to use it. If you understand layout
managers, this will help you comprehend the code that the IDE is
generating for you.
  • (^) Second, using a layout manager to lay out your components makes your
GUI more portable. You might be surprised to see that a GUI that you
created using an IDE looks great on Windows, but not so great on a Unix or
Macintosh platform. The same problem can occur if you try to lay out
components exactly where you want them..

JDBC : Ques.

  • (^) Q: Q: How does the layout manager know how you want your GUI to look?
  • (^) A: You need to understand the way each type of layout manager behaves. For example, you need to know that the FlowLayout manager gives components their preferred size, and that BorderLayout places components in specific regions of the container.
  • (^) By using the different layout managers and nesting containers, you have great control over the look of the GUI, while at the same time letting the layout managers determine the exact location and size of your GUI components.