Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

SWING container


May 15, 2021 SWING


Table of contents


SWING container

Containers are part of the SWING GUI component. A container provides a space where components can be placed. I n AWT, a container is the component itself, and it adds functionality to add the component itself. Here are some considerations to consider.

  • A child class of a container is called a container. Examples are JPanel, JFrame, and JWindow.

  • Containers can simply add components to themselves.

  • A default layout uses the setLayout method to render in each container that can be overrritten.

Functionally, it can be divided into:

Top containers: JFrame, JApplet, JDialog, JWindow 4

Intermediate containers: J Panel, J Scroll Pane, J SplitPane, J Tool Bar

When you create a GUI interface using the JFrame class, the layout of its components is organized as shown in Figure 1.

SWING container
Figure 1 JFrame window component organization
In Figure 1, the Swing component with Hello needs to be placed on top of the content pane, which is then placed on top of the JFrame top container. T he menu bar can be placed directly on the top container JFrame, rather than through the content pane. The content pane is a transparent, borderless intermediate container.

SWING container:

The following is a list of containers that are commonly used when designing GUI events with SWING.

Serial number Containers and descriptions
1 Panel
JPanel is the simplest container. It provides space where any other component can be placed, including other panels.
2 Frame
JFrame is a top-level window with a title and boundaries.
3 Window
The JWindow object is a top-level window with no boundaries and menu bars.

Now that you understand the syntax of the window component JFrame, let's create a window using the JFrame class. A sk to set the window titled "Java First GUI Program" and add "This is a window created with the JFrame class" text to the window. The implementation code is as follows:

  1. package ch17;
  2. import javax.swing.JFrame;
  3. import javax.swing.JLabel;
  4. import java.awt.*;
  5. public class JFrameDemo extends JFrame
  6. {
  7. public JFrameDemo()
  8. {
  9. setTitle("Java 第一个 GUI 程序"); //设置显示窗口标题
  10. setSize(400,200); //设置窗口显示尺寸
  11. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //置窗口是否可以关闭
  12. JLabel jl=new JLabel("这是使用JFrame类创建的窗口"); //创建一个标签
  13. Container c=getContentPane(); //获取当前窗口的内容窗格
  14. c.add(jl); //将标签组件添加到内容窗格上
  15. setVisible(true); //设置窗口是否可见
  16. }
  17. public static void main(String[] agrs)
  18. {
  19. new JFrameDemo(); //创建一个实例化对象
  20. }
  21. }


The JFrameDemo class created by the above code inherits the JFrame class, so the JFrameDemo class can use the JFrame class method directly. The setTitle() method is used to set the window title, and the setDefaultCloseOperation() method is used to set the response mode, i.e. to exit the program when the Close button is clicked.

Use the JLabel class to create a label object jl in the construction method, whose parameters are the text cues of the label. T he getContentPane() method of the JFrame framework gets the content pane object and adds the label to the content pane using the add() method. The final setVisible() method is the method inherited from the parent class.

The result of the window in which the program runs is shown in Figure 2.

SWING container
Figure 2 The results of the window run