【综合练习2】霓虹灯(标签水平居中JLabel.setHorizontalAlignment(SwingConstants.CENTER);标签文字颜色JLabel.setDoreground())

【综合练习2】霓虹灯
创建一个小窗体,显示“流·浪·地·球”字样,同时让字体样式、字体颜色以及背景板颜色每3秒发生一次变化。

/*【综合练习2】霓虹灯
 * 创建一个小窗体,显示“流·浪·地·球”字样,同时让字体样式、字体颜色
 * 以及背景板颜色每3秒发生一次变化。
 * 
 * */

package dxc;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import java.awt.Color;
import java.awt.Font;
import java.awt.BorderLayout;

public class Zhlx2 extends JFrame{
	
	private JPanel contentPane;
	private JLabel lblWords;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Zhlx2 mrsoftFrame = new Zhlx2();
		mrsoftFrame.setVisible(true);
	}
	
	public Zhlx2() {
		setResizable(false);
		setTitle("霓虹灯之流浪地球");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 350, 150);
		
		//	内容面板
		contentPane = new JPanel();
		setContentPane(contentPane);
		contentPane.setBackground(Color.WHITE);
		contentPane.setLayout(new BorderLayout(0, 0));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		
		//	文字标签
		lblWords = new JLabel("流 · 浪 · 地 · 球");
		contentPane.add(lblWords,BorderLayout.CENTER);
		lblWords.setFont(new Font("华文行楷", Font.PLAIN, 28));
		lblWords.setHorizontalAlignment(SwingConstants.CENTER);
		
		//	创建背景色线程子类,字体样式子类
		Thread colorThread = new BackgroundColor2(contentPane);
		Thread styleThread = new WordsStyle2(lblWords);
		
		colorThread.start();
		styleThread.start();
	}

}

class BackgroundColor2 extends Thread{
	private JPanel contentPane;
	
	public BackgroundColor2(JPanel contentPane) {
		this.contentPane = contentPane;
	}
	
	@Override
	public void run() {
		while(true) {
			try {
				Thread.sleep(3000);
			}catch(InterruptedException e) {
				e.printStackTrace();
			}
			contentPane.setBackground(Color.YELLOW);
			try {
				Thread.sleep(3000);
			}catch(InterruptedException e) {
				e.printStackTrace();
			}
			contentPane.setBackground(Color.CYAN);
			try {
				Thread.sleep(3000);
			}catch(InterruptedException e) {
				e.printStackTrace();
			}
			contentPane.setBackground(Color.MAGENTA);
			try {
				Thread.sleep(3000);
			}catch(InterruptedException e) {
				e.printStackTrace();
			}
			contentPane.setBackground(Color.WHITE);
		}
		
	}
	
}

class WordsStyle2 extends Thread{
	private JLabel lblWords;
	
	public WordsStyle2(JLabel lblWords) {
		this.lblWords = lblWords;
	}
	
	@Override
	public void run() {
		while(true) {
			try {
				Thread.sleep(3000);
			}catch(InterruptedException e) {
				e.printStackTrace();
			}
			lblWords.setFont(new Font("方正舒体",Font.BOLD,28));
			lblWords.setForeground(Color.BLUE);
			
			try {
				Thread.sleep(3000);
			}catch(InterruptedException e) {
				e.printStackTrace();
			}
			lblWords.setFont(new Font("华文新魏",Font.ITALIC|Font.BOLD,28));
			lblWords.setForeground(Color.BLUE);
			
			try {
				Thread.sleep(3000);
			}catch(InterruptedException e) {
				e.printStackTrace();
			}
			lblWords.setFont(new Font("华文隶书",Font.ITALIC,28));
			lblWords.setForeground(Color.BLUE);
			
			try {
				Thread.sleep(3000);
			}catch(InterruptedException e) {
				e.printStackTrace();
			}
			lblWords.setFont(new Font("华文行楷",Font.PLAIN,28));
			lblWords.setForeground(Color.BLUE);
			
		}
	}
}

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值