标签:
import java.awt.*; import java.util.Calendar; import java.util.GregorianCalendar; import javax.swing.*; import javax.swing.border.LineBorder; public class Test_17_17 extends JFrame{ private CanlendarPanel calDer = new CanlendarPanel(); private JPanel JPholder = new JPanel(); private JLabel jl[] = { new JLabel("Sunday"),new JLabel("Monday"),new JLabel("Tuesday"),new JLabel("Wednesday"),new JLabel("Wednesday"),new JLabel("Thursday"), new JLabel("Friday"),new JLabel("Saturday") }; public Test_17_17(){ for(int i = 0; i < 7 ; i++) jl[i].setBorder(new LineBorder(Color.black,1)); JPholder.setLayout(new GridLayout(1,7)); for(int i = 0; i < 7 ; i++) JPholder.add(jl[i]); add(new JPanel().add(new Label(calDer.Year+"/"+calDer.Month)),BorderLayout.NORTH); add(JPholder,BorderLayout.CENTER); add(calDer,BorderLayout.SOUTH); } public static void main(String[] args) { // TODO Auto-generated method stubs Test_17_17 frame = new Test_17_17(); frame.setTitle("Test_17_17"); frame.setSize(400,200); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } class CanlendarPanel extends JPanel{ private Calendar G = new GregorianCalendar(); private int Year = G.get(Calendar.YEAR); private int Month = G.get(Calendar.MONTH); private int Days_of_Month = G.getActualMaximum(Calendar.DATE); private int Day_of_Week = G.get(Calendar.DAY_OF_WEEK); public CanlendarPanel(){ //draw the month calendar setLayout(new GridLayout(G.getActualMaximum(Calendar.WEEK_OF_MONTH),7)); //把G当前的天设置为1号 G.set(Calendar.DATE,1); System.out.println("第一天是星期几"+ G.get(Calendar.DAY_OF_WEEK)); for(int i = 1 , j = 1; i <= G.getActualMaximum(Calendar.WEEK_OF_MONTH) * 7; i++) { if(i < G.get(Calendar.DAY_OF_WEEK)) add(new JLabel()); else if(i <= Days_of_Month) { JLabel jlTemp = new JLabel(j +""); jlTemp.setBorder(new LineBorder(Color.BLACK,1)); add(jlTemp); j++; } else add(new JLabel()); } } } }
标签:
原文地址:http://www.cnblogs.com/wanjiang/p/5774345.html