Java stuy day two -- Demo about show instance tme in windows with Custom Interface

Code:
part1:

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

public class ClockPanel extends JPanel {
	public ClockPanel(){
		super();
		String currentTime = getTime();
		JLabel jl = new JLabel("Time: ");
		JLabel jll = new JLabel(currentTime);
		add(jl);
		add(jll);
	}
	final String getTime(){
		String time="";
		Calendar now = Calendar.getInstance();
		int hour = now.get(Calendar.HOUR_OF_DAY);
		int minute = now.get(Calendar.MINUTE);
		int month = now.get(Calendar.MONTH);
		int day = now.get(Calendar.DAY_OF_MONTH);
		int year = now.get(Calendar.YEAR);
		switch (month) {
			case 0:
				time = "January";
				break;
			case 1:
				time = "February";
				break;
			case 2:
				time = "March";
				break;
			case 3:
				time = "April";
				break;
			case 4:
				time = "May";
				break;
			case 5:
				time = "June";
				break;
			case 6:
				time = "July";
				break;
			case 7:
				time = "August";
				break;
			case 8:
				time = "September";
				break;
			case 9:
				time = "October";
				break;
			case 10:
				time = "November";
				break;
			case 11:
				time = "December";	
		}
		time += " " + day + " " + year + " " + hour + ": " + minute;
		return time;
	}
}

part2:

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

public class ClockFrame extends JFrame {
	public ClockFrame(){
		super();
		setTitle("ClockFrame");
		setSize(300, 300);
		setLookAndFeel();
		FlowLayout flo = new FlowLayout();
		ClockPanel time = new ClockPanel();
		add(time);
		setVisible(true);
	}
	private void setLookAndFeel(){
		try{
			UIManager.setLookAndFeel (
			"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
			);
		}catch (Exception exc){
			//ignore Exception
		}
	}
	public static void main(String[] args) {
		ClockFrame clf = new ClockFrame();
	}
}

Result:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值