Swing

Swing

窗口、面板

package com.hua.demo4;

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

public class JFrameDemo {
    //init(); 初始化
    public void init(){
        //顶级窗口
        JFrame jf = new JFrame("这是一个JFrame窗口");
        jf.setVisible(true);
        jf.setBounds(100,100,300,200);
        jf.setBackground(Color.LIGHT_GRAY);
        //设置文字Jlabel
        JLabel label = new JLabel("这是一个label");
        jf.add(label);
        //关闭事件
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);;
    }
    public static void main(String[] args) {
        //建立一个窗口
        new JFrameDemo().init();
    }
}

标签居中

package com.hua.demo4;

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

public class JFrameDemo02 {
    public static void main(String[] args) {
        new MyJFrame2().init();
    }
}
class MyJFrame2 extends JFrame{
    public void init(){
        this.setVisible(true);
        this.setBounds(100,100,200,100);

        JLabel jlabel = new JLabel("这是一个Jlabel");
        this.add(jlabel);

        //让文本标签居中  设置水平对齐
        jlabel.setHorizontalAlignment(SwingConstants.CENTER);

        //获得一个容器
        Container container = this.getContentPane();
        container.setBackground(Color.green);
    }
}

 

posted @ 2022-01-19 23:44  少时凌云志  阅读(106)  评论(0)    收藏  举报