The Frame class extends Window to define a main application window that can have a menu bar.
In the context of Core Java, specifically in the context of GUI (Graphical User Interface) programming using Swing, a Window and a Frame refer to different classes provided by the Java Foundation Classes (JFC) library.
- Frame:
- A
Frame
is a top-level container that represents a standalone application window. - It can have a title bar, borders, and can be resized, maximized, and minimized.
- The
Frame
class is part of the AWT (Abstract Window Toolkit) package, which is an older GUI toolkit in Java.
- A
Example of creating a Frame:
import java.awt.Frame;
public class MyFrame extends Frame {
public MyFrame(String title) {
super(title);
// Other frame initialization code goes here
}
public static void main(String[] args) {
MyFrame myFrame = new MyFrame(“My Frame”);
myFrame.setSize(400, 300);
myFrame.setVisible(true);
}
}
- Window:
- A
Window
is also a top-level container, but it is a more generic container than aFrame
. - A
Window
doesn’t have a title bar, borders, or other decorations that aFrame
typically has. - The
Window
class is part of the Swing package, which is a more modern and feature-rich GUI toolkit in Java.
- A
Example of creating a Window:
import javax.swing.JFrame;
public class MyWindow extends JFrame {
public MyWindow(String title) {
super(title);
// Other window initialization code goes here
}
public static void main(String[] args) {
MyWindow myWindow = new MyWindow(“My Window”);
myWindow.setSize(400, 300);
myWindow.setVisible(true);
}
}
In summary, the main difference is that Frame
is part of AWT and provides a traditional window with title and decorations, while Window
is part of Swing and is a more generic container without these decorations. In modern Java GUI development, Swing is often preferred over AWT due to its richer set of components and features.