Java awt frame window

Java AWT tutorial for beginners

AWT stands for Abstract Window Toolkit. It is a platform dependent API for creating Graphical User Interface (GUI) for java programs.

Why AWT is platform dependent? Java AWT calls native platform (Operating systems) subroutine for creating components such as textbox, checkbox, button etc. For example an AWT GUI having a button would have a different look and feel across platforms like windows, Mac OS & Unix, this is because these platforms have different look and feel for their native buttons and AWT directly calls their native subroutine that creates the button. In simple, an application build on AWT would look like a windows application when it runs on Windows, but the same application would look like a Mac application when runs on Mac OS.

AWT is rarely used now days because of its platform dependent and heavy-weight nature. AWT components are considered heavy weight because they are being generated by underlying operating system (OS). For example if you are instantiating a text box in AWT that means you are actually asking OS to create a text box for you.

Swing is a preferred API for window based applications because of its platform independent and light-weight nature. Swing is built upon AWT API however it provides a look and feel unrelated to the underlying platform. It has more powerful and flexible components than AWT. In addition to familiar components such as buttons, check boxes and labels, Swing provides several advanced components such as tabbed panel, scroll panes, trees, tables, and lists. We will discuss Swing in detail in a separate tutorial.

AWT hierarchy

Java AWT hierarchy diagram

Components and containers

All the elements like buttons, text fields, scrollbars etc are known as components. In AWT we have classes for each component as shown in the above diagram. To have everything placed on a screen to a particular position, we have to add them to a container. A container is like a screen wherein we are placing components like buttons, text fields, checkbox etc. In short a container contains and controls the layout of components. A container itself is a component (shown in the above hierarchy diagram) thus we can add a container inside container.

Types of containers:
As explained above, a container is a place wherein we add components like text field, button, checkbox etc. There are four types of containers available in AWT: Window, Frame, Dialog and Panel. As shown in the hierarchy diagram above, Frame and Dialog are subclasses of Window class.

Window: An instance of the Window class has no border and no title
Dialog: Dialog class has border and title. An instance of the Dialog class cannot exist without an associated instance of the Frame class.
Panel: Panel does not contain title bar, menu bar or border. It is a generic container for holding components. An instance of the Panel class provides a container to which to add components.
Frame: A frame has title, border and menu bars. It can contain several components like buttons, text fields, scrollbars etc. This is most widely used container while developing an application in AWT.

Читайте также:  Javascript src text file

Java AWT Example

We can create a GUI using Frame in two ways:
1) By extending Frame class
2) By creating the instance of Frame class
Lets have a look at the example of each one.

AWT Example 1: creating Frame by extending Frame class

import java.awt.*; /* We have extended the Frame class here, * thus our class "SimpleExample" would behave * like a Frame */ public class SimpleExample extends Frame < SimpleExample()< Button b=new Button("Button!!"); // setting button position on screen b.setBounds(50,50,50,50); //adding button into frame add(b); //Setting Frame width and height setSize(500,300); //Setting the title of Frame setTitle("This is my First AWT example"); //Setting the layout for the Frame setLayout(new FlowLayout()); /* By default frame is not visible so * we are setting the visibility to true * to make it visible. */ setVisible(true); >public static void main(String args[]) < // Creating the instance of Frame SimpleExample fr=new SimpleExample(); >>

AWT example 1

Output:

AWT Example 2: creating Frame by creating instance of Frame class

import java.awt.*; public class Example2 < Example2() < //Creating Frame Frame fr=new Frame(); //Creating a label Label lb = new Label("UserId: "); //adding label to the frame fr.add(lb); //Creating Text Field TextField t = new TextField(); //adding text field to the frame fr.add(t); //setting frame size fr.setSize(500, 300); //Setting the layout for the Frame fr.setLayout(new FlowLayout()); fr.setVisible(true); >public static void main(String args[]) < Example2 ex = new Example2(); >>

AWT example 2

Output:

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

Источник

AWT Frame Class

The class Frame is a top level window with border and title. It uses BorderLayout as default layout manager.

Class declaration

Following is the declaration for java.awt.Frame class:

public class Frame extends Window implements MenuContainer

Field

Following are the fields for java.awt.Frame class:

  • static float BOTTOM_ALIGNMENT — Ease-of-use constant for getAlignmentY.
  • static int CROSSHAIR_CURSOR — Deprecated. replaced by Cursor.CROSSHAIR_CURSOR.
  • static int DEFAULT_CURSOR — Deprecated. replaced by Cursor.DEFAULT_CURSOR.
  • static int E_RESIZE_CURSOR — Deprecated. replaced by Cursor.E_RESIZE_CURSOR.
  • static int HAND_CURSOR — Deprecated. replaced by Cursor.HAND_CURSOR.
  • static int ICONIFIED — This state bit indicates that frame is iconified.
  • static int MAXIMIZED_BOTH — This state bit mask indicates that frame is fully maximized (that is both horizontally and vertically).
  • static int MAXIMIZED_HORIZ — This state bit indicates that frame is maximized in the horizontal direction.
  • static int MAXIMIZED_VERT — This state bit indicates that frame is maximized in the vertical direction.
  • static int MOVE_CURSOR — Deprecated. replaced by Cursor.MOVE_CURSOR.
  • static int N_RESIZE_CURSOR — Deprecated. replaced by Cursor.N_RESIZE_CURSOR.
  • static int NE_RESIZE_CURSOR — Deprecated. replaced by Cursor.NE_RESIZE_CURSOR.
  • static int NORMAL — Frame is in the «normal» state.
  • static int NW_RESIZE_CURSOR — Deprecated. replaced by Cursor.NW_RESIZE_CURSOR.
  • static int S_RESIZE_CURSOR — Deprecated. replaced by Cursor.S_RESIZE_CURSOR.
  • static int SE_RESIZE_CURSOR — Deprecated. replaced by Cursor.SE_RESIZE_CURSOR.
  • static int SW_RESIZE_CURSOR — Deprecated. replaced by Cursor.SW_RESIZE_CURSOR.
  • static int TEXT_CURSOR — Deprecated. replaced by Cursor.TEXT_CURSOR.
  • static int W_RESIZE_CURSOR — Deprecated. replaced by Cursor.W_RESIZE_CURSOR.
  • static int WAIT_CURSOR — Deprecated. replaced by Cursor.WAIT_CURSOR.
Читайте также:  Выпадающее меню при помощи css

Class constructors

Constructs a new instance of Frame that is initially invisible.

Frame(GraphicsConfiguration gc)

Constructs a new, initially invisible Frame with the specified GraphicsConfiguration.

Constructs a new, initially invisible Frame object with the specified title.

Frame(String title, GraphicsConfiguration gc)

Constructs a new, initially invisible Frame object with the specified title and a GraphicsConfiguration.

Class methods

Makes this Frame displayable by connecting it to a native screen resource.

AccessibleContext getAccessibleContext()

Gets the AccessibleContext associated with this Frame.

Deprecated. As of JDK version 1.1, replaced by Component.getCursor().

Gets the state of this frame.

static Frame[] getFrames()

Returns an array of all Frames created by this application.

Returns the image to be displayed as the icon for this frame.

Rectangle getMaximizedBounds()

Gets maximized bounds for this frame.

Gets the menu bar for this frame.

Gets the state of this frame (obsolete).

Gets the title of the frame.

Indicates whether this frame is resizable by the user.

boolean isUndecorated()

Indicates whether this frame is undecorated.

protected String paramString()

Returns a string representing the state of this Frame.

void remove(MenuComponent m)

Removes the specified menu bar from this frame.

Makes this Frame undisplayable by removing its connection to its native screen resource.

void setCursor(int cursorType)

Deprecated. As of JDK version 1.1, replaced by Component.setCursor(Cursor).

void setExtendedState(int state)

Sets the state of this frame.

void setIconImage(Image image)

Sets the image to be displayed as the icon for this window.

void setMaximizedBounds(Rectangle bounds)

Sets the maximized bounds for this frame.

void setMenuBar(MenuBar mb)

Sets the menu bar for this frame to the specified menu bar.

void setResizable(boolean resizable)

Sets whether this frame is resizable by the user.

void setState(int state)

Sets the state of this frame (obsolete).

void setTitle(String title)

Sets the title for this frame to the specified string.

void setUndecorated(boolean undecorated)

Disables or enables decorations for this frame.

Methods inherited

This class inherits methods from the following classes:

Frame Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >

package com.tutorialspoint.gui; import java.awt.*; import java.awt.event.*; public class AwtContainerDemo < private Frame mainFrame; private Label headerLabel; private Label statusLabel; private Panel controlPanel; private Label msglabel; public AwtContainerDemo()< prepareGUI(); >public static void main(String[] args) < AwtContainerDemo awtContainerDemo = new AwtContainerDemo(); awtContainerDemo.showFrameDemo(); >private void prepareGUI() < mainFrame = new Frame("Java AWT Examples"); mainFrame.setSize(400,400); mainFrame.setLayout(new GridLayout(3, 1)); mainFrame.addWindowListener(new WindowAdapter() < public void windowClosing(WindowEvent windowEvent)< System.exit(0); >>); headerLabel = new Label(); headerLabel.setAlignment(Label.CENTER); statusLabel = new Label(); statusLabel.setAlignment(Label.CENTER); statusLabel.setSize(350,100); msglabel = new Label(); msglabel.setAlignment(Label.CENTER); msglabel.setText("Welcome to TutorialsPoint AWT Tutorial."); controlPanel = new Panel(); controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); > private void showFrameDemo() < headerLabel.setText("Container in action: Frame"); final Frame frame = new Frame(); frame.setSize(300, 300); frame.setLayout(new FlowLayout()); frame.add(msglabel); frame.addWindowListener(new WindowAdapter() < public void windowClosing(WindowEvent windowEvent)< frame.dispose(); >>); Button okButton = new Button("Open a Frame"); okButton.addActionListener(new ActionListener() < public void actionPerformed(ActionEvent e) < statusLabel.setText("A Frame shown to the user."); frame.setVisible(true); >>); controlPanel.add(okButton); mainFrame.setVisible(true); > >

Compile the program using command prompt. Go to D:/ > AWT and type the following command.

D:\AWT>javac com\tutorialspoint\gui\AwtContainerDemo.java

If no error comes that means compilation is successful. Run the program using following command.

D:\AWT>java com.tutorialspoint.gui.AwtContainerDemo

Verify the following output

Читайте также:  convert a float number to a whole number in JavaScript

Источник

Class Frame

The size of the frame includes any area designated for the border. The dimensions of the border area may be obtained using the getInsets method, however, since these dimensions are platform-dependent, a valid insets value cannot be obtained until the frame is made displayable by either calling pack or show . Since the border area is included in the overall size of the frame, the border effectively obscures a portion of the frame, constraining the area available for rendering and/or displaying subcomponents to the rectangle which has an upper-left corner location of (insets.left, insets.top) , and has a size of width — (insets.left + insets.right) by height — (insets.top + insets.bottom) .

The default layout for a frame is BorderLayout .

A frame may have its native decorations (i.e. Frame and Titlebar ) turned off with setUndecorated . This can only be done while the frame is not displayable .

In a multi-screen environment, you can create a Frame on a different screen device by constructing the Frame with Frame(GraphicsConfiguration) or Frame(String title, GraphicsConfiguration) . The GraphicsConfiguration object is one of the GraphicsConfiguration objects of the target screen device.

In a virtual device multi-screen environment in which the desktop area could span multiple physical screen devices, the bounds of all configurations are relative to the virtual-coordinate system. The origin of the virtual-coordinate system is at the upper left-hand corner of the primary physical screen. Depending on the location of the primary screen in the virtual device, negative coordinates are possible, as shown in the following figure.

In such an environment, when calling setLocation , you must pass a virtual coordinate to this method. Similarly, calling getLocationOnScreen on a Frame returns virtual device coordinates. Call the getBounds method of a GraphicsConfiguration to find its origin in the virtual coordinate system.

The following code sets the location of the Frame at (10, 10) relative to the origin of the physical screen of the corresponding GraphicsConfiguration . If the bounds of the GraphicsConfiguration is not taken into account, the Frame location would be set at (10, 10) relative to the virtual-coordinate system and would appear on the primary physical screen, which might be different from the physical screen of the specified GraphicsConfiguration .

Frame f = new Frame(GraphicsConfiguration gc); Rectangle bounds = gc.getBounds(); f.setLocation(10 + bounds.x, 10 + bounds.y);
  • WINDOW_OPENED
  • WINDOW_CLOSING :
    If the program doesn’t explicitly hide or dispose the window while processing this event, the window close operation is canceled.
  • WINDOW_CLOSED
  • WINDOW_ICONIFIED
  • WINDOW_DEICONIFIED
  • WINDOW_ACTIVATED
  • WINDOW_DEACTIVATED
  • WINDOW_GAINED_FOCUS
  • WINDOW_LOST_FOCUS
  • WINDOW_STATE_CHANGED

Источник

Оцените статью