GUI programming study notes 9-Swing related (simple creation of windows, setting colors, adding labels)

GUI programming study notes nine-Swing related (simple operation)


Introduction

  • Graphical interface package newly developed to solve the problems of AWT. Swing is an improvement and expansion of AWT.
  • The general usage is similar to that of AWT, except that there is something more. When using it, add a j

Simply create windows, set colors, add labels

package pers.ylw.lesson04;

import javax.swing.*;
import java.awt.*;

public class JFrameDemo {

    //init() 初始化
    public void init(){
        JFrame jFrame = new JFrame("这是一个JFrame窗口");
        jFrame.setVisible(true);
        jFrame.setBounds(100,100,300,300);

        //设置文字标签
        JLabel jLabel = new JLabel("文字");
        jLabel.setHorizontalAlignment(SwingConstants.CENTER); //设置水平对齐,SwingConstants.CENTER居中
        jFrame.add(jLabel);

        //获得容器,在这里面设置颜色之类的才有效
        Container container = jFrame.getContentPane();
        container.setBackground(Color.YELLOW);


        //关闭事件,和AWT不同的是,这里已经帮我们写好了关闭事件
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //默认关闭事件,EXIT_ON_CLOSE是程序关闭,窗口关闭
    }

    public static void main(String[] args) {
        new JFrameDemo().init();
    }
}

Published 318 original articles · Like 44 · Visitors 20,000+

Guess you like

Origin blog.csdn.net/qq_43594119/article/details/105704521