| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 607 人关注过本帖
标题:求大神指教,怎样把日期代码和时钟在同一个框里显示出来啊?
只看楼主 加入收藏
男孩的诗
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2014-4-1
结帖率:50%
收藏
已结贴  问题点数:18 回复次数:3 
求大神指教,怎样把日期代码和时钟在同一个框里显示出来啊?
我想把日期和时钟加到一起,在同一个框里显示,可是不知道怎么弄,就像windows的日期一样显示出来,布局上也不清楚用哪一个,求大神指教啊。
代码如下:
日期:package fancy;
 
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
 
public class MyCalendar extends JApplet {
 
    public static final String WEEK_SUN = "星期日";
    public static final String WEEK_MON = "星期一";
    public static final String WEEK_TUE = "星期二";
    public static final String WEEK_WED = "星期三";
    public static final String WEEK_THU = "星期四";
    public static final String WEEK_FRI = "星期五";
    public static final String WEEK_SAT = "星期六";
 
    public static final Color background = Color.white;
    public static final Color foreground = Color.black;
    public static final Color headerBackground = Color.blue;
    public static final Color headerForeground = Color.white;
    public static final Color selectedBackground = Color.blue;
    public static final Color selectedForeground = Color.white;
 
    private JPanel cPane;
    private JLabel yearsLabel;
    private JSpinner yearsSpinner;
    private JLabel monthsLabel;
    private JComboBox monthsComboBox;
    private JTable daysTable;
    private AbstractTableModel daysModel;
    private Calendar calendar;
   
    public MyCalendar() {
        cPane = (JPanel) getContentPane();
    }
 
    public void init() {
        cPane.setLayout(new BorderLayout());
 
        calendar = Calendar.getInstance();
        calendar = Calendar.getInstance();
        yearsLabel = new JLabel("年份: ");
        yearsSpinner = new JSpinner();
        yearsSpinner.setEditor(new JSpinner.NumberEditor(yearsSpinner, "0000"));
        yearsSpinner.setValue(new Integer(calendar.get(Calendar.YEAR)));
        yearsSpinner.addChangeListener(new ChangeListener() {
                public void stateChanged(ChangeEvent changeEvent) {
                    int day = calendar.get(Calendar.DAY_OF_MONTH);
                    calendar.set(Calendar.DAY_OF_MONTH, 1);
                    calendar.set(Calendar.YEAR, ((Integer) yearsSpinner.getValue()).intValue());
                    int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                    calendar.set(Calendar.DAY_OF_MONTH, day > maxDay ? maxDay : day);
                    updateView();
                }
            });
 
        JPanel yearMonthPanel = new JPanel();
        cPane.add(yearMonthPanel, BorderLayout.NORTH);
        yearMonthPanel.setLayout(new BorderLayout());
        yearMonthPanel.add(new JPanel(), BorderLayout.CENTER);
        JPanel yearPanel = new JPanel();
        yearMonthPanel.add(yearPanel, BorderLayout.WEST);
        yearPanel.setLayout(new BorderLayout());
        yearPanel.add(yearsLabel, BorderLayout.WEST);
        yearPanel.add(yearsSpinner, BorderLayout.CENTER);
 
        monthsLabel = new JLabel("月份: ");
        monthsComboBox = new JComboBox();
        for (int i = 1; i <= 12; i++) {
            monthsComboBox.addItem(new Integer(i));
        }
        monthsComboBox.setSelectedIndex(calendar.get(Calendar.MONTH));
        monthsComboBox.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent actionEvent) {
                    int day = calendar.get(Calendar.DAY_OF_MONTH);
                    calendar.set(Calendar.DAY_OF_MONTH, 1);
                    calendar.set(Calendar.MONTH, monthsComboBox.getSelectedIndex());
                    int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                    calendar.set(Calendar.DAY_OF_MONTH, day > maxDay ? maxDay : day);
                    updateView();
                }
            });
        JPanel monthPanel = new JPanel();
        yearMonthPanel.add(monthPanel, BorderLayout.EAST);
        monthPanel.setLayout(new BorderLayout());
        monthPanel.add(monthsLabel, BorderLayout.WEST);
        monthPanel.add(monthsComboBox, BorderLayout.CENTER);
 
        daysModel = new AbstractTableModel() {
                public int getRowCount() {
                    return 7;
                }
 
                public int getColumnCount() {
                    return 7;
                }
 
                public Object getValueAt(int row, int column) {
                    if (row == 0) {
                        return getHeader(column);
                    }
                    row--;
                    Calendar calendar = (Calendar) MyCalendar.this.calendar.clone();
                    calendar.set(Calendar.DAY_OF_MONTH, 1);
                    int dayCount = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                    int moreDayCount = calendar.get(Calendar.DAY_OF_WEEK) - 1;
                    int index = row * 7 + column;
                    int dayIndex = index - moreDayCount + 1;
                    if (index < moreDayCount || dayIndex > dayCount) {
                        return null;
                    } else {
                        return new Integer(dayIndex);
                    }
                }
            };
 
        daysTable = new CalendarTable(daysModel, calendar);
        daysTable.setCellSelectionEnabled(true);
        daysTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 
        daysTable.setDefaultRenderer(daysTable.getColumnClass(0), new TableCellRenderer() {
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                                                               boolean hasFocus, int row, int column) {
                    String text = (value == null) ? "" : value.toString();
                    JLabel cell = new JLabel(text);
                    cell.setOpaque(true);
                    if (row == 0) {
                        cell.setForeground(headerForeground);
                        cell.setBackground(headerBackground);
                    } else {
                        if (isSelected) {
                            cell.setForeground(selectedForeground);
                            cell.setBackground(selectedBackground);
                        } else {
                            cell.setForeground(foreground);
                            cell.setBackground(background);
                        }
                    }
 
                    return cell;
                }
            });
        updateView();
 
        cPane.add(daysTable, BorderLayout.CENTER);
    }
 
    public static String getHeader(int index) {
        switch (index) {
        case 0:
            return WEEK_SUN;
        case 1:
            return WEEK_MON;
        case 2:
            return WEEK_TUE;
        case 3:
            return WEEK_WED;
        case 4:
            return WEEK_THU;
        case 5:
            return WEEK_FRI;
        case 6:
            return WEEK_SAT;
        default:
            return null;
        }
    }
 
    public void updateView() {
        daysModel.fireTableDataChanged();
        daysTable.setRowSelectionInterval(calendar.get(Calendar.WEEK_OF_MONTH),
                                          calendar.get(Calendar.WEEK_OF_MONTH));
        daysTable.setColumnSelectionInterval(calendar.get(Calendar.DAY_OF_WEEK) - 1,
                                             calendar.get(Calendar.DAY_OF_WEEK) - 1);
    }

    public static class CalendarTable extends JTable {

        private Calendar calendar;

        public CalendarTable(TableModel model, Calendar calendar) {
            super(model);
            this.calendar = calendar;
        }

        public void changeSelection(int row, int column, boolean toggle, boolean extend) {
            super.changeSelection(row, column, toggle, extend);
            if (row == 0) {
                return;
            }
            Object obj = getValueAt(row, column);
            if (obj != null) {
                calendar.set(Calendar.DAY_OF_MONTH, ((Integer)obj).intValue());
            }
        }

    }
 
    public static void main(String[] args) {
        JFrame frame = new JFrame("Calendar Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MyCalendar myCalendar = new MyCalendar();
        myCalendar.init();
        frame.getContentPane().add(myCalendar);
        frame.setSize(500, 175);
        frame.show();
    }
 
}



时钟:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.Timer;
public class TestClock{ public static void main(String[] args)
{  Frame win = new Frame();
    Label label = new Label();
     win.add(new Clock(),BorderLayout.CENTER);   
    win.add(label,BorderLayout.SOUTH);   
    win.setVisible(true);  
    win.setSize(246,280);  
    win.validate();  
    win.addWindowListener(    new WindowAdapter()
    {     public void windowClosing(WindowEvent e)
      {      System.exit(0);
       }
        }  );
   new Thread(new TestThread(label)).start();
 }
}
class TestThread implements Runnable{ Label label;
     TestThread(Label label)
    {  this.label = label;
 }  
public void run()
{  while(true)
    {   label.setText(new SimpleDateFormat("HH:mm:ss").format(new Date()));
   try
{    Thread.sleep(1000);  
 }
catch (InterruptedException e) {    e.printStackTrace();  
 }  
}
}
}
class Clock extends Canvas implements ActionListener{ /**  *   */ private static final long serialVersionUID = 1L;
 Date date;
Timer secondTime;

 int hour,munite,second; Line2D secondLine,muniteLine,hourLine;
int a,b,c;
double  pointSX[] = new double[60],   pointSY[] = new double[60],   pointMX[] = new double[60],   pointMY[] = new double[60],   pointHX[] = new double[60],   pointHY[] = new double[60];
 Clock(){  secondTime = new Timer(1000,this);
  pointSX[0]=0;  
  pointSY[0]=-100;
  pointMX[0]=0;
  pointMY[0]=-90;
  pointHX[0]=0;  
  pointHY[0]=-70;  
  double adgle=6*Math.PI/180;
  for(int i=0;i<59;i++)
{  
 pointSX[i+1]=pointSX[i]*Math.cos(adgle)-Math.sin(adgle)*pointSY[i];
   pointSY[i+1]=pointSY[i]*Math.cos(adgle)+Math.sin(adgle)*pointSX[i];
   pointMX[i+1]=pointMX[i]*Math.cos(adgle)-Math.sin(adgle)*pointMY[i];
   pointMY[i+1]=pointMY[i]*Math.cos(adgle)+Math.sin(adgle)*pointMX[i];   pointHX[i+1]=pointHX[i]*Math.cos(adgle)-Math.sin(adgle)*pointHY[i];   pointHY[i+1]=pointHY[i]*Math.cos(adgle)+Math.sin(adgle)*pointHX[i];  }  for(int i=0;i<60;i++){   pointSX[i] = pointSX[i]+120;   pointSY[i] = pointSY[i]+120;   pointMX[i] = pointMX[i]+120;   pointMY[i] = pointMY[i]+120;   pointHX[i] = pointHX[i]+120;   pointHY[i] = pointHY[i]+120;  }  secondLine = new Line2D.Double(0,0,0,0);  muniteLine = new Line2D.Double(0,0,0,0);  hourLine = new Line2D.Double(0,0,0,0);  secondTime.start(); }  public void paint(Graphics g){  for(int i=0;i<60;i++){   int m = (int)pointSX[i];   int n = (int)pointSY[i];   if(i%5==0){    g.setColor(Color.red);    g.fillOval(m-4, n-4, 8, 8);   }else{    g.setColor(Color.cyan);    g.fillOval(m-2, n-2, 4, 4);   }   g.fillOval(115, 115, 10, 10);   Graphics2D g_2d = (Graphics2D)g;   g_2d.setColor(Color.red);   g_2d.draw(secondLine);   BasicStroke bs= new BasicStroke(3f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER);
   g_2d.setStroke(bs);
   g_2d.setColor(Color.blue);
   g_2d.draw(muniteLine);
   bs = new BasicStroke(6f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER);
   g_2d.setStroke(bs);   
   g_2d.setColor(Color.green);  
   g_2d.draw(hourLine);
 }
 }
public void actionPerformed(ActionEvent e) {  if(e.getSource() == secondTime){   date = new Date();
   String s = date.toString();
     hour = Integer.parseInt(s.substring(11,13));  
     munite = Integer.parseInt(s.substring(14,16));
     second=Integer.parseInt(s.substring(17,19));
        int h = hour%12;
            a = second;  
            b = munite;
            c = h*5+munite/12;
       secondLine.setLine(120,120,(int)pointSX[a],(int)pointSY[a]);
       muniteLine.setLine(120,120,(int)pointMX[b],(int)pointMY[b]);
       hourLine.setLine(120,120,(int)pointHX[c],(int)pointHY[c]);
       repaint();  
           }
       }
}
搜索更多相关主题的帖子: windows package public import 
2014-05-31 10:41
砖家的谎言
Rank: 12Rank: 12Rank: 12
等 级:禁止访问
威 望:30
帖 子:693
专家分:3898
注 册:2013-12-6
收藏
得分:18 
几种布局都可以,其实你不用布局就默认是边框布局,然后根据组件和容器的关系进行添加!

我不是砖家,要努力成为砖家。
2014-05-31 15:36
男孩的诗
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2014-4-1
收藏
得分:0 
回复 2 楼 砖家的谎言
我是初学者,还不知道怎样把两个代码连起来,又不能简单的把代码相加,能不能帮我写一下啊?谢谢啦!!
2014-06-01 10:35
男孩的诗
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2014-4-1
收藏
得分:0 
大神在哪啊,求指点啊,如何将这两个代码编成一个代码?中间怎么能连接起来?
2014-06-01 14:14
快速回复:求大神指教,怎样把日期代码和时钟在同一个框里显示出来啊?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021472 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved