陈桥驿站 陈桥驿站

Java课程设计:模仿Windows经典时钟

Java 阅读 ( 286 ) 陈桥驿站

简介

最近很多童鞋的Java课程设计需要写一个Windows时钟。就是WindowsXP和Windows7经典的时钟布局。本文用SWING实现包含年份月份对日历的显示以及实时时钟的显示[JavaWindowsXPClock.zip]。

WindowsXP显示效果

MacOS显示效果

代码

Tab分页选项卡

//顶部标签
JTabbedPane pane = new JTabbedPane();
pane.add("时间和日期", dateTimePanel);
pane.add("时区", new JPanel());
pane.add("Internet时间", new JPanel());

动态绘制时钟

private static final long serialVersionUID = -4055293104208441985L;
@Override
public void paint(Graphics g) {
    super.paint(g);
    g.fillOval(96, 72, 4, 4);
    Calendar c = Calendar.getInstance();
    for(int i=0;i<360;i++){
        if(i%5 == 0){
            //小刻度
            g.fillOval(96 + (int)(72*Math.sin(i*1./360.*2*Math.PI)), 72 - (int)(72*Math.cos(i*1./360.*2*Math.PI)), 2, 2);
        }
        if(i%30 == 0){
            //大刻度
            g.fillOval(96 + (int)(72*Math.sin(i*1./360.*2*Math.PI)), 72 - (int)(72*Math.cos(i*1./360.*2*Math.PI)), 3, 3);
        }
    }
    int h = c.get(Calendar.HOUR_OF_DAY) % 12;
    int m = c.get(Calendar.MINUTE);
    int s = c.get(Calendar.SECOND);
    g.setColor(Color.RED);
    g.drawLine(96, 72, 96 + 2*(int)(72*Math.sin((h+m*1./60.)*30 *1./360.*2*Math.PI))/3, 72 - 2*(int)(72*Math.cos((h+m*1./60.)*30 *1./360.*2*Math.PI))/3);
    g.drawLine(94, 72, 96 + 2*(int)(72*Math.sin((h+m*1./60.)*30 *1./360.*2*Math.PI))/3, 72 - 2*(int)(72*Math.cos((h+m*1./60.)*30 *1./360.*2*Math.PI))/3);
    g.drawLine(98, 72, 96 + 2*(int)(72*Math.sin((h+m*1./60.)*30 *1./360.*2*Math.PI))/3, 72 - 2*(int)(72*Math.cos((h+m*1./60.)*30 *1./360.*2*Math.PI))/3);
    g.setColor(Color.BLUE);
    g.drawLine(95, 72, 96 + 3*(int)(72*Math.sin((m+s*1./60.)*6 *1./360.*2*Math.PI))/4, 72 - 3*(int)(72*Math.cos((m+s*1./60.)*6 *1./360.*2*Math.PI))/4);
    g.drawLine(97, 72, 96 + 3*(int)(72*Math.sin((m+s*1./60.)*6 *1./360.*2*Math.PI))/4, 72 - 3*(int)(72*Math.cos((m+s*1./60.)*6 *1./360.*2*Math.PI))/4);
    g.setColor(Color.GREEN);
    g.drawLine(96, 72, 96 + 4*(int)(72*Math.sin(s*6*1./360.*2*Math.PI))/5, 72 - 4*(int)(72*Math.cos(s*6*1./360.*2*Math.PI))/5);
}

月份和年份的下拉选择框

private static JPanel comboxs(){
    //日期中的月份和年份下拉菜单
    String months[] = new String[]{"一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"};
    JComboBox<String> month = new JComboBox<String>(months);
    Vector<String> years = new Vector<String>();
    for(int i=1970;i<2050;i++){
        years.add(String.valueOf(i));
    }
    JComboBox<String> year = new JComboBox<String>(years);
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(1, 2));
    month.setSelectedIndex(tableMonth);
    year.setSelectedIndex(tableYear-1970);
    month.addItemListener(new ItemListener() {
        //月份的监听
        @Override
        public void itemStateChanged(ItemEvent e) {
            @SuppressWarnings("unchecked")
            JComboBox<String> box = (JComboBox<String>)e.getSource();
            tableMonth = box.getSelectedIndex();
            table.setModel(updateModel());
            
        }
    });
    year.addItemListener(new ItemListener() {
        //年的监听
        @Override
        public void itemStateChanged(ItemEvent e) {
            @SuppressWarnings("unchecked")
            JComboBox<String> box = (JComboBox<String>)e.getSource();
            tableYear = Integer.parseInt(String.valueOf(box.getSelectedItem()));
            table.setModel(updateModel());
        }
    });
    panel.add(month);
    panel.add(year);
    return panel;
}

更新时间的线程

//更新时间线程
new Thread(new Runnable() {
    @Override
    public void run() {
        while(true){
            try {
                Thread.sleep(1000);
                time.setText(new SimpleDateFormat("HH:mm:ss").format(new Date()));
                clock.repaint();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}).start();

下拉框的监听和JTable的更新

month.addItemListener(new ItemListener() {
    //月份的监听
    @Override
    public void itemStateChanged(ItemEvent e) {
        @SuppressWarnings("unchecked")
        JComboBox<String> box = (JComboBox<String>)e.getSource();
        tableMonth = box.getSelectedIndex();
        table.setModel(updateModel());
        
    }
});
year.addItemListener(new ItemListener() {
    //年的监听
    @Override
    public void itemStateChanged(ItemEvent e) {
        @SuppressWarnings("unchecked")
        JComboBox<String> box = (JComboBox<String>)e.getSource();
        tableYear = Integer.parseInt(String.valueOf(box.getSelectedItem()));
        table.setModel(updateModel());
    }
});

显示日期的表格

private static DefaultTableModel updateModel(){
    //更新Model
    String strings[][] = new String[6][7];
    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, tableYear);
    c.set(Calendar.MONTH, tableMonth);
    c.set(Calendar.DAY_OF_MONTH, 1);
    int firstDay = c.get(Calendar.DAY_OF_WEEK);
    int print = 1;
    //第一行
    for(int i=1;i<firstDay-1;i++){
        strings[0][i] = "";
    }
    for(int i=firstDay-1;i<7;i++){
        strings[0][i] = String.valueOf(print);
        print ++;
    }
    //中间的行
    int rows = 1;
    int column = 0;
    for(int i=print;i<=c.getActualMaximum(Calendar.DAY_OF_MONTH);i++){
        strings[rows][column] = String.valueOf(i);
        column++;
        if(column == 7){
            rows ++;
            column = 0;
        }
    }
    DefaultTableModel model = new DefaultTableModel(strings, new String[]{"日", "一", "二", "三", "四", "五", "六"});
    return model;
}
jrotty WeChat Pay

微信打赏

jrotty Alipay

支付宝打赏

文章二维码

扫描二维码,在手机上阅读!

本文基于《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权
文章链接:http://www.cctv3.net/archives/JavaWindowsXPClock.html (转载时请注明本文出处及文章链接)

Java
发表新评论
陈桥驿站已萌萌哒运行
© 2020百度云提供云加速服务 阿里云提供域名解析和云计算服务 鲁ICP备18036467号-2
PREVIOUS NEXT
雷姆
拉姆