Swing Top-level container

Source: Internet
Author: User


The top-level container is worth a container component that accommodates other containers, including the JFrame class, Jwindows class, JDialog class, JApplet, and so on.
Top-level container classes in swing
Swing offers three top-level container classes: Jframe,jdialog and JApplet. When using these container classes, keep in mind the following guidelines.
To be displayed on the screen, each GUI component must be part of a "container-level".
Each GUI component can belong to only one container, and if the component is already in one container and attempting to add it to another container, then the component should be removed from the first container and then added to 21 containers.
Various visual components are not placed directly in the top-level container, but are placed in the container's content panel.
You can choose to add a menu bar to the top-level container, and the menu bar should be in the top container by convention, but must be outside the content panel.
Swing also has an internal form named JInternalFrame that mimics JFrame, but the inner form is not really the top-level container.
Let's create a form with a menu bar and a content template:

Import java.awt.*;
Import java.awt.event.*;
Import javax.swing.*;
Topleveldemo.java no additional files required

public class Topleveldemo {
Create a GUI interface and display it. In a thread-safe consideration, this method should be called from the event distribution thread
private static void Createandshowgui () {
Create and set up a form
JFrame frame=new JFrame ("Topleveldemo");
Create a Form object with a caption
Set the program to exit automatically when the form is closed
Frame.setdefaultcloseoperation (Jframe.exit_on_close);
Create a menu bar and set its background to green.
JMenuBar Greenmenubar =new JMenuBar ();
Greenmenubar.setopaque (TRUE);//Set the menu background to opaque
Setting the menu bar background color
Greenmenubar.setbackground (New Color (154,165,107));
Set the menu bar preference size
Greenmenubar.setpreferredsize (New Dimension (200,20));
Create a yellow label and put it in the content template
JLabel yellowlabel=new JLabel ();//Create a Label object
Yellowlabel.setopaque (TRUE);//Set the label background to opaque
Set the label background color
Yellowlabel.setbackground (New Color (248,210,101));
Set Label Preference size
Yellowlabel.setpreferredsize (New Dimension (200,180));
Set up the menu bar and add labels to the content template
Frame.setjmenubar (Greenmenubar);//Add the menu bar to the form
Add a label to a form's content template
Frame.getcontentpane (). Add (Yellowlabel,borderlayout.center);
Show form
Frame.pack ();
Frame.setvisible (TRUE);
}
public static void Main (String []args) {
To schedule a job for the event distribution thread, create a GUI that shows the program
Javax.swing.SwingUtilities.invokeLater (New Runnable () {
public void Run () {
Createandshowgui ();
}
});
}

}

Although the sample program uses JFrame in a standalone application, the same principle applies to applets and JDialog

Swing Top-level container

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.