⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newapplet.java

📁 这个程序主要是用来显示日历的
💻 JAVA
字号:
// CalendarPanel.java: Display calendar for a month
import java.applet.Applet;
import java.awt.*;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.util.*;
import java.text.*;

@SuppressWarnings("serial")
class CalendarPanel extends JPanel {
  // The header label
  private JLabel jlblHeader = new JLabel(" ", JLabel.CENTER);

  // Labels to display day names and days
  private JButton[] jlblDay = new JButton[49];

  private Calendar calendar = new GregorianCalendar();
  private int month;  // The specified month
  private int year;  // The specified year
  Button nextYear,nextMonth,lastYear,lastMonth;
  TextField Year,Month;
  /** Default constructor */
  public CalendarPanel() {
    // Panel jpDays to hold day names and days
    JPanel jpDays = new JPanel();
    jpDays.setLayout(new GridLayout(7, 1));
    for (int i = 0; i < 49; i++) {
      jpDays.add(jlblDay[i] = new JButton());
      jlblDay[i].setBorder(new LineBorder(Color.black, 1));
      jlblDay[i].setHorizontalAlignment(JButton.CENTER);
      jlblDay[i].setVerticalAlignment(JButton.BOTTOM);
    }

    // Place header and calendar body in the panel
    this.setLayout(new BorderLayout());
    this.add(jlblHeader, BorderLayout.NORTH);
    this.add(jpDays, BorderLayout.CENTER);

    // Set current month, and year
    calendar = new GregorianCalendar();
    month = calendar.get(Calendar.MONTH) + 1;
    year = calendar.get(Calendar.YEAR);

    // Show calendar
    showHeader();
    showDayNames();
    showDays();
  }

  /** Update the header based on locale */
  private void showHeader() {
	  	setLayout(new FlowLayout(FlowLayout.CENTER));
		add(lastYear = new Button("<"));
		add(Year = new TextField("" + calendar.get(Calendar.YEAR),4));
		add(nextYear = new Button(">"));
		add(lastMonth = new Button("<"));
		add(Month = new TextField("" + (calendar.get(Calendar.MONTH) + 1),2));
		add(nextMonth = new Button(">"));
  }

  /** Update the day names based on locale */
  private void showDayNames() {
    DateFormatSymbols dfs = new DateFormatSymbols(getLocale());
    String dayNames[] = dfs.getWeekdays();

    // Set calendar days
    for (int i = 0; i < 7; i++) {
      jlblDay[i].setText(dayNames[i + 1]);
      jlblDay[i].setHorizontalAlignment(JLabel.CENTER);
    }
  }

  /** Display days */
  public void showDays() {
    // Set the calendar to the first day of the
    // specified month and year
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONTH, month-1);
    calendar.set(Calendar.DATE, 1);
    System.out.print("month:" + month + "  " + "Date:" + calendar.get(Calendar.YEAR) + "/" + (calendar.get(Calendar.MONTH)+1) + "   ");
    System.out.println("day:" + calendar.get(Calendar.DATE));
	//  calendar.set(year, month-1,Calendar.DATE);
    // Get the day of the first day in a month
    int startingDayOfMonth = calendar.get(Calendar.DAY_OF_WEEK);

    // Fill the calendar with the days before this month
    Calendar cloneCalendar = (Calendar)calendar.clone();
    cloneCalendar.add(Calendar.DATE, -1);
    int daysInMonth = cloneCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    
    System.out.println("days:" + daysInMonth);
    for (int i = 0; i < startingDayOfMonth - 1; i++) {
      jlblDay[i + 7].setForeground(Color.yellow);
      jlblDay[i + 7].setText(daysInMonth - startingDayOfMonth + 2 + i + "");
    }

    // Display days of this month
    for (int i = 1; i <= daysInMonth; i++) {
      jlblDay[i - 2 + startingDayOfMonth + 7].
        setForeground(Color.black);
      jlblDay[i - 2 + startingDayOfMonth + 7].setText(i + "");
    }

    // Fill the calendar with the days after this month
    int j = 1;
    for (int i = daysInMonth - 1 + startingDayOfMonth + 7;
      i < 49; i++) {
      jlblDay[i].setForeground(Color.yellow);
      jlblDay[i].setText(j++ + "");
    }
  }
  public boolean action(Event evt, Object arg){
		if(evt.target.equals(lastYear)){
			year--;
			setYear(year);
		//	setMonth(month);
			Year.setText("" + year);
		//	repaint();
		}
		if(evt.target.equals(nextYear)){
			year++;
			setYear(year);
		//	setMonth(month);
			Year.setText("" + year);
		//	repaint();
		}
		if(evt.target.equals(lastMonth)){
			if(Integer.valueOf(Month.getText()).intValue() == 1)
			{
				month = 13;
				year--;
				Year.setText("" + year);
			}
			month--;
		//	setYear(year);
			setMonth(month);
			Month.setText("" + month);
		//	repaint();
		}
		if(evt.target.equals(nextMonth)){
			if(Integer.valueOf(Month.getText()).intValue() == 12)
			{
				month = 0;
				year++;
				Year.setText("" + year);
			}
			month++;
		//	setYear(year);
			setMonth(month);
			Month.setText("" + month);
		//	repaint();
		}
/*		if(evt.target.equals(lastDay)){
			today.setDate(today.getDate() - 1);
			repaint();
		}	
		if(evt.target.equals(nextDay)){
			today.setDate(today.getDate() + 1);
			repaint();
		}
		setDateText();
*/		return true;
	}
  /** Return month */
  public int getMonth() {
    return month;
  }

  /** Set a new month */
  public void setMonth(int newmonth) {
    month = newmonth;
    showDays();
  }

  /** Return year */
  public int getYear() {
    return year;
  }

  /** Set a new year */
  public void setYear(int newyear) {
    year = newyear;
    showDays();
  }

  /** Set a new locale */
  public void changeLocale(Locale newLocale) {
	  showHeader();
	  setLocale(newLocale);
	  showDayNames();
  }
}
@SuppressWarnings("serial")
public class NewApplet extends Applet{
	CalendarPanel calendar;
	
	public void init(){
		calendar = new CalendarPanel();
		setLayout(new BorderLayout());
		add("Center", calendar);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -