Topic: 求助:我做的日历运行时有重影,请高手帮我解决一下

  Print this page

1.求助:我做的日历运行时有重影,请高手帮我解决一下 Copy to clipboard
Posted by: jidilanbing
Posted on: 2006-07-03 13:17

import java.util.Calendar;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.GregorianCalendar;
import javax.swing.JTable;

public class DateTime extends JFrame implements MouseListener
{
int year,month,day;
JTextField showDay[];
JLabel title[];

Calendar calendar1;
Clock clock=null;
int week;
Month monthChange;
Year yearChange;
String 星期[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
JPanel leftPanel,rightPanel;

public DateTime(int year,int month,int day)
{
leftPanel=new JPanel();
JPanel leftCenter=new JPanel();
JPanel leftNorth=new JPanel();
JPanel leftSouth=new JPanel();
leftCenter.setLayout(new GridLayout(7,7));

rightPanel=new JPanel();

this.year=year;
this.month=month;
this.day=day;
yearChange=new Year(this);
yearChange.setYear(year);
monthChange=new Month(this);
monthChange.setMonth(month);

title=new JLabel[7];
showDay=new JTextField[42];
for(int j=0;j<7;j++)
{
title[j]=new JLabel();
title[j].setText(星期[j]);

leftCenter.add(title[j]);
}

for(int i=0;i<42;i++)
{
showDay[i]=new JTextField();
showDay[i].addMouseListener(this);
showDay[i].setEditable(false);
leftCenter.add(showDay[i]);
}
calendar1=Calendar.getInstance();
Box box=Box.createHorizontalBox();
box.add(yearChange);
box.add(monthChange);
leftNorth.add(box);
leftPanel.setLayout(new BorderLayout());
leftPanel.add(leftNorth,BorderLayout.NORTH);
leftPanel.add(leftCenter,BorderLayout.CENTER);
leftPanel.add(leftSouth,BorderLayout.SOUTH) ;
leftPanel.validate();
Container con=getContentPane();
JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
leftPanel,rightPanel);

con.add(split,BorderLayout.CENTER);
con.validate();
clock=new Clock();

rightPanel.add(clock);
setCalendarcard(year,month);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
setBounds(200,200,524,285);
validate();
}
public void setCalendarcard(int year,int month)
{
calendar1.set(year,month-1,1);

week=calendar1.get(Calendar.DAY_OF_WEEK)-1;
if(month==1||month==2||month==3||month==5||month==7
||month==8||month==10||month==12)
{
layCode(week,31);
}
else if(month==4||month==6||month==9||month==11)
{
layCode(week,30);
}
else if(month==2)
{
if((year%4==0&&year%100!=0)||(year%400==0))
{
layCode(week,29);
}
else
{
layCode(week,28);
}
}
}
public void layCode(int week,int layCode)
{
for(int i=week,n=1;i<week+layCode;i++)
{
showDay[i].setText(""+n);
if(n==day)
{
showDay[i].setForeground(Color.green);
showDay[i].setFont(new Font("TimesRoman",Font.BOLD,20));
}
else
{
showDay[i].setFont(new Font("TimesRoman",Font.BOLD,12));
showDay[i].setForeground(Color.black);
}
if(i%7==6)
{
showDay[i].setForeground(Color.blue);
}
if(i%7==0)
{
showDay[i].setForeground(Color.red);
}
n++;
}
for(int i=0;i<week;i++)
{
showDay[i].setText("");
}
for(int i=week+layCode;i<42;i++)
{
showDay[i].setText("");
}
}
public int getYear()
{
return year;
}
public void setYear(int y)
{
year=y;

}
public int getMonth()
{
return month;
}
public void setMonth(int m)
{
month=m;

}
public int getDay()
{
return day;
}
public void setDay(int d)
{
day=d;

}

public void mousePressed(MouseEvent e)
{
JTextField source=(JTextField)e.getSource();
try{
day=Integer.parseInt(source.getText());

}
catch(Exception ee)
{
}
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}

public static void main(String args[])
{
Calendar calendar=Calendar.getInstance();
int y=calendar.get(Calendar.YEAR);
int m=calendar.get(Calendar.MONTH)+1;
int d=calendar.get(Calendar.DAY_OF_MONTH);
new DateTime(y,m,d);
}
}

class Month extends Box implements ActionListener
{
int month;
JTextField showMonth=null;
JButton 下月,上月;
DateTime calendar1;
Month(DateTime calendar1)
{
super(BoxLayout.X_AXIS);
this.calendar1=calendar1;
showMonth=new JTextField(2);
month=calendar1.getMonth();
showMonth.setEditable(false);
showMonth.setForeground(Color.blue);
showMonth.setFont(new Font("TimesRomn",Font.BOLD,16));
下月=new JButton("下月");
上月=new JButton("上月");
add(上月);
add(showMonth);
add(下月);
上月.addActionListener(this);
下月.addActionListener(this);
showMonth.setText(""+month);
}
public void setMonth(int month)
{
if(month<=12&&month>=1)
{
this.month=month;
}
else
{
this.month=1;
}
showMonth.setText(""+month);
}
public int getMonth()
{
return month;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==上月)
{
if(month>=2)
{
month=month-1;
calendar1.setMonth(month);
calendar1.setCalendarcard(calendar1.getYear(),month);
}
else if(month==1)
{
month=12;
calendar1.setMonth(month);
calendar1.setCalendarcard(calendar1.getYear(),month);
}
showMonth.setText(""+month);
}
else if(e.getSource()==下月)
{
if(month<12)
{
month=month+1;
calendar1.setMonth(month);
calendar1.setCalendarcard(calendar1.getYear(),month);
}
else if(month==12)
{
month=1;
calendar1.setMonth(month);
calendar1.setCalendarcard(calendar1.getYear(),month);
}
showMonth.setText(""+month);
}
}
}

class Year extends Box implements ActionListener
{
int year;
JTextField showYear=null;
JButton 明年,去年;
DateTime calendar1;
Year(DateTime calendar1)
{
super(BoxLayout.X_AXIS);
showYear=new JTextField(4);
showYear.setForeground(Color.blue);
showYear.setFont(new Font("TimesRomn",Font.BOLD,14));
this.calendar1=calendar1;
year=calendar1.getYear();
明年=new JButton("下年");
去年=new JButton("上年");
add(去年);
add(showYear);
add(明年);
showYear.addActionListener(this);
去年.addActionListener(this);
明年.addActionListener(this);
}
public void setYear(int year)
{
this.year=year;
showYear.setText(""+year);
}
public int getYear()
{
return year;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==去年)
{
year=year-1;
showYear.setText(""+year);
calendar1.setYear(year);
calendar1.setCalendarcard(year,calendar1.getMonth());
}
else if(e.getSource()==明年)
{
year=year+1;
showYear.setText(""+year);
calendar1.setYear(year);
calendar1.setCalendarcard(year,calendar1.getMonth());
}
else if(e.getSource()==showYear)
{
try
{
year=Integer.parseInt(showYear.getText());
showYear.setText(""+year);
calendar1.setYear(year);
calendar1.setCalendarcard(year,calendar1.getMonth());
}
catch(NumberFormatException ee)
{
showYear.setText(""+year);
calendar1.setYear(year);
calendar1.setCalendarcard(year,calendar1.getMonth());
}
}
}
}
class Clock extends JPanel implements ActionListener {
int x,y,x0,y0,r,h,olds_x,olds_y,oldm_x,oldm_y,oldh_x,oldh_y,ss,mm,hh,old_m,old_h,ang;
final double RAD = Math.PI/180; //度数转换成弧度的比例

//构造函数创建了一个窗体
public Clock() {
this.setPreferredSize(new Dimension(200,200));
int delay = 1000;
//创建一个监听事件
ActionListener drawClock = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
repaint();
}
} ;
//创建一个时间计数器,每一秒触发一次
new Timer(delay,drawClock).start() ;
}

//实现ActionListener接口必须实现的方法
public void actionPerformed(ActionEvent e) { }

//绘制图形
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
Insets insets = getInsets();
int L = insets.left/2 ,T = insets.top/2 ;
h = getSize().height ;
g.setColor(Color.black);
//画圆
g2D.setStroke(new BasicStroke(4.0f));
g.drawOval(L+40,T+40,h-80,h-80);
r=h/2-40;
x0=40+r-5+L;
y0=40+r-5-T;
ang=60;
//绘制时钟上的12个字
for (int i=1; i<=12; i++) {
x=(int)((r+10)*Math.cos(RAD*ang)+x0);
y=(int)((r+10)*Math.sin(RAD*ang)+y0);
g.drawString(""+i,x,h-y);
ang-=30;
}
//获得现在的时间
Calendar now = new GregorianCalendar();

int nowh = now.get(Calendar.HOUR_OF_DAY );
int nowm = now.get(Calendar.MINUTE );
int nows = now.get(Calendar.SECOND );
String st;

if(nowh<10) st = "0"+nowh ; else st = ""+nowh;
if(nowm<10) st += ":0"+nowm; else st += ":"+nowm;
if(nows<10) st += ":0"+nows; else st += ":"+nows;




//在窗体上显示时间
g.setColor(Color.white);
g.fillRect(L,T,200,25);
g.setColor(Color.blue );
g.drawString(st,L+80,T+20);
//计算时间与度数的关系
ss = 90-nows*6;
mm = 90-nowm*6;
hh = 90-nowh*30-nowm/2;
x0 = r+40+L;
y0 = r+40+T;
g2D.setStroke(new BasicStroke(1.2f));
//擦除秒针
if(olds_x>0) {
g.setColor(getBackground());
g.drawLine(x0,y0,olds_x,h-olds_y);
}
else {
old_m = mm;
old_h = hh;
}
//绘制秒针
x = (int)(r*0.9*Math.cos(RAD*ss))+x0;
y = (int)(r*0.9*Math.sin(RAD*ss))+y0-2*T;
g.setColor(Color.yellow );
g.drawLine(x0,y0,x,h-y);
olds_x = x;
olds_y = y;
g2D.setStroke(new BasicStroke(2.2f));
//擦除分针
if(old_m!=mm) {
g.setColor(getBackground());
g.drawLine(x0,y0,oldm_x,h-oldm_y);
}
//绘制分针
x = (int)(r*0.7*Math.cos(RAD*mm))+x0;
y = (int)(r*0.7*Math.sin(RAD*mm))+y0-2*T;
g.setColor(Color.green );
g.drawLine(x0,y0,x,h-y);
oldm_x = x;
oldm_y = y;
old_m = mm;
g2D.setStroke(new BasicStroke(3.4f));
//擦除时针
if(old_h!=hh) {
g.setColor(getBackground());
g.drawLine(x0,y0,oldh_x,h-oldh_y);
}
//绘制时针
x = (int)(r*0.5*Math.cos(RAD*hh))+x0;
y = (int)(r*0.5*Math.sin(RAD*hh))+y0-2*T;
g.setColor(Color.red );
g.drawLine(x0,y0,x,h-y);
oldh_x = x;
oldh_y = y;
old_h = hh;
}

}

2.Re:求助:我做的日历运行时有重影,请高手帮我解决一下 [Re: jidilanbing] Copy to clipboard
Posted by: ljy0000
Posted on: 2006-07-03 21:44

用swing作界面时,当在JPanel上画图时,要重载paintComponent函数,并且在函数的第一句要加上基类的调用:super.paintComponent(g);

3.Re:求助:我做的日历运行时有重影,请高手帮我解决一下 [Re: jidilanbing] Copy to clipboard
Posted by: fanchen1111
Posted on: 2006-09-21 08:57

厉害啊,我得多研究研究啊


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923