java如何在窗口中显示日历
package显示日历;importjavax.swing.JOptionPane;publicclass显示日历{publicstaticvoidmain(String...
package 显示日历;
import javax.swing.JOptionPane;
public class 显示日历 {
public static void main(String[] args) {
String yString = JOptionPane.showInputDialog(null,"Enter full year (e.g.:2001)","日历",JOptionPane.QUESTION_MESSAGE);
int year = Integer.parseInt(yString);
String mString = JOptionPane.showInputDialog(null,"Enter month in number between 1 and 12","日历",JOptionPane.QUESTION_MESSAGE);
int month = Integer.parseInt(mString);
printMonth(year,month);
}
public static void printMonth(int year, int month) {
printMonthTitle(year,month);
printMonthBody(year,month);
}
public static void printMonthTitle(int year, int month){
String a =" "+ getMonthName(month)+" " + year
+"\n----------------------------"
+"\nSun Mon Tue Wed Thu Fri Sat ";
}
public static String getMonthName(int month){
String monthName = " ";
switch(month){
case 1: monthName = "January"; break;
case 2: monthName = "February"; break;
case 3: monthName = "March"; break;
case 4: monthName = "April"; break;
case 5: monthName = "May"; break;
case 6: monthName = "June"; break;
case 7: monthName = "July"; break;
case 8: monthName = "August"; break;
case 9: monthName = "September"; break;
case 10: monthName = "October"; break;
case 11: monthName = "November"; break;
case 12: monthName = "December";
}
return monthName;
}
public static void printMonthBody(int year, int month) {
int startDay = getStartDay(year, month);
int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);
int i=0;
String b;
for(i=0;i<startDay;i++)
b = " ";
for(i=1;i<=numberOfDaysInMonth;i++){
System.out.printf("%4d",i);
if((i+startDay)%7==0)
System.out.println();
}
System.out.println();
}
public static int getStartDay(int year, int month) {
final int START_DAY_FOR_JAN_1_1800 = 3;
int totalNumberOfDays = getTotalNumberOfDays(year, month);
return (totalNumberOfDays+START_DAY_FOR_JAN_1_1800)%7;
}
public static int getTotalNumberOfDays(int year, int month){
int total = 0;
for(int i=1800;i<year;i++)
if(isLeapYear(i))
total=total+366;
else
total=total+365;
for(int i = 1; i < month; i++)
total = total +getNumberOfDaysInMonth(year,i);
return total;
}
public static int getNumberOfDaysInMonth(int year, int month){
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
return 31;
if(month==4||month==6||month==9||month==11)
return 30;
if(month==2)
return isLeapYear(year)?29:28;
return 0;
}
public static boolean isLeapYear(int year){
return year%400==0||(year%4==0&&year%100!=0);
}
}
日历部分怎么用GUI现实阿阿阿 求助!!!! 展开
import javax.swing.JOptionPane;
public class 显示日历 {
public static void main(String[] args) {
String yString = JOptionPane.showInputDialog(null,"Enter full year (e.g.:2001)","日历",JOptionPane.QUESTION_MESSAGE);
int year = Integer.parseInt(yString);
String mString = JOptionPane.showInputDialog(null,"Enter month in number between 1 and 12","日历",JOptionPane.QUESTION_MESSAGE);
int month = Integer.parseInt(mString);
printMonth(year,month);
}
public static void printMonth(int year, int month) {
printMonthTitle(year,month);
printMonthBody(year,month);
}
public static void printMonthTitle(int year, int month){
String a =" "+ getMonthName(month)+" " + year
+"\n----------------------------"
+"\nSun Mon Tue Wed Thu Fri Sat ";
}
public static String getMonthName(int month){
String monthName = " ";
switch(month){
case 1: monthName = "January"; break;
case 2: monthName = "February"; break;
case 3: monthName = "March"; break;
case 4: monthName = "April"; break;
case 5: monthName = "May"; break;
case 6: monthName = "June"; break;
case 7: monthName = "July"; break;
case 8: monthName = "August"; break;
case 9: monthName = "September"; break;
case 10: monthName = "October"; break;
case 11: monthName = "November"; break;
case 12: monthName = "December";
}
return monthName;
}
public static void printMonthBody(int year, int month) {
int startDay = getStartDay(year, month);
int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);
int i=0;
String b;
for(i=0;i<startDay;i++)
b = " ";
for(i=1;i<=numberOfDaysInMonth;i++){
System.out.printf("%4d",i);
if((i+startDay)%7==0)
System.out.println();
}
System.out.println();
}
public static int getStartDay(int year, int month) {
final int START_DAY_FOR_JAN_1_1800 = 3;
int totalNumberOfDays = getTotalNumberOfDays(year, month);
return (totalNumberOfDays+START_DAY_FOR_JAN_1_1800)%7;
}
public static int getTotalNumberOfDays(int year, int month){
int total = 0;
for(int i=1800;i<year;i++)
if(isLeapYear(i))
total=total+366;
else
total=total+365;
for(int i = 1; i < month; i++)
total = total +getNumberOfDaysInMonth(year,i);
return total;
}
public static int getNumberOfDaysInMonth(int year, int month){
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
return 31;
if(month==4||month==6||month==9||month==11)
return 30;
if(month==2)
return isLeapYear(year)?29:28;
return 0;
}
public static boolean isLeapYear(int year){
return year%400==0||(year%4==0&&year%100!=0);
}
}
日历部分怎么用GUI现实阿阿阿 求助!!!! 展开
3个回答
展开全部
MainFrame.java是显示日历程序,Clock.java是日历计算程序(可以不要)。
编译后运行MainFrame这个类即可。
swing窗口显示万年历,jdk1.4以上环境编译运行。
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 package org.test;import java.awt.BorderLayout;import java.awt.Color;import java.awt.GridLayout;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sql.Date;import java.util.Calendar;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;public class MainFrame extends JFrame { private static final long serialVersionUID = -306484324260972141L; JPanel panel = new JPanel(new BorderLayout()); JPanel panel1 = new JPanel(); JPanel panel2 = new JPanel(new GridLayout(7, 7)); JPanel panel3 = new JPanel(); JLabel[] label = new JLabel[49]; JLabel y_label = new JLabel("年份"); JLabel m_label = new JLabel("月份"); JComboBox com1 = new JComboBox(); JComboBox com2 = new JComboBox(); int re_year, re_month, x_size, y_size; String year_num; Calendar now = Calendar.getInstance(); // 实例化Calendar MainFrame() { super("万年历"); setSize(300, 350); x_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth()); y_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight()); setLocation((x_size - 300) / 2, (y_size - 350) / 2); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel1.add(y_label); panel1.add(com1); panel1.add(m_label); panel1.add(com2); for (int i = 0; i < 49; i++) { label[i] = new JLabel("", JLabel.CENTER);// 将显示的字符设置为居中 panel2.add(label[i]); } panel3.add(new Clock(this)); panel.add(panel1, BorderLayout.NORTH); panel.add(panel2, BorderLayout.CENTER); panel.add(panel3, BorderLayout.SOUTH); panel.setBackground(Color.white); panel1.setBackground(Color.white); panel2.setBackground(Color.white); panel3.setBackground(Color.white); Init(); com1.addActionListener(new ClockAction()); com2.addActionListener(new ClockAction()); setContentPane(panel); setVisible(true); setResizable(false); } class ClockAction implements ActionListener { public void actionPerformed(ActionEvent arg0) { int c_year, c_month, c_week; c_year = Integer.parseInt(com1.getSelectedItem().toString()); // 得到当前所选年份 c_month = Integer.parseInt(com2.getSelectedItem().toString()) - 1; // 得到当前月份,并减1,计算机中的月为0-11 c_week = use(c_year, c_month); // 调用函数use,得到星期几 Resetday(c_week, c_year, c_month); // 调用函数Resetday } } public void Init() { int year, month_num, first_day_num; String log[] = { "日", "一", "二", "三", "四", "五", "六" }; for (int i = 0; i < 7; i++) { label[i].setText(log[i]); } for (int i = 0; i < 49; i = i + 7) { label[i].setForeground(Color.red); // 将星期日的日期设置为红色 } for (int i = 6; i < 49; i = i + 7) { label[i].setForeground(Color.green);// 将星期六的日期设置为绿色 } for (int i = 1; i < 10000; i++) { com1.addItem("" + i); } for (int i = 1; i < 13; i++) { com2.addItem("" + i); } month_num = (int) (now.get(Calendar.MONTH)); // 得到当前时间的月份 year = (int) (now.get(Calendar.YEAR)); // 得到当前时间的年份 com1.setSelectedIndex(year - 1); // 设置下拉列表显示为当前年 com2.setSelectedIndex(month_num); // 设置下拉列表显示为当前月 first_day_num = use(year, month_num); Resetday(first_day_num, year, month_num); } public int use(int reyear, int remonth) { int week_num; now.set(reyear, remonth, 1); // 设置时间为所要查询的年月的第一天 week_num = (int) (now.get(Calendar.DAY_OF_WEEK));// 得到第一天的星期 return week_num; } @SuppressWarnings("deprecation") public void Resetday(int week_log, int year_log, int month_log) { int month_day_score; // 存储月份的天数 int count; month_day_score = 0; count = 1; Date date = new Date(year_log, month_log + 1, 1); // now Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.MONTH, -1); // 前个月 month_day_score = cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 最后一天 for (int i = 7; i < 49; i++) { // 初始化标签 label[i].setText(""); } week_log = week_log + 6; // 将星期数加6,使显示正确 month_day_score = month_day_score + week_log; for (int i = week_log; i < month_day_score; i++, count++) { label[i].setText(count + ""); } } public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); new MainFrame(); }}
12345678910111213141516171819202122232425262728293031323334353637383940414243 package org.test;import java.awt.Color;import java.util.Calendar;import java.awt.Canvas;import java.awt.Font;import java.awt.Graphics;import java.text.SimpleDateFormat;public class Clock extends Canvas implements Runnable{ private static final long serialVersionUID = 3660124045489727166L; MainFrame mf; Thread t; String time; public Clock(MainFrame mf){ this.mf=mf; setSize(280,40); setBackground(Color.white); t=new Thread(this);//实例化线程 t.start(); //调用线程 } public void run(){ while(true){ try{ Thread.sleep(1000);//休眠1秒钟 }catch(InterruptedException e){ System.out.println("异常"); } this.repaint(100); } } public void paint(Graphics g){ Font f=new Font("宋体",Font.BOLD,16); SimpleDateFormat SDF=new SimpleDateFormat("yyyy'年'MM'月'dd'日'HH:mm:ss");//格式化时间显示类型 Calendar now=Calendar.getInstance(); time=SDF.format(now.getTime()); //得到当前日期和时间 g.setFont(f); g.setColor(Color.orange); g.drawString(time,45,25); }}
2015-04-29
展开全部
Date date = new Date();//你具体要赋值的Date值
JComboBox comboBox = new JComboBox();//你自己的下拉列表
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int maxWeek =cal.get(Calendar.WEEK_OF_YEAR);
for(int i=1;i<=maxWeek;i++){
comboBox.addItem(i);
}
JComboBox comboBox = new JComboBox();//你自己的下拉列表
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int maxWeek =cal.get(Calendar.WEEK_OF_YEAR);
for(int i=1;i<=maxWeek;i++){
comboBox.addItem(i);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你使用JTABEL添加进去不久好了嘛
更多追问追答
追问
加不进去阿
追答
这个jtable肯定是可以加的,不过要设置一下行和列等信息,我也好久没搞这个swing了,多查查相关资料肯定可以的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询