星期几=日历.get(Calendar.DAY_OF_WEEK)-1; if(month==1||month==2||month==3||month==5||month==7 ||month==8||month==10||month==12) { 排列号码(星期几,31); } else if(month==4||month==6||month==9||month==11) { 排列号码(星期几,30); } else if(month==2) { if((year%4==0&&year%100!=0)||(year%400==0)) { 排列号码(星期几,29); } else { 排列号码(星期几,28); } } } public void 排列号码(int 星期几,int 月天数) { for(int i=星期几,n=1;i<星期几+月天数;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<星期几;i++) { showDay[i].setText(""); } for(int i=星期几+月天数;i<42;i++) { showDay[i].setText(""); } } public int getYear() { return year; } public void setYear(int y) { year=y; notepad.setYear(year); } public int getMonth() { return month; } public void setMonth(int m) { month=m; notepad.setMonth(month); } public int getDay() { return day; } public void setDay(int d) { day=d; notepad.setDay(day); } public Hashtable getHashtable() { return hashtable; } public File getFile() { return file; } public void mousePressed(MouseEvent e) { JTextField source=(JTextField)e.getSource(); try{ day=Integer.parseInt(source.getText()); notepad.setDay(day); notepad.设置信息条(year,month,day); notepad.设置文本区(null); notepad.获取日志内容(year,month,day); } 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 CalendarPad(y,m,d); } }
这是时钟的 import java.awt.*; import java.awt.geom.Ellipse2D; import java.awt.geom.GeneralPath; import java.awt.geom.Line2D; import java.awt.geom.Rectangle2D; import java.util.Calendar; import java.util.Date; import javax.swing.BorderFactory; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.UIManager; public class Clock extends JComponent { private static final Color INTEGRAL_COLOR = new Color(0, 128, 128); private int radius; private Calendar currentTime = Calendar.getInstance(); private double s = 0.03; public Clock(int radius) { this.radius = radius; } public void setCurrentTime(Date time) //设置当前时间 { this.currentTime.setTime(time); } public void setCurrentTime(long millis) { this.currentTime.setTimeInMillis(millis); } public Dimension getPreferredSize() { Insets insets = getInsets(); int r = (int) (radius == -1 ? 0 : radius*(1+s))+1; return new Dimension(r * 2 + insets.left + insets.right,r * 2 + insets.top + insets.bottom); //返回一个指定宽、高的Dimension } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Insets insets = getInsets(); int wid = getWidth() - insets.left - insets.right; int hei = getHeight() - insets.top - insets.bottom; int r = (int) ((Math.min(wid, hei)) / 2 / (1+s)); g2d.translate(insets.left + r * (1+s), insets.top + r * (1+s)); g2d.scale(1, -1); for (int i = 0; i < 60; i++) { int angle = 90 - i * 6; double pos[] = calcPos(r, angle); paintMinuteDot(r, g2d, pos[0], pos[1], i % 5 == 0); } paintHourPointer(r, g2d); paintMinutePointer(r, g2d); paintSecondPointer(r, g2d); paintCenterPoint(g2d); g2d.scale(1, -1); g2d.translate(-insets.left - r * (1+s), -insets.top - r * (1+s)); } private void paintCenterPoint(Graphics2D g2d) { g2d.setColor(Color.BLUE); Rectangle2D rect = new Rectangle2D.Double(-2, -2, 4, 4); g2d.fill(rect); } private void paintMinutePointer(int r, Graphics2D g2d) { int minute = currentTime.get(Calendar.MINUTE); int second = currentTime.get(Calendar.SECOND); double angle = 90 - (minute + second / 60.0) * 6; Shape pointerShape = createPointerShape(r * 0.8, r * 0.04, r * 0.08, angle); g2d.setColor(Color.LIGHT_GRAY); g2d.fill(pointerShape); g2d.setColor(Color.DARK_GRAY); g2d.draw(pointerShape); } private void paintHourPointer(int r, Graphics2D g2d) { int hour = currentTime.get(Calendar.HOUR); int minute = currentTime.get(Calendar.MINUTE); int second = currentTime.get(Calendar.SECOND); double angle = 90 - (hour + minute / 60.0 + second / 3600.0) * 30; Shape pointerShape = createPointerShape(r * 0.6, r * 0.06, r * 0.1, angle); g2d.setColor(Color.LIGHT_GRAY); g2d.fill(pointerShape); g2d.setColor(Color.DARK_GRAY); g2d.draw(pointerShape); } private Shape createPointerShape(double r1, double r2, double r3, double angle) { GeneralPath gp = new GeneralPath(); double[] pos = calcPos(r1, angle); double[] pos1 = calcPos(r2, angle + 90); gp.append(new Line2D.Double(pos[0], pos[1], pos1[0], pos1[1]), true); double[] pos2 = calcPos(r3, angle + 180); gp.lineTo((float)pos2[0], (float)pos2[1]); double[] pos3 = calcPos(r2, angle + 270); gp.lineTo((float)pos3[0], (float)pos3[1]); gp.closePath(); return gp; } private void paintSecondPointer(int r, Graphics2D g2d) { g2d.setColor(Color.BLACK); int second = currentTime.get(Calendar.SECOND); int angle = 90 - second * 6; double pos[] = calcPos(r * 0.9, angle); double pos1[] = calcPos(r * 0.2, angle + 180); Line2D line = new Line2D.Double(pos1[0], pos1[1], pos[0], pos[1]); g2d.draw(line); } private void paintMinuteDot(int r, Graphics2D g2d, double x, double y, boolean flag) { g2d.setColor(flag ? Color.RED : Color.BLACK); if (flag) { //Rectangle2D rect = new Rectangle2D.Double( Ellipse2D rect = new Ellipse2D.Double( x - r * s, y - r * s, r * s * 2, r * s * 2); g2d.fill(rect); } else { //Rectangle2D rect = new Rectangle2D.Double( Ellipse2D rect = new Ellipse2D.Double( x - r * 0.02, y - r * 0.02, r * 0.04, r * 0.04); g2d.fill(rect); } } private double[] calcPos(double r, double angle) { double radian = Math.toRadians(angle); double x = r * Math.cos(radian); double y = r * Math.sin(radian); return new double[] {x, y}; } public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } final Clock clock = new Clock(50); clock.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); JFrame f = new JFrame( "软件4班 3104007114 彭洪钰 "); //f.setBounds(380,200,500,600); f.getContentPane().add(clock, BorderLayout.CENTER); f.pack(); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); new Thread() { public void run() { while (true) { try { Thread.sleep(1000); } catch (InterruptedException ex) { ex.printStackTrace(); } clock.setCurrentTime(System.currentTimeMillis()); clock.repaint(); } } }.start(); } }
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Hashtable;
public class CalendarPad extends JFrame implements MouseListener
{
int year,month,day;
Hashtable hashtable;
File file;
JTextField showDay[];
JLabel title[];
Calendar 日历;
int 星期几;
NotePad notepad=null;
Month 负责改变月;
Year 负责改变年;
Clock 负责改变时间;
String 星期[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
JPanel leftPanel,rightPanel;
public CalendarPad(int year,int month,int day)
{
leftPanel=new JPanel();
JPanel leftCenter=new JPanel();
JPanel leftNorth=new JPanel();
leftCenter.setLayout(new GridLayout(7,7));
rightPanel=new JPanel();
this.year=year;
this.month=month;
this.day=day;
负责改变年=new Year(this);
负责改变年.setYear(year);
负责改变月=new Month(this);
负责改变月.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]);
title[j].setBorder(BorderFactory.createRaisedBevelBorder());
leftCenter.add(title[j]);
}
title[0].setForeground(Color.red);
title[6].setForeground(Color.blue); for(int i=0;i<42;i++)
{
showDay[i]=new JTextField();
showDay[i].addMouseListener(this);
showDay[i].setEditable(false);
leftCenter.add(showDay[i]);
}
日历=Calendar.getInstance();
Box box=Box.createHorizontalBox();
box.add(负责改变年);
box.add(负责改变月);
leftNorth.add(box);
leftPanel.setLayout(new BorderLayout());
leftPanel.add(leftNorth,BorderLayout.NORTH);
leftPanel.add(leftCenter,BorderLayout.CENTER);
leftPanel.add(new Label("请在年份输入框输入所查年份(负数表示公元前),并回车确定"),
BorderLayout.SOUTH) ;
leftPanel.validate();
Container con=getContentPane();
JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
leftPanel,rightPanel);
con.add(split,BorderLayout.CENTER);
con.validate();
hashtable=new Hashtable();
file=new File("日历记事本.txt");
if(!file.exists())
{
try{
FileOutputStream out=new FileOutputStream(file);
ObjectOutputStream objectOut=new ObjectOutputStream(out);
objectOut.writeObject(hashtable);
objectOut.close();
out.close();
}
catch(IOException e)
{
}
}
notepad=new NotePad(this);
rightPanel.add(notepad);
设置日历牌(year,month);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
setBounds(100,50,524,285);
validate();
}
public void 设置日历牌(int year,int month)
{
日历.set(year,month-1,1);
星期几=日历.get(Calendar.DAY_OF_WEEK)-1;
if(month==1||month==2||month==3||month==5||month==7
||month==8||month==10||month==12)
{
排列号码(星期几,31);
}
else if(month==4||month==6||month==9||month==11)
{
排列号码(星期几,30);
}
else if(month==2)
{
if((year%4==0&&year%100!=0)||(year%400==0))
{
排列号码(星期几,29);
}
else
{
排列号码(星期几,28);
}
}
}
public void 排列号码(int 星期几,int 月天数)
{
for(int i=星期几,n=1;i<星期几+月天数;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<星期几;i++)
{
showDay[i].setText("");
}
for(int i=星期几+月天数;i<42;i++)
{
showDay[i].setText("");
}
}
public int getYear()
{
return year;
}
public void setYear(int y)
{
year=y;
notepad.setYear(year);
}
public int getMonth()
{
return month;
}
public void setMonth(int m)
{
month=m;
notepad.setMonth(month);
}
public int getDay()
{
return day;
}
public void setDay(int d)
{
day=d;
notepad.setDay(day);
}
public Hashtable getHashtable()
{
return hashtable;
}
public File getFile()
{
return file;
}
public void mousePressed(MouseEvent e)
{
JTextField source=(JTextField)e.getSource();
try{
day=Integer.parseInt(source.getText());
notepad.setDay(day);
notepad.设置信息条(year,month,day);
notepad.设置文本区(null);
notepad.获取日志内容(year,month,day);
}
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 CalendarPad(y,m,d);
}
}
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.Calendar;
import java.util.Date; import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.UIManager; public class Clock extends JComponent
{
private static final Color INTEGRAL_COLOR = new Color(0, 128, 128); private int radius; private Calendar currentTime = Calendar.getInstance(); private double s = 0.03; public Clock(int radius)
{
this.radius = radius;
} public void setCurrentTime(Date time) //设置当前时间
{
this.currentTime.setTime(time);
} public void setCurrentTime(long millis)
{
this.currentTime.setTimeInMillis(millis);
} public Dimension getPreferredSize()
{
Insets insets = getInsets();
int r = (int) (radius == -1 ? 0 : radius*(1+s))+1;
return new Dimension(r * 2 + insets.left + insets.right,r * 2 + insets.top + insets.bottom); //返回一个指定宽、高的Dimension
} protected void paintComponent(Graphics g)
{
super.paintComponent(g); Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Insets insets = getInsets();
int wid = getWidth() - insets.left - insets.right;
int hei = getHeight() - insets.top - insets.bottom;
int r = (int) ((Math.min(wid, hei)) / 2 / (1+s)); g2d.translate(insets.left + r * (1+s), insets.top + r * (1+s));
g2d.scale(1, -1); for (int i = 0; i < 60; i++) {
int angle = 90 - i * 6;
double pos[] = calcPos(r, angle);
paintMinuteDot(r, g2d, pos[0], pos[1], i % 5 == 0);
} paintHourPointer(r, g2d);
paintMinutePointer(r, g2d);
paintSecondPointer(r, g2d);
paintCenterPoint(g2d); g2d.scale(1, -1);
g2d.translate(-insets.left - r * (1+s), -insets.top - r * (1+s));
} private void paintCenterPoint(Graphics2D g2d)
{
g2d.setColor(Color.BLUE);
Rectangle2D rect = new Rectangle2D.Double(-2, -2, 4, 4);
g2d.fill(rect);
} private void paintMinutePointer(int r, Graphics2D g2d)
{
int minute = currentTime.get(Calendar.MINUTE);
int second = currentTime.get(Calendar.SECOND);
double angle = 90 - (minute + second / 60.0) * 6; Shape pointerShape = createPointerShape(r * 0.8, r * 0.04, r * 0.08, angle); g2d.setColor(Color.LIGHT_GRAY);
g2d.fill(pointerShape); g2d.setColor(Color.DARK_GRAY);
g2d.draw(pointerShape);
} private void paintHourPointer(int r, Graphics2D g2d)
{
int hour = currentTime.get(Calendar.HOUR);
int minute = currentTime.get(Calendar.MINUTE);
int second = currentTime.get(Calendar.SECOND);
double angle = 90 - (hour + minute / 60.0 + second / 3600.0) * 30; Shape pointerShape = createPointerShape(r * 0.6, r * 0.06, r * 0.1, angle); g2d.setColor(Color.LIGHT_GRAY);
g2d.fill(pointerShape); g2d.setColor(Color.DARK_GRAY);
g2d.draw(pointerShape);
} private Shape createPointerShape(double r1, double r2, double r3, double angle)
{
GeneralPath gp = new GeneralPath();
double[] pos = calcPos(r1, angle);
double[] pos1 = calcPos(r2, angle + 90);
gp.append(new Line2D.Double(pos[0], pos[1], pos1[0], pos1[1]), true); double[] pos2 = calcPos(r3, angle + 180);
gp.lineTo((float)pos2[0], (float)pos2[1]); double[] pos3 = calcPos(r2, angle + 270);
gp.lineTo((float)pos3[0], (float)pos3[1]); gp.closePath(); return gp;
} private void paintSecondPointer(int r, Graphics2D g2d)
{
g2d.setColor(Color.BLACK);
int second = currentTime.get(Calendar.SECOND);
int angle = 90 - second * 6;
double pos[] = calcPos(r * 0.9, angle);
double pos1[] = calcPos(r * 0.2, angle + 180); Line2D line = new Line2D.Double(pos1[0], pos1[1], pos[0], pos[1]);
g2d.draw(line);
} private void paintMinuteDot(int r, Graphics2D g2d, double x, double y, boolean flag)
{
g2d.setColor(flag ? Color.RED : Color.BLACK);
if (flag) {
//Rectangle2D rect = new Rectangle2D.Double(
Ellipse2D rect = new Ellipse2D.Double(
x - r * s, y - r * s, r * s * 2, r * s * 2);
g2d.fill(rect);
}
else {
//Rectangle2D rect = new Rectangle2D.Double(
Ellipse2D rect = new Ellipse2D.Double(
x - r * 0.02, y - r * 0.02, r * 0.04, r * 0.04);
g2d.fill(rect);
}
} private double[] calcPos(double r, double angle)
{
double radian = Math.toRadians(angle);
double x = r * Math.cos(radian);
double y = r * Math.sin(radian); return new double[] {x, y};
} public static void main(String[] args)
{
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
} final Clock clock = new Clock(50);
clock.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); JFrame f = new JFrame( "软件4班 3104007114 彭洪钰 ");
//f.setBounds(380,200,500,600);
f.getContentPane().add(clock, BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
new Thread() {
public void run()
{
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
clock.setCurrentTime(System.currentTimeMillis());
clock.repaint();
}
}
}.start();
}
}