14day

day14

swing

窗口和面板

package com.wang.lesson04;

import org.omg.CosNaming.BindingIterator;

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

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

标签居中

package com.wang.lesson04;

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

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

       //设置文字
       JLabel label = new JLabel("欢迎来到德莱联盟");
       this.add(label);

       //文本居中
       label.setHorizontalAlignment(0);
       //获得容器
       Container contentPane = this.getContentPane();
       contentPane.setBackground(Color.GREEN);
  }
}

 

posted @ 2021-04-19 20:21  卧剑之鱼  阅读(47)  评论(0)    收藏  举报