Advertisement
Constructor | Description |
---|---|
public Frame() | Creates a Frame window with no name. |
public Frame(String name) | Creates a Frame window with a name. |
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. |
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.
Advertisement
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 -
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.
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement