?? calendar.java
字號:
package calendar;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.awt.Font;
import java.applet.AudioClip;
import java.net.URL;
import java.applet.Applet;
class calendar extends JFrame{
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("Year");
JLabel m_label = new JLabel("Month");
JComboBox com1 = new JComboBox();
JComboBox com2 = new JComboBox();
JButton button = new JButton("Find");
XYLayout xYLayout1 = new XYLayout();
JButton jButtonMemo = new JButton();
JButton jButtonAlert = new JButton();
JPanel jPanel1 = new JPanel();
XYLayout xYLayout2 = new XYLayout();
Calendar nowt = new GregorianCalendar();
String time;
int re_year, re_month;
int x_size, y_size;
String year_num;
Calendar now = Calendar.getInstance(); //設定Calendar
JPanel jPanel2_time = new JPanel();
JTextField jTextField1 = new JTextField();
XYLayout xYLayout3 = new XYLayout();
FlowLayout flowLayout1 = new FlowLayout();
JEditorPane jEditorPane1_Memo = new JEditorPane();
JEditorPane jEditorPane1_Alert = new JEditorPane();
calendar() {
super("Calendar");
setSize(400, 500);
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);
panel1.add(button);
for (int i = 0; i < 49; i++) {
label[i] = new JLabel("", JLabel.CENTER); //將顯示的字符居中
panel2.add(label[i]);
}
/*panel3.add(new calendar(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);
jbInit();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
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
}
});
setContentPane(panel);
setVisible(true);
setResizable(false);
}
class clock extends Thread {
Font f = new Font("宋體", Font.BOLD, 16);
SimpleDateFormat SDF = new SimpleDateFormat("HH:mm:ss"); //格式化時間顯示類型
public void run() {
while (true) {
try {
Thread.sleep(1000);
Calendar nowt;
nowt = Calendar.getInstance();
time = SDF.format(nowt.getTime()); //得到當前日期和時間
jTextField1.setText(time);
jTextField1.setFont(f);
} catch (InterruptedException e) {
System.out.println("異常");
}
}
}
}
public void jbInit() {
int year, month_num, first_day_num;
String log[] = {"Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"};
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.MAGENTA); //將星期日的日期設置為品紅色
}
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);
}
clock t = new clock();
t.start();
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);
panel3.setLayout(xYLayout1);
jButtonMemo.setFont(new java.awt.Font("Monotype Corsiva",
Font.BOLD | Font.ITALIC, 18));
jButtonMemo.setText("New Memo");
jButtonMemo.addMouseListener(new calendar_jButtonMemo_mouseAdapter(this));
jButtonAlert.setFont(new java.awt.Font("Monotype Corsiva", Font.ITALIC,
20));
jButtonAlert.setForeground(Color.red);
jButtonAlert.setText("Alarm");
jButtonAlert.addMouseListener(new calendar_jButtonAlert_mouseAdapter(this));
xYLayout1.setWidth(401);
xYLayout1.setHeight(291);
panel.setFont(new java.awt.Font("Viner Hand ITC", Font.BOLD, 12));
panel1.setFont(new java.awt.Font("MS UI Gothic", Font.PLAIN, 12));
panel2.setFont(new java.awt.Font("Lucida Fax", Font.PLAIN, 12));
jPanel1.setLayout(xYLayout2);
jPanel2_time.setBackground(Color.pink);
jPanel2_time.setLayout(xYLayout3);
jTextField1.setBackground(UIManager.getColor("inactiveCaptionText"));
jTextField1.setFont(new java.awt.Font("宋體", Font.BOLD, 19));
jTextField1.setForeground(SystemColor.activeCaption);
jTextField1.setText("jTextField1");
this.getContentPane().setLayout(flowLayout1);
jEditorPane1_Memo.setBackground(SystemColor.inactiveCaptionText);
jEditorPane1_Memo.setFont(new java.awt.Font("華文細黑", Font.PLAIN, 16));
jEditorPane1_Memo.setForeground(SystemColor.desktop);
jEditorPane1_Memo.setText("Memo Here");
jEditorPane1_Alert.setBackground(new Color(34, 195, 103));
jEditorPane1_Alert.setFont(new java.awt.Font("Viner Hand ITC",
Font.PLAIN, 15));
jEditorPane1_Alert.setForeground(Color.red);
jEditorPane1_Alert.setText("To-do list");
jPanel2_time.add(jTextField1, new XYConstraints(36, 6, 101, 28));
panel3.add(jButtonAlert, new XYConstraints(299, 233, 90, 40));
panel3.add(jPanel2_time, new XYConstraints(146, 233, 150, 40));
panel3.add(jButtonMemo, new XYConstraints(4, 233, 141, 40));
panel3.add(jEditorPane1_Memo, new XYConstraints(5, 3, 258, 216));
panel3.add(jEditorPane1_Alert, new XYConstraints(267, 3, 122, 216));
}
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;
}
class alarm extends Thread {
String todo = JOptionPane.showInputDialog("To-do");
String alarmtime = JOptionPane.showInputDialog("Set the time");
SimpleDateFormat SDF = new SimpleDateFormat("HH:mm:ss");
public void run() {
while (true) {
try {
Thread.sleep(1000);
Calendar nowt;
nowt = Calendar.getInstance();
time = SDF.format(nowt.getTime()); //得到當前日期和時間
if(alarmtime.compareTo(time.substring(0, 5))== 0){
jEditorPane1_Alert.setText(todo);
String[] sound = { "music.wav" };
URL file1 = getClass().getResource(sound[0]);
AudioClip alarm = Applet.newAudioClip(file1);
alarm.play();
}
else {};
}
catch (InterruptedException e) {
System.out.println("異常");
}
}
}
}
/*public class AudiosApplet extends java.applet.Applet{
AudioClip loopMusic;
public void init(){
loopMusic = getAudioClip();
}
public void start(){
if (loopMusic != null)
loopMusic.loop();
}
public void stop(){
if(loopMusic != null)
loopMusic.stop();
}
}*/
public void Resetday(int week_log, int year_log, int month_log) {
int month_score_log; //判斷是否是閏年的標記
int month_day_score; //存儲月份的天數
int count;
month_score_log = 0;
month_day_score = 0;
count = 1;
if (year_log % 4 == 0 && year_log % 100 != 0 || year_log % 400 == 0) { //判斷是否為閏年
month_score_log = 1;
}
month_log = month_log + 1; //將傳來的月份數加1
switch (month_log) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
month_day_score = 31;
break;
case 4:
case 6:
case 9:
case 11:
month_day_score = 30;
break;
case 2:
if (month_score_log == 1) {
month_day_score = 29;
} else {
month_day_score = 28;
}
break;
}
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);
calendar start=new calendar();
}
public void jButtonMemo_mousePressed(MouseEvent e) {
jEditorPane1_Memo.setText("");
}
public void jButtonAlert_mousePressed(MouseEvent e) {
alarm b= new alarm();
b.start();
}
class calendar_jButtonMemo_mouseAdapter extends MouseAdapter {
private calendar adaptee;
calendar_jButtonMemo_mouseAdapter(calendar adaptee) {
this.adaptee = adaptee;
}
public void mousePressed(MouseEvent e) {
adaptee.jButtonMemo_mousePressed(e);
}
}
}
class calendar_jButtonAlert_mouseAdapter extends MouseAdapter {
private calendar adaptee;
calendar_jButtonAlert_mouseAdapter(calendar adaptee) {
this.adaptee = adaptee;
}
public void mousePressed(MouseEvent e) {
adaptee.jButtonAlert_mousePressed(e);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -