?? denglu.java
字號:
//package com.JavaSeries.Java;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//import java.awt.event.*;
import java.awt.*;
import java.util.*;
//import java.text.*;
public class denglu implements ActionListener {
// 聲明組件
int k = 0;//某年某月某日所對應的星期幾
int l = 0;//后面查找與顯示今天日期所用data[]的index.
Calendar now = Calendar.getInstance();
Integer yearnow = now.get(Calendar.YEAR);//本地年
String nowyear = yearnow.toString();
Integer monthnow = now.get(Calendar.MONTH) + 1;
String nowmonth = monthnow.toString();
Integer d = now.get(Calendar.DATE);
String nowdate = d.toString();
JLabel jlabel = new JLabel("公歷:", SwingConstants.CENTER);
String str1[] = { nowyear, "1995" };
JComboBox jcombobox1 = new JComboBox(str1);
JLabel jlabel2 = new JLabel("年", SwingConstants.LEFT);
String str2[] = { nowmonth, "1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12" };
JComboBox jcombobox2 = new JComboBox(str2);
JLabel jlabel3 = new JLabel("月", SwingConstants.LEFT);
JLabel time = new JLabel("今天:");
JTextField jtextfield = new JTextField();
JLabel[] xingqi = new JLabel[7];
JLabel[] data = new JLabel[42];
// 構造方法
public denglu() {
// 建立框架
JFrame frame = new JFrame("萬年歷");
frame.setSize(440, 330);
frame.setLocation(500, 500);
//三個容器
JPanel jpanel1 = new JPanel();
JPanel jpanel2 = new JPanel();
JPanel jpanel3 = new JPanel();
Container contentPane = frame.getContentPane();
contentPane.setBackground(Color.lightGray);
jpanel1.setLayout(new FlowLayout());
jpanel2.setLayout(new GridLayout(1, 7, 2, 5));
jpanel3.setLayout(new GridLayout(6, 7, 2, 1));
jpanel1.add(jlabel);
jpanel1.add(jcombobox1);
jpanel1.add(jlabel2);
jpanel1.add(jcombobox2);
jpanel1.add(jlabel3);
jpanel1.add(time);
jpanel1.add(jtextfield);
jtextfield.setEditable(false);//設置文本框不可編輯
jcombobox1.setBackground(Color.getHSBColor(20, 25, 36));
jcombobox2.setBackground(Color.getHSBColor(20, 25, 36));
xingqi[0] = new JLabel("日");
xingqi[1] = new JLabel("一");
xingqi[2] = new JLabel("二");
xingqi[3] = new JLabel("三");
xingqi[5] = new JLabel("五");
xingqi[6] = new JLabel("六");
xingqi[0].setForeground(Color.red);
xingqi[6].setForeground(Color.red);
for (int j = 0; j < 7; j++) {
jpanel2.add(xingqi[j]);
Font f1 = new Font("宋體", Font.PLAIN, 20);
xingqi[j].setFont(f1);
xingqi[j].setHorizontalAlignment(SwingConstants.CENTER);
}
for (Integer i = 0; i < 37; i++) {
// String st=i.toString();
data[i] = new JLabel();
data[i].setHorizontalAlignment(SwingConstants.CENTER);
jpanel3.add(data[i]);
}
contentPane.add(jpanel1, BorderLayout.NORTH);
contentPane.add(jpanel2, BorderLayout.CENTER);
contentPane.add(jpanel3, BorderLayout.SOUTH);
jpanel1.setBackground(Color.getHSBColor(20, 125, 36));
jpanel2.setBackground(Color.getHSBColor(20, 205, 6));
jpanel3.setBackground(Color.getHSBColor(20, 25, 156));
//顯示文本框文本
jtextfield.setText(nowyear + "-" + nowmonth + "-" + nowdate);
jtextfield.setBackground(Color.getHSBColor(20, 25, 36));
for (Integer g = 1996; g <= 2050; g++) {
jcombobox1.addItem(g.toString());
}
//設置周六日列為紅色
data[0].setForeground(Color.red);
for (int i = 35; i >= 7; i -= 7) {
data[i].setForeground(Color.red);
data[i - 1].setForeground(Color.red);
}
//調用成員方法
this.month_day();
//設置今天顯示為藍色
while (!nowdate.equals(data[l].getText())) {
l++;
}
data[l].setForeground(Color.blue);
// 設置監(jiān)聽器
jcombobox1.addActionListener(this);
jcombobox2.addActionListener(this);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// 事件處理方法
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 37; i++) {
data[i].setText("");
}
this.month_day();
}
public void month_day() {
// System.out.println(jcombobox1.getSelectedItem());
// String year = str1[jcombobox1.getSelectedIndex()];
//獲取組合框內容
String year = jcombobox1.getSelectedItem().toString();
Integer y = Integer.parseInt(year);
String month = str2[jcombobox2.getSelectedIndex()];
Integer m = Integer.parseInt(month);
//計算某年某月某日所對應星期幾k
int monthnum = 0;
if (m == 2)
if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
monthnum = 29;
else
monthnum = 28;
else if (m == 4 | m == 6 | m == 9 | m == 11)
monthnum = 30;
else
monthnum = 31;
// System.out.println(monthnum);
int c0 = y / 100;
int y0 = (y % 1000) % 100;
int anser = (c0 / 4) - 2 * c0 + y0 + (y0 / 4) + (13 * (m + 1) / 5);
k = (anser + 70) % 7;
for (Integer i = 1; i <= monthnum; i++) {
String st = i.toString();
data[k].setText(st);
Font f1 = new Font("宋體", Font.PLAIN, 30);
data[k].setFont(f1);
k++;
}
if (nowyear.equals(year) && nowmonth.equals(month))
data[l].setForeground(Color.blue);
else if ((l + 1) % 7 == 0 || l % 7 == 0)
data[l].setForeground(Color.red);
else
data[l].setForeground(Color.black);
}
// class happen{
public static void main(String[] args) {
// denglu mm=
new denglu();
// while(true)
// mm.month_day();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -