标题:各位,帮个忙!
只看楼主
wlf3861069
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-10-31
 问题点数:0 回复次数:6 
各位,帮个忙!

在这个日历代码上加个时钟显示功能,怎么做?各位帮帮忙!
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 = "SUN";
public static final String WEEK_MON = "MON";
public static final String WEEK_TUE = "TUE";
public static final String WEEK_WED = "WED";
public static final String WEEK_THU = "THU";
public static final String WEEK_FRI = "FRI";
public static final String WEEK_SAT = "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("Year: ");
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("Month: ");
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(240, 172);
frame.show();
}

}

搜索更多相关主题的帖子: package public import 日历 
2006-01-12 13:01
Jhyvin
Rank: 2
等 级:新手上路
威 望:4
帖 子:81
专家分:0
注 册:2005-12-29
得分:0 

import java.util.*;
import java.awt.*;
import java.applet.*;
import java.text.*;
public class clock extends Applet implements Runnable{
Thread timer;
int lastxs, lastys, lastxm,lastym, lastxh, lastyh;
SimpleDateFormat formatter;
String lastdate;
Font clockFaceFont;
Date currentDate;
Color handColor,numberColor;
public void init(){
int x,y;
lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0;
formatter = new SimpleDateFormat (\"yyyy EEE MMM dd hh:mm:ss \");
currentDate = new Date();
lastdate = formatter.format(currentDate);
clockFaceFont = new Font(\"Serif\", Font.PLAIN, 14);
handColor = Color.blue;
numberColor = Color.darkGray;
try {
setBackground(new Color(Integer.parseInt(getParameter(\"bgcolor\"),16)));
}
catch (Exception E) {
}
try {
handColor = new Color(Integer.parseInt(getParameter(\"fgcolor1\"),16));
}
catch (Exception E) { }
try {
numberColor = new Color(Integer.parseInt(getParameter(\"fgcolor2\"),16));
}
catch (Exception E) { }
resize(200,200); // 设置时钟窗口大小
}
public void plotpoints(int x0, int y0, int x, int y, Graphics g){
g.drawLine(x0+x,y0+y,x0+x,y0+y);
g.drawLine(x0+y,y0+x,x0+y,y0+x);
g.drawLine(x0+y,y0-x,x0+y,y0-x);
g.drawLine(x0+x,y0-y,x0+x,y0-y);
g.drawLine(x0-x,y0-y,x0-x,y0-y);
g.drawLine(x0-y,y0-x,x0-y,y0-x);
g.drawLine(x0-y,y0+x,x0-y,y0+x);
g.drawLine(x0-x,y0+y,x0-x,y0+y);
}
public void circle(int x0, int y0, int r, Graphics g){
int x,y;
float d;
x=0;
y=r;
d=5/4-r;
plotpoints(x0,y0,x,y,g);
while (y>x) {
if (d<0) {
d=d+2*x+3;
x++;
}
else {
d=d+2*(x-y)+5;
x++;
y--;
}
plotpoints(x0,y0,x,y,g);
}
}
public void paint(Graphics g){
int xh, yh, xm, ym, xs, ys, s = 0, m = 10, h = 10, xcenter, ycenter;
String today;
currentDate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(\"s\",Locale.getDefault());
try {
s = Integer.parseInt(formatter.format(currentDate));
}
catch (NumberFormatException n) {
s = 0;
}
formatter.applyPattern(\"m\");
try {
m = Integer.parseInt(formatter.format(currentDate));
}
catch (NumberFormatException n) {
m = 10;
}
formatter.applyPattern(\"h\");
try {
h = Integer.parseInt(formatter.format(currentDate));
}
catch (NumberFormatException n) {
h = 10;
}
formatter.applyPattern(\"EEE MMM dd HH:mm:ss yyyy\");
today = formatter.format(currentDate);
xcenter=80;
ycenter=55;
xs = (int)(Math.cos(s * 3.14f/30 - 3.14f/2) * 45 + xcenter);
ys = (int)(Math.sin(s * 3.14f/30 - 3.14f/2) * 45 + ycenter);
xm = (int)(Math.cos(m * 3.14f/30 - 3.14f/2) * 40 + xcenter);
ym = (int)(Math.sin(m * 3.14f/30 - 3.14f/2) * 40 + ycenter);
xh = (int)(Math.cos((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + xcenter);
yh = (int)(Math.sin((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + ycenter);
g.setFont(clockFaceFont);
g.setColor(handColor);
circle(xcenter,ycenter,50,g);
g.setColor(numberColor);
g.drawString(\"9\",xcenter-45,ycenter+3);
g.drawString(\"3\",xcenter+40,ycenter+3);
g.drawString(\"12\",xcenter-5,ycenter-37);
g.drawString(\"6\",xcenter-3,ycenter+45);
g.setColor(getBackground());
if (xs != lastxs || ys != lastys) {
g.drawLine(xcenter, ycenter, lastxs, lastys);
g.drawString(lastdate, 5, 125);
}
if (xm != lastxm || ym != lastym) {
g.drawLine(xcenter, ycenter-1, lastxm, lastym);
g.drawLine(xcenter-1, ycenter, lastxm, lastym);
}
if (xh != lastxh || yh != lastyh) {
g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
g.drawLine(xcenter-1, ycenter, lastxh, lastyh);
}
g.setColor(numberColor);
g.drawString(\"\", 5, 125);
g.drawString(today, 5, 125);
g.drawLine(xcenter, ycenter, xs, ys);
g.setColor(handColor);
g.drawLine(xcenter, ycenter-1, xm, ym);
g.drawLine(xcenter-1, ycenter, xm, ym);
g.drawLine(xcenter, ycenter-1, xh, yh);
g.drawLine(xcenter-1, ycenter, xh, yh);
lastxs=xs; lastys=ys;
lastxm=xm; lastym=ym;
lastxh=xh; lastyh=yh;
lastdate = today;
currentDate=null;
}
public void start(){
timer = new Thread(this);
timer.start();
}
public void stop(){
timer = null;
}
public void run(){
Thread me = Thread.currentThread();
while (timer == me) {
try {
Thread.currentThread().sleep(1000);
}
catch (InterruptedException e) {
}
repaint();
}
}
public void update(Graphics g){
paint(g);
}
}

一万年太久,只争朝夕! 从此不再乱翻书!!!
2006-01-12 13:04
wlf3861069
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-10-31
得分:0 
编译通过,运行时提示:
Exception in thread "main" java.lang.Noswhmethoderror:main
不好意思啊!我超级菜鸟,不懂啊!麻烦各位了!
2006-01-12 13:15
Jhyvin
Rank: 2
等 级:新手上路
威 望:4
帖 子:81
专家分:0
注 册:2005-12-29
得分:0 
以下是引用wlf3861069在2006-1-12 13:15:00的发言:
编译通过,运行时提示:
Exception in thread "main" java.lang.Noswhmethoderror:main
不好意思啊!我超级菜鸟,不懂啊!麻烦各位了!

applet就是没有main的丫


一万年太久,只争朝夕! 从此不再乱翻书!!!
2006-01-12 14:58
cll19820814
Rank: 2
等 级:新手上路
威 望:3
帖 子:328
专家分:0
注 册:2005-11-30
得分:0 

楼主稍微改改就行了


懵懵懂懂,看千遍而不会;设身处地,试一下就成功!
2006-01-12 15:16
wlf3861069
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-10-31
得分:0 
我要是会改就好了!还要麻烦其他人吗?

[此贴子已经被作者于2006-1-12 17:55:39编辑过]


2006-01-12 17:55
bagger
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:33
帖 子:891
专家分:0
注 册:2005-8-16
得分:0 
在你的CLASS文件夹中加一个HTML文件

<HTML>
<HEAD>
</HEAD>
<BODY BGCOLOR="000000">
<CENTER>
<APPLET
code = "Clock.class"
width = "500"
height = "300"
>
</APPLET>
</CENTER>
</BODY>
</HTML>

运行这个HTML文件就能看到结果了

【三元毕业设计论文】
三元论文真的只有三元钱
客服QQ:742670649
http://shop35094218./
2006-01-13 08:38



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-42552-1-1.html




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

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