Advertisement



< Prev
Next >



Frame




Frame is a top-level container which is used to hold components in it. Components such as a button, checkbox, radio button, menu, list, table etc.




Constructors of Frame


Constructor Description
public Frame() Creates a Frame window with no name.
public Frame(String name) Creates a Frame window with a name.





Frame methods


Methods Description
public void add(Component comp) This method adds the component, comp, to the container Frame.
public void setLayout(LayoutManager object) This method sets the layout of the components in a container, Frame.
public void remove(Component comp) This method removes a component, comp, from the container, Frame.
public void setSize(int widthPixel, int heightPixel) This method sets the size of a Frame in terms of pixels.





BorderLayout is the default layout manager of Frame.


The default layout of Frame by which it positions the components in it is BoderLayout manager. Hence, if we add components to a Frame without calling it's setLayout() method, these components are automatically added to the center region using BorderLayout manager.

Note Remember, using the BorderLayout manager, only one component can be placed in a region, hence if multiple elements are added to a region, only the last element will be visible.

import java.awt.*;

class FrameEx
{
Frame frame;
Label label;
Button button;

FrameEx()
{
frame = new Frame("Frame");
label = new Label("Hello there, how are you today? :-)");
button = new Button("Button1");

//Adding JLabel and JButton to Frame, which will added to BorderLayout.CENTER region of Frame.
frame.add(label);
frame.add(button);

frame.setSize(210,250);
frame.setVisible(true);
}

public static void main(String... ar)
{
new FrameEx();
}

}
In the last code, two components i.e. Label and Button are added top-level container, Frame, without calling its setLayout() . These components are by default added to BorderLayout.CENTER region of Frame . Hence, only the last component added, i.e. JButton will be visible. When you run the code, you are presented a window that only shown a button.

Figure 1



Advertisement




Setting the layout of Frame.



import java.awt.*;

class FrameEx2 
{
Frame frame;
Label label;
Button button;

FrameEx2()
{
frame = new Frame("Frame");
label = new Label("Hello there, how are you today? :-)");
button = new Button("Button1");


//Setting the layout of  components in Frame to FlowLayout
frame.setLayout(new FlowLayout()); 

frame.add(label);
frame.add(button);
frame.setSize(210,250);
frame.setVisible(true);
}

public static void main(String...ar)
{
new FrameEx2();
}

}
When you run the code, you are presented a window that contains a top-level container, Frame, which includes two components that are positioned in Frame using FlowLayout manager -

Figure 2





How to close a Frame window in AWT?


In the last two programs of Frame, we couldn't close the Frame window. In the upcoming code, we are going to see how we can successfully close the Frame window by extending a WindowAdapter class and overriding its windowClosing() method.

import java.awt.*;
import java.awt.event.*;


class FrameEx3 extends WindowAdapter
{
Frame frame;
Button button1;
Button button2;

FrameEx3()
{
frame = new Frame("Frame");
button1 = new Button("Button1");
button2 = new Button("Button2");

//Setting the layout of Frame to FlowLayout
frame.setLayout(new FlowLayout());

//Adding JLabel and JButton to Frame, which will added to BorderLayout.CENTER region of Frame.
frame.add(button1);
frame.add(button2);

frame.addWindowListener(this);

frame.setSize(210,250);
frame.setVisible(true);
}

public static void main(String... ar)
{
new FrameEx3();
}

public void windowClosing(WindowEvent we)
{
frame.dispose();
}

}
When you run the code, you are presented a window that contains a top-level container, Frame, which includes two buttons that are positioned in Frame using FlowLayout manager, as shown in the figure 3. When you click on (x) on Frame window to close it, the dispose() method within the windowClosing() method is called and Frame window is successfully closed.

Figure 3




Please share this article -





< Prev
Next >
< TextListener
MenuBar >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement