使用JAVA编程万年历要求:使用图形用户界面;实现日期与星期的查询。
发布网友
发布时间:2022-05-11 12:21
我来回答
共2个回答
热心网友
时间:2023-10-12 09:38
这是我刚做出来的,时间紧迫,做得有些粗糙,但是你要的功能基本实现了!import java.awt.*;
import java.text.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
//import java.util.Timer;
public class DataSimple implements ItemListener
{
JFrame mainFrame;
JLabel yearLabel,monthLabel,showTextLabel,timeLabel;
JTextArea showArea;
JComboBox yearBox,monthBox;
JPanel panel_top,panel_down;
Container con;
//JTable myTable;
//JScrollPane JSpane;
String year[]=new String[9000];
String month[]=new String[12];
Date today;
GregorianCalendar cal;
//Timer myTimer;
//TimerTask myTask;
int totalDays,startDay,i;
String selectYear,selectMonth;
Object [][] day=new Object[4][6];
static final String head[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
SimpleDateFormat timeFormat=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
public DataSimple()
{
mainFrame=new JFrame("模拟万年日历");
con=mainFrame.getContentPane();
yearLabel=new JLabel("选择年份:");
monthLabel=new JLabel("选择月份:");
for(int i=1900;i<10000;i++)
{
try
{
//year[i-1900]=(new Integer(i)).toString();
year[i-1900]=String.valueOf(i);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(mainFrame,"出现异常");
}
}
yearBox=new JComboBox(year);
yearBox.setSelectedItem("2010");
yearBox.addItemListener(this);
for(int i=0;i<12;i++)
{
month[i]=(new Integer(i)).toString();
}
monthBox=new JComboBox(month);
monthBox.setSelectedItem("1");
monthBox.addItemListener(this);
panel_top=new JPanel();
panel_top.setLayout(new FlowLayout());
panel_top.add(yearLabel);
panel_top.add(yearBox);
panel_top.add(monthLabel);
panel_top.add(monthBox);
con.add(panel_top,BorderLayout.NORTH);
showArea=new JTextArea();
showArea.setEditable(false);
showDate(2010,1);
//myTable=new JTable(day,head);
con.add(showArea,BorderLayout.CENTER);
showTextLabel=new JLabel("现在时间是:");
today=new Date();
timeLabel=new JLabel(timeFormat.format(today));
panel_down=new JPanel();
panel_down.setLayout(new FlowLayout());
panel_down.add(showTextLabel);
panel_down.add(timeLabel);
con.add(panel_down,BorderLayout.SOUTH);
//con.add(showArea,BorderLayout.CENTER);
mainFrame.setSize(400,300);
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void showDate(int year,int month)
{
showArea.setText("");
cal=new GregorianCalendar(year,month-1,1);
totalDays=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
startDay=cal.get(Calendar.DAY_OF_WEEK)-1;
for(i=0;i<head.length;i++)
showArea.append(head[i]+" ");
showArea.append("\n");
//输出第一天之前的空格,每个输出项8个空格
for(i=0;i<startDay;i++)
{
//day[0][i]=new Integer(0);
showArea.append(" ");
}
//依次输出每一天,每一个输出项占8个字符宽度
for(int days=1;days<=totalDays;days++)
{ /*for(int j=0;j<5;j++)
{
for(int z=0;z<7;z++)
{
if(!day[j][z].equals(0))
{
day[j][z]=new Integer(days);
}*/
if(days<10)
{
showArea.append(" "+days+" ");
}
else if(days>=10)
{
showArea.append(" "+days+" ");
}
i++;
if(i==7)
{
showArea.append("\n");
i=0;
}
/*}
}*/
}
}
public void itemStateChanged(ItemEvent e)
{
if(e.getStateChange()==ItemEvent.SELECTED)
{
selectYear=(String)(yearBox.getSelectedItem());
selectMonth=(String)(monthBox.getSelectedItem());
showDate(Integer.parseInt(selectYear),Integer.parseInt(selectMonth));
}
}
public static void main(String argv[])
{
new DataSimple();
}
}运行截图:
热心网友
时间:2023-10-12 09:38
日历倒好编,
万年历嘛,农历部分的算法不好算。