注意代码来自阿发Swing高级0305篇,个人仅仅做了极少部分修改。转载请注明出处!!!
首先建立三个class结构名称如下
其中SwingDemo代码如下:
import javax.swing.JFrame;
public class SwingDemo {
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run()
{
createGUI();
}
});
}
private static void createGUI()
{
myFrame frame = new myFrame("日历");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,600);
frame.setVisible(true);
}
}
myFrame代码如下:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
public class myFrame extends JFrame{
myPanel p = new myPanel();
public myFrame(String title)
{
super(title);
Container root = this.getContentPane();
root.setLayout(new BorderLayout());
root.add(p,BorderLayout.CENTER);
// //设置边框
// Border border = BorderFactory.createLineBorder(Color.BLUE,1);
// p.setBorder(border);
// JButton button1 = new JButton();
// button1.setText("hello");
// root.add(button1,BorderLayout.PAGE_START);
}
}
myPanel代码如下:
package Swing_0305;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.util.Calendar;
import javax.swing.JPanel;
public class myPanel extends JPanel{
public myPanel()
{
}
@Override
protected void paintComponent(Graphics g)
{
int width = this.getWidth();
int height = this.getHeight();
Graphics2D g2d = (Graphics2D) g;
//平滑绘制反锯齿
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(Color.white);
g2d.fillRect(0, 0, width, height);
int x = 0;
int y = 0;
int x_size = 60;//单元格宽度
int y_size = 60;//单元格高度
//第一行,显示一二三四五六日
g2d.setPaint(Color.GRAY);
g2d.drawLine(x, y,x + x_size * 7, y);
g2d.drawLine(x, y+y_size, x_size*7, y+y_size);
String [] cc = {"一","二","三","四","五","六","日"};
g2d.setPaint(Color.GRAY);
g2d.setFont(new Font("宋体",0,20));
for(int i = 0; i < 7;i++)
{
Rectangle rect =new Rectangle(x+i*x_size, y, x_size, y_size);
drawText(g2d,cc[i],rect);
}
//计算本月第一天是星期几
Calendar cal = Calendar.getInstance();
int theYear = cal.get(Calendar.YEAR);
int theMonth = cal.get(Calendar.MONTH);//当前月份
int theDay = cal.get(Calendar.DAY_OF_MONTH);//当前几号
cal.set(Calendar.DAY_OF_MONTH, 1);//向前推到本月第一号
int weekday = cal.get(Calendar.DAY_OF_WEEK);//计算本月第一天是星期几
//System.out.println("week:"+weekday);
// //第几周转换为英文
// String weekDay = null ;
// switch(weekday)
// {
// case 1: weekDay = "monday" ; break;
// case 2: weekDay = "tuesday" ; break;
// case 3: weekDay = "wednesday" ; break;
// case 4: weekDay = "thursday" ; break;
// case 5: weekDay = "friday" ; break;
// case 6: weekDay = "saturdy" ; break;
// case 7: weekDay = "sunday" ; break;
// default: break;
// }
// System.out.println("today is " + theYear +"/" + theMonth + "/" + theDay );
// System.out.println("and today is " + weekDay);
//往前推N天
//Calendar里规定Sunday是第一天,
//他是周一,前推0天,周二推一天,周五推4天,周日推6天
int start = weekday - Calendar.MONDAY;
if(start < 0) start = 6;
cal.add(Calendar.DAY_OF_MONTH, 0 - start);
//绘制五行
//每月最多31天,分五行显示
x = 0;
y += y_size;
for(int i = 0;i < 6 ;i++)//6行
{
for(int j = 0;j < 7;j++)//7列
{
//判断月份是否相同
if(cal.get(Calendar.MONTH) == theMonth)
{
g2d.setPaint(Color.BLACK);
}
else
{
g2d.setPaint(Color.GRAY);
}
int day = cal.get(Calendar.DAY_OF_MONTH);
Rectangle rect = new Rectangle(x+x_size*j,y+y_size*i,x_size,y_size);
drawText(g2d,String.valueOf(day),rect);
//日期加一
cal.add(Calendar.DAY_OF_MONTH, 1);
}
}
}
//将str绘制在矩形rent中,居中显示
private void drawText(Graphics2D g2d, String str, Rectangle rect)
{
FontMetrics fm = g2d.getFontMetrics();
int fontSize = fm.getHeight();//字高
int textWidth = fm.stringWidth(str);//获取字宽
int leading = fm.getLeading();
int ascent = fm.getAscent();
int descent = fm.getDescent();
//System.out.println(fontSize + "," + leading + "," + ascent + "," + descent );
int x = 0;
int y = 0;
x = rect.x + (rect.width - textWidth)/2;//水平居中
y = rect.y + rect.height / 2 + (fontSize -leading)/2 -descent;
g2d.drawString(str, x, y);
}
}
实现的效果如下:
改进建议:(我尽力更新)
1.当前日期高亮,
2.点击其他日期高亮边框显示。
3.可选择年份,月份。(顶部)
4.添加边框。
5.居中显示。
后期可添加农历以及其他信息等
按月,按周,按年显示。
甚至可以添加提醒事件。