大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.awt.*;
创新互联是一家专注于网站设计制作、网站设计与策划设计,牙克石网站建设哪家好?创新互联做网站,专注于网站建设10余年,网设计领域的专业建站公司;建站业务涵盖:牙克石等地区。牙克石做网站价格咨询:13518219792
import java.awt.event.*;
import java.io.*;
public class Note extends Frame implements ActionListener{
private Menu filemenu = new Menu("文件");
private Menu editmenu = new Menu("编辑");
private Menu formatmenu = new Menu("格式");
private Menu helpmenu = new Menu("帮助");
private Menu lookmenu = new Menu("查看");
private MenuItem filenew = new MenuItem("新建");
private MenuItem fileopen = new MenuItem("打开...");
private MenuItem filesave = new MenuItem("保存");
private MenuItem fileexit = new MenuItem("退出");
private MenuItem filesaveas = new MenuItem("另存为...");
private MenuItem cut = new MenuItem("剪切");
private MenuItem copy = new MenuItem("复制");
private MenuItem paste = new MenuItem("粘贴");
private MenuItem allselect = new MenuItem("全选");
private MenuItem delete = new MenuItem("删除");
private MenuItem font = new MenuItem("字体...");
private MenuItem statusbar = new MenuItem("状态栏");
private MenuItem help = new MenuItem("帮助...");
private TextArea text = new TextArea();
private String filename="";//用来记录文件地址和文件名
private String pasteplate;//剪切板
private class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{System.exit(0);}
}
public Note(String title)
{
super(title);
//添加文件菜单项
filemenu.add(filenew);
filemenu.add(fileopen);
filemenu.add(filesave);
filemenu.add(filesaveas);
filemenu.addSeparator();
filemenu.add(fileexit);
//添加编辑菜单项
editmenu.add(cut);
editmenu.add(copy);
editmenu.add(paste);
editmenu.add(allselect);
editmenu.add(delete);
//添加格式菜单项
formatmenu.add(font);
//添加查看菜单项
lookmenu.add(statusbar);
//添加帮助菜单项
helpmenu.add(help);
MenuBar menu = new MenuBar();
menu.add(filemenu);
menu.add(editmenu);
menu.add(formatmenu);
menu.add(lookmenu);
menu.add(helpmenu);
setMenuBar(menu);
setLayout(new BorderLayout());
add("Center",text);
text.setEditable(true);
setSize(800,500);
/*
题目:输出任意年份任意月份的日历表(公元后)
思路:
1.已知1年1月1日是星期日,1 % 7 = 1 对应的是星期日,2 % 7 = 2 对应的是星期一,以此类推;
2.计算当年以前所有天数+当年当月1号之前所有天数;
a.年份分平年闰年,平年365天,闰年366天;
b.闰年的判断方法year % 400 == 0 || (year % 100 != 0 year % 4 == 0)若为真,则为闰年否则为平年;
c.定义平年/闰年数组,包含各月天数;
d.遍历数组求和,计算当年当月前总天数;
e.当年以前所有天数+当年当月前总天数+1即为1年1月1日到当年当月1日的总天数;
3.总天数对7取模,根据结果判断当月1号是星期几,输出空白区域;
4.输出当月日历表,逢星期六换行
*/
import java.util.Scanner;
class FindMonthList {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份:");
int year = sc.nextInt(); //年份
if (year 1) { //判断非法输入年份
System.out.println("输入错误!");
return;
}
System.out.println("请输入月份:");
int month = sc.nextInt(); //月份
if (month 1 || month 12) { //判断非法输入月份
System.out.println("输入错误!");
return;
}
//输出表头
System.out.println("-------" + year + " 年 " + month + " 月 " + "-------");
System.out.println();
System.out.println("日 一 二 三 四 五 六");
//计算当前年份以前所有天数beforeYearTotalDay;每4年一个闰年,闰年366天,平年365天
int beforeYearTotalDay = ((year - 1) / 4 * 366) + (year-1 - ((year - 1) / 4)) * 365;
int[] arrLeapYear = {0,31,29,31,30,31,30,31,31,30,31,30,31}; //闰年各月天数 int数组
int[] arrNormalYear = {0,31,28,31,30,31,30,31,31,30,31,30,31}; //平年各月天数 int数组
int beforeMonthTotalDay = 0; //定义本年当月之前月份的总天数
if (year % 400 == 0 || (year % 100 != 0 year % 4 == 0)) { //判断当前年份是否是闰年
for (int i = 0 ; i month ; i ++ ) { //for循环计算当月之前总天数
//计算当前月份之前的所有天数
beforeMonthTotalDay = beforeMonthTotalDay + arrLeapYear[i];
}
//判断当月1日是星期几
int totalDay = beforeYearTotalDay + beforeMonthTotalDay + 1;
int week = totalDay % 7;//已知1年1月1日是星期日,即模7得1对应的是星期日
for (int i = 0 ; i (week - 1 + 7) % 7 ; i ++) { //如果写成i (week-1)会出现i-1的情况
System.out.print(" ");//输出开头空白
}
for (int i = 1 ;i = arrLeapYear[month] ;i ++ ) { //for循环输出各月天数
System.out.print(i + " ");
if (i 10 ) { //小于10的数补一个空格,以便打印整齐
System.out.print(" ");
}
if (i % 7 == ((7-(week - 1)) % 7 ) || i == arrLeapYear[month]) {//每逢星期六/尾数换行
System.out.println();
}
}
} else { //不是闰年就是平年
for (int i = 0 ; i month ; i ++ ) { //for循环计算出当月之前月份总天数
beforeMonthTotalDay = beforeMonthTotalDay + arrNormalYear[i];
}
//判断当月1日是星期几
int totalDay = beforeYearTotalDay + beforeMonthTotalDay + 1;
int week = totalDay % 7;//已知1年1月1日是星期日,即模7得1对应的是星期日
for (int i = 0 ; i (week - 1 + 7) % 7 ; i ++) { //如果写成i (week-1)会出现i-1的情况
System.out.print(" ");//输出开头空白
}
for (int i = 1 ;i = arrNormalYear[month] ;i ++ ) {//for循环输出各月天数
System.out.print(i + " ");
if (i 10 ) { //小于10的数补一个空格,以便打印整齐
System.out.print(" ");
}
if (i % 7 == ((7-(week - 1)) % 7 ) || i == arrNormalYear[month]) {//每逢星期六/尾数换行
System.out.println();
}
}
}
}
}
显示效果:
也不知道你具体需求是什么,以前改过一个日历程序,一共四个java类,放在同一个包里。经测试可以运行。
//Start.java
import java.awt.*;
import javax.swing.*;
class Start{
public static void main(String [] args){
DateFrame frame=new DateFrame();
frame.setLocationRelativeTo(frame);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
//DateInfo.java
import java.util.*;
public class DateInfo{
private int mYear, mMonth;
private int mDayOfMonth, mFristWeek;
public DateInfo(int year, int month) throws DateException{
mYear = year;
if (month 0 || month 12){
throw (new DateException());
}
mMonth = month;
mDayOfMonth = getDayOfMonth(mYear, mMonth);
mFristWeek = getFristWeek(mYear, mMonth);
}
private int getDayOfMonth(int year, int month){
int[][] ary = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
return (ary[isLeapYear(year)][month]);
}
private int isLeapYear(int year){
if (year % 4 == 0 year % 100 != 0 ||year % 400 == 0){
return (1);
}
else{
return (0);
}
}
private int getFristWeek(int year, int month){
java.util.Calendar cal = Calendar.getInstance();
cal.set(year, month - 1, 1);
return (cal.get(Calendar.DAY_OF_WEEK) - 1);
}
public String toString(){
String str;
str = "\t\t" + mYear + "年" + mMonth + "月\n";
str += "日\t一\t二\t三\t四\t五\t六\n";
int i;
for (i = 1; i = mFristWeek; i++){
str += " \t";
}
for (int j = 1; j = mDayOfMonth; j++, i++){
str +=j+"\t" ;
if (i % 7 == 0){
str += "\n";
}
}
return (str);
}
}
//DateFrame.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Calendar;
class DateFrame extends JFrame implements Runnable{
Calendar date=Calendar.getInstance();
String[] str={"1","2","3","4","5","6","7","8","9","10","11","12"};
JLabel lblYear=new JLabel("年 ");
JLabel lblMonth=new JLabel("月 ");
JLabel lblDate=new JLabel("现在的时间是:");
JLabel lblShowDate=new JLabel();
// javax.swing.JTextField trxt =new JTextField(10);
// trxt.setHorizontalAlignment(JTextField.RIGHT); //设置文本从右边输入
JComboBox cboMonth=new JComboBox(str);
JComboBox cboYear=new JComboBox();
JTextArea txaShow=new JTextArea();
JPanel pnlNorth=new JPanel();
JPanel pnlSOUTH=new JPanel();
JButton btnShow=new JButton("显示");
JButton btnClose=new JButton("关闭");
JScrollPane jsp=new JScrollPane(txaShow);
Container c=this.getContentPane();
public DateFrame(){
Thread thread=new Thread(this);
thread.start();
this.setTitle("玩玩日历拉!!!");
this.setSize(300,260);
for (int i = 1990; i=2025; i++) {
cboYear.addItem(""+i);
}
cboYear.setSelectedItem(""+(date.get(Calendar.YEAR)));
cboMonth.setSelectedItem(""+(date.get(Calendar.MONTH)+1));
pnlNorth.add(cboYear);
txaShow.setTabSize(4); //设置tab键的距离
txaShow.setForeground(Color.GREEN);
pnlNorth.add(lblYear);
pnlNorth.add(cboMonth);
pnlNorth.add(lblMonth);
pnlNorth.add(lblDate);
pnlNorth.add(lblShowDate);
c.add(pnlNorth,BorderLayout.NORTH);
c.add(jsp);
pnlSOUTH.add(btnShow);
pnlSOUTH.add(btnClose);
c.add(pnlSOUTH,BorderLayout.SOUTH);
btnShow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int year=Integer.parseInt((String)cboYear.getSelectedItem());
int month=Integer.parseInt((String)cboMonth.getSelectedItem());
try {
DateInfo date=new DateInfo(year,month);
txaShow.setText(""+date);
}
catch (DateException ex) {
ex.printStackTrace();
}
}
});
btnClose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
}
public void run(){
try {
while(true){
Thread.sleep(1000);
int hour=date.get(Calendar.HOUR);
int minute=date.get(Calendar.MINUTE);
int second=date.get(Calendar.SECOND);
String str=hour+":"+minute+":"+second;
lblShowDate.setText(str);
//this.repaint();
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
//DateException.java
public class DateException extends Exception{
public DateException(){
super("日期数据不合法.");
}
}
import java.util.Date;
import java.util.Calendar;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.awt.Color;
import java.awt.Font;
import java.awt.Point;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.border.LineBorder;
/**
* @company:NEUSOFT
* @Title:日期选择控件
* @Description:在原有基础上修改了以下内容:
* 1. 将容器由Frame改为了Dialog,以便在基于对话框的程序中也能够使用
* 2. 将最小日期由1980改为了1950,考虑到目前球员的出生日期可能早于1980年
* 3. 将初始显示格式设置为 yyyy年MM月dd日 格式,原有的小时去掉了,不适合于出生日期字段
*/
public class DateChooserJButton extends JButton {
private DateChooser dateChooser = null;
private String preLabel = "";
public DateChooserJButton() {
this(getNowDate());
}
public DateChooserJButton(SimpleDateFormat df, String dateString) {
this();
setText(df, dateString);
}
public DateChooserJButton(Date date) {
this("", date);
}
public DateChooserJButton(String preLabel, Date date) {
if (preLabel != null)
this.preLabel = preLabel;
setDate(date);
setBorder(null);
setCursor(new Cursor(Cursor.HAND_CURSOR));
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (dateChooser == null)
dateChooser = new DateChooser();
Point p = getLocationOnScreen();
p.y = p.y + 30;
dateChooser.showDateChooser(p);
}
});
}
private static Date getNowDate() {
return Calendar.getInstance().getTime();
}
private static SimpleDateFormat getDefaultDateFormat() {
return new SimpleDateFormat("yyyy年MM月dd日");
}
// 覆盖父类的方法
public void setText(String s) {
Date date;
try {
date = getDefaultDateFormat().parse(s);
} catch (ParseException e) {
date = getNowDate();
}
setDate(date);
}
public void setText(SimpleDateFormat df, String s) {
Date date;
try {
date = df.parse(s);
} catch (ParseException e) {
date = getNowDate();
}
setDate(date);
}
public void setDate(Date date) {
super.setText(preLabel + getDefaultDateFormat().format(date));
}
public Date getDate() {
String dateString = this.getText().substring(preLabel.length());
try {
return getDefaultDateFormat().parse(dateString);
} catch (ParseException e) {
return getNowDate();
}
}
public String getDateString()
{
Date birth =getDate();
DateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
return formatDate.format(birth).toString();
//return this.getText().substring(preLabel.length());
}
// 覆盖父类的方法使之无效
//public void addActionListener(ActionListener listener) {
//}
private class DateChooser extends JPanel implements ActionListener,
ChangeListener {
int startYear = 1950; // 默认【最小】显示年份
int lastYear = 2050; // 默认【最大】显示年份
int width = 200; // 界面宽度
int height = 200; // 界面高度
Color backGroundColor = Color.gray; // 底色
// 月历表格配色----------------//
Color palletTableColor = Color.white; // 日历表底色
Color todayBackColor = Color.orange; // 今天背景色
Color weekFontColor = Color.blue; // 星期文字色
Color dateFontColor = Color.black; // 日期文字色
Color weekendFontColor = Color.red; // 周末文字色
// 控制条配色------------------//
Color controlLineColor = Color.pink; // 控制条底色
Color controlTextColor = Color.white; // 控制条标签文字色
Color rbFontColor = Color.white; // RoundBox文字色
Color rbBorderColor = Color.red; // RoundBox边框色
Color rbButtonColor = Color.pink; // RoundBox按钮色
Color rbBtFontColor = Color.red; // RoundBox按钮文字色
JDialog dialog;
JSpinner yearSpin;
JSpinner monthSpin;
JSpinner hourSpin;
JButton[][] daysButton = new JButton[6][7];
DateChooser() {
setLayout(new BorderLayout());
setBorder(new LineBorder(backGroundColor, 2));
setBackground(backGroundColor);
JPanel topYearAndMonth = createYearAndMonthPanal();
add(topYearAndMonth, BorderLayout.NORTH);
JPanel centerWeekAndDay = createWeekAndDayPanal();
add(centerWeekAndDay, BorderLayout.CENTER);
}
private JPanel createYearAndMonthPanal() {
Calendar c = getCalendar();
int currentYear = c.get(Calendar.YEAR);
int currentMonth = c.get(Calendar.MONTH) + 1;
int currentHour = c.get(Calendar.HOUR_OF_DAY);
JPanel result = new JPanel();
result.setLayout(new FlowLayout());
result.setBackground(controlLineColor);
yearSpin = new JSpinner(new SpinnerNumberModel(currentYear,
startYear, lastYear, 1));
yearSpin.setPreferredSize(new Dimension(48, 20));
yearSpin.setName("Year");
yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####"));
yearSpin.addChangeListener(this);
result.add(yearSpin);
JLabel yearLabel = new JLabel("年");
yearLabel.setForeground(controlTextColor);
result.add(yearLabel);
monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1,
12, 1));
monthSpin.setPreferredSize(new Dimension(35, 20));
monthSpin.setName("Month");
monthSpin.addChangeListener(this);
result.add(monthSpin);
JLabel monthLabel = new JLabel("月");
monthLabel.setForeground(controlTextColor);
result.add(monthLabel);
hourSpin = new JSpinner(new SpinnerNumberModel(currentHour, 0, 23,
1));
hourSpin.setPreferredSize(new Dimension(35, 20));
hourSpin.setName("Hour");
hourSpin.addChangeListener(this);
result.add(hourSpin);
JLabel hourLabel = new JLabel("时");
hourLabel.setForeground(controlTextColor);
result.add(hourLabel);
return result;
}
private JPanel createWeekAndDayPanal() {
String colname[] = { "日", "一", "二", "三", "四", "五", "六" };
JPanel result = new JPanel();
// 设置固定字体,以免调用环境改变影响界面美观
result.setFont(new Font("宋体", Font.PLAIN, 12));
result.setLayout(new GridLayout(7, 7));
result.setBackground(Color.white);
JLabel cell;
for (int i = 0; i 7; i++) {
cell = new JLabel(colname[i]);
cell.setHorizontalAlignment(JLabel.RIGHT);
if (i == 0 || i == 6)
cell.setForeground(weekendFontColor);
else
cell.setForeground(weekFontColor);
result.add(cell);
}
int actionCommandId = 0;
for (int i = 0; i 6; i++)
for (int j = 0; j 7; j++) {
JButton numberButton = new JButton();
numberButton.setBorder(null);
numberButton.setHorizontalAlignment(SwingConstants.RIGHT);
numberButton.setActionCommand(String
.valueOf(actionCommandId));
numberButton.addActionListener(this);
numberButton.setBackground(palletTableColor);
numberButton.setForeground(dateFontColor);
if (j == 0 || j == 6)
numberButton.setForeground(weekendFontColor);
else
numberButton.setForeground(dateFontColor);
daysButton[i][j] = numberButton;
result.add(numberButton);
actionCommandId++;
}
return result;
}
private JDialog createDialog(JDialog owner) {
JDialog result = new JDialog(owner, "日期时间选择", true);
result.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
result.getContentPane().add(this, BorderLayout.CENTER);
result.pack();
result.setSize(width, height);
return result;
}
void showDateChooser(Point position) {
JDialog owner = (JDialog) SwingUtilities
.getWindowAncestor(DateChooserJButton.this);
if (dialog == null || dialog.getOwner() != owner)
dialog = createDialog(owner);
dialog.setLocation(getAppropriateLocation(owner, position));
flushWeekAndDay();
dialog.setVisible(true);
}
Point getAppropriateLocation(JDialog owner, Point position) {
Point result = new Point(position);
Point p = owner.getLocation();
int offsetX = (position.x + width) - (p.x + owner.getWidth());
int offsetY = (position.y + height) - (p.y + owner.getHeight());
if (offsetX 0) {
result.x -= offsetX;
}
if (offsetY 0) {
result.y -= offsetY;
}
return result;
}
private Calendar getCalendar() {
Calendar result = Calendar.getInstance();
result.setTime(getDate());
return result;
}
private int getSelectedYear() {
return ((Integer) yearSpin.getValue()).intValue();
}
private int getSelectedMonth() {
return ((Integer) monthSpin.getValue()).intValue();
}
private int getSelectedHour() {
return ((Integer) hourSpin.getValue()).intValue();
}
private void dayColorUpdate(boolean isOldDay) {
Calendar c = getCalendar();
int day = c.get(Calendar.DAY_OF_MONTH);
c.set(Calendar.DAY_OF_MONTH, 1);
int actionCommandId = day - 2 + c.get(Calendar.DAY_OF_WEEK);
int i = actionCommandId / 7;
int j = actionCommandId % 7;
if (isOldDay)
daysButton[i][j].setForeground(dateFontColor);
else
daysButton[i][j].setForeground(todayBackColor);
}
private void flushWeekAndDay() {
Calendar c = getCalendar();
c.set(Calendar.DAY_OF_MONTH, 1);
int maxDayNo = c.getActualMaximum(Calendar.DAY_OF_MONTH);
int dayNo = 2 - c.get(Calendar.DAY_OF_WEEK);
for (int i = 0; i 6; i++) {
for (int j = 0; j 7; j++) {
String s = "";
if (dayNo = 1 dayNo = maxDayNo)
s = String.valueOf(dayNo);
daysButton[i][j].setText(s);
dayNo++;
}
}
dayColorUpdate(false);
}
public void stateChanged(ChangeEvent e) {
JSpinner source = (JSpinner) e.getSource();
Calendar c = getCalendar();
if (source.getName().equals("Hour")) {
c.set(Calendar.HOUR_OF_DAY, getSelectedHour());
setDate(c.getTime());
return;
}
dayColorUpdate(true);
if (source.getName().equals("Year"))
c.set(Calendar.YEAR, getSelectedYear());
else
// (source.getName().equals("Month"))
c.set(Calendar.MONTH, getSelectedMonth() - 1);
setDate(c.getTime());
flushWeekAndDay();
}
public void actionPerformed(ActionEvent e) {
JButton source = (JButton) e.getSource();
if (source.getText().length() == 0)
return;
dayColorUpdate(true);
source.setForeground(todayBackColor);
int newDay = Integer.parseInt(source.getText());
Calendar c = getCalendar();
c.set(Calendar.DAY_OF_MONTH, newDay);
setDate(c.getTime());
}
}
}
这是一个专门的选日期的类 ,你看看完了调用就行了
类CalendarPrinter有两个主要的方法printCal()和printOut(),printCal()打印你输入年份的每一个月份,printOut()打印每一个月的每一天,在printCal()方法先创建一个格里高利历法的对象,并把你输入的年份设置为该历法的年份,然后for循环打印12个月份,然后再把每个月分别设置为你输入那年的每个月,接着调用printOut()方法打印每一天,在printOut()方法中,先获得月份,然后把日期设置为该月的第一天,接着在获取这月的第一天是一周中的周几weekday,然后打印出第一行“Sun MOn Tue Wed Thu Fri Sat”,接着就进入for循环,这个for循环就是打印每个月第一天开始前的空格,接着就进入do……while循环,获得天day,如果day10,打印day的时候day前面的空格大一些,否则day前面的空格就小一些,为了排版好看,然后判断是否是周六,如果是周六就换行,然后把day+1,重新获得新的day在一周中是周几。循环一直下去,该月的天从1一直加到30或31,再加的话月份就该+1,所以do……while循环结束的条件就是进入到下一个月。最后判断该月的最后一天是不是该月的周日,如果不是就换行