实践作业三 结对项目
學號: 201303014008 ?姓名:張燕 ? 班級:計科(高職)13-1
?
一、題目簡介
? 利用Java GUI的組件類、容器類和輔助類設計所選項目的用戶界面,并在深入理解并掌握事件驅動程序設計原理的基礎上,實現事件驅動的程序設計。
? 1.實現圖形界面。
? 2.能以月歷形式顯示日期與星期。工作日、雙休日、當天分別以黑色、紅色和藍色顯示。
? 3.支持用戶通過下拉形式菜單來選擇月份。
? 4.通過點擊“現在日期”來刷新日歷。
?
二.結對分工
?
結對者: 張燕 ? 黃彥瀟
?
?張燕:代碼編寫以及程序測試
?
?黃彥瀟:程序分析及代碼規范
?
三.代碼地址:
?
https://github.com/yranqiu/test/blob/master/shiyan3
?
四.測試代碼:
?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
?
public class CalenderTrain extends JFrame implements ActionListener {
JComboBox Month = new JComboBox(); //月份下拉列表框
JComboBox Year = new JComboBox(); //年份下拉列表框
JLabel Year_l = new JLabel("年份:"); //定義標簽
JLabel Month_l = new JLabel("月份:"); //定義標簽
Date now_date = new Date(); //獲取今天的日期
JButton[] button_day = new JButton[49]; //定義一個數組用來存放日期
JButton button_jump = new JButton("日期跳轉"); //現實選擇日期
JButton button_today = new JButton("現在日期"); //顯示今天日期按鈕
int now_year = now_date.getYear() + 1900; //獲取年份值
int now_month = now_date.getMonth(); //獲取月份值(當前月份-1)
boolean bool = false;
String year_int = null; //存放年份
int month_int; //存放月份
JPanel pane_ym = new JPanel(); //放置下拉列表框和控制按鈕面板
JPanel pane_day = new JPanel(); //放置日期面板
JPanel pane_parent = new JPanel(); //放置以上兩個面板
//定義方法繪制面板
?
public CalenderTrain() {
super("JAVA日歷程序"); //設定面板標題
//---以下幾行使得關閉面板時退出程序
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClose(WindowEvent e) {
//System.out.print("CLOSING THE WIN");
?
System.exit(0);
}
});
?
setResizable(false); //面板的大小不能變化
//設定年月
/*年份的區間是當前年份的過去10年到當前年份的未來20年
* 月份正常1-12月
*/
for (int i = now_year - 10; i <= now_year + 20; i++) {
Year.addItem(i + "");
}
for (int i = 1; i < 13; i++) {
Month.addItem(i + "");
}
Year.setSelectedIndex(10); //設定年份下拉列表為當前年份
//Year.setText()
pane_ym.add(Year_l); //添加年份標簽
pane_ym.add(Year); //添加年份下拉列表框
Month.setSelectedIndex(now_month); //設定月份下拉列表為當前月份
pane_ym.add(Month_l); //添加月份標簽
pane_ym.add(Month); //添加月份下拉列表框
pane_ym.add(button_jump); //添加跳轉按鈕
pane_ym.add(button_today); //添加“現在日期”按鈕
button_jump.addActionListener(this); //跳轉按鈕添加 監聽事件
button_today.addActionListener(this); //“現在日期”按鈕添加 監聽事件
//年月設定結束
//初始化日期按鈕并繪制
pane_day.setLayout(new GridLayout(7, 7));
for (int i = 0; i < 49; i++) {
button_day[i] = new JButton(" ");
pane_day.add(button_day[i]);
}
this.setDay(); //調用setDay()方法
pane_parent.setLayout(new BorderLayout()); //設定布局管理器
setContentPane(pane_day);
setContentPane(pane_ym);
pane_parent.add(pane_day, BorderLayout.SOUTH);
pane_parent.add(pane_ym, BorderLayout.NORTH);
setContentPane(pane_parent);
pack();
show();
}
?
//SET DAY 方法,顯示日期
void setDay() {
if (bool) {
year_int = now_year + "";
month_int = now_month;
} else {
year_int = Year.getSelectedItem().toString();
month_int = Month.getSelectedIndex();
??? //year_int=Year.getText();
??? //month_int=Integer.parseInt(Month.getText());
}
?
int year_sel = Integer.parseInt(year_int) - 1900; //獲得年份值
Date dt = new Date(year_sel, month_int, 1); //構造一個日期
GregorianCalendar cal = new GregorianCalendar(); //創建一個Calendar實例
cal.setTime(dt);
String week[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
int day = 0; //day中存放某個月份的天數
int day_week = 0; //用來存放某個月的第一天是星期幾的數值
//--將星期添加到前7個按鈕中
for (int i = 0; i < 7; i++) {
button_day[i].setText(week[i]);
}
//--
/*判斷是幾月份,根據它來設定day的值
* 其中二月份要判斷是否是閏年
*/
if (month_int == 0
|| month_int == 2
|| month_int == 4
|| month_int == 6
|| month_int == 7
|| month_int == 9
|| month_int == 11) {
day = 31;
} else if (
month_int == 3
|| month_int == 5
|| month_int == 8
|| month_int == 10) {
day = 30;
} else {
if (cal.isLeapYear(year_sel)) {
day = 29;
} else {
day = 28;
}
}
day_week = 7 + dt.getDay();
int count = 1;
/*繪制按鈕
*首先要根據選定的月份的第一天是星期幾來確定我們繪制按鈕的起始位置
* 其中day_week就是我們要繪制的起始位置
* 對于那些沒有數值可以顯示的按鈕要置空
*/
for (int i = day_week; i < day_week + day; count++, i++) {
if (i % 7 == 0
|| i == 13
|| i == 20
|| i == 27
|| i == 48
|| i == 34
|| i == 41) {
if (i == day_week + now_date.getDate() - 1) {
button_day[i].setForeground(Color.blue);
button_day[i].setText(count + "");
} else {
button_day[i].setForeground(Color.red);
button_day[i].setText(count + "");
}
?
} else {
if (i == day_week + now_date.getDate() - 1) {
button_day[i].setForeground(Color.blue);
button_day[i].setText(count + "");
} else {
button_day[i].setForeground(Color.black);
button_day[i].setText(count + "");
}
}
}
?
?
//--對于沒有日期數值顯示的按鈕進行置空處理
if (day_week == 0) {
for (int i = day; i < 49; i++) {
button_day[i].setText(" ");
}
} else {
//第一天前面的按鈕置空
for (int i = 7; i < day_week; i++) {
button_day[i].setText(" ");
} //最后一天后面的按鈕置空
for (int i = day_week + day; i < 49; i++) {
button_day[i].setText(" ");
}
}
}
?
?
//點擊按鈕產生的效果
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button_jump) {
bool = false;
this.setDay(); //如果點擊跳轉按鈕就調用setDay()方法重新繪制按鈕
?
} else if (e.getSource() == button_today) {
bool = true;
this.setDay(); //如果點擊現在日期按鈕,得到今天的日期
Month.setSelectedIndex(now_month);//將月份置為當前月份
Year.setSelectedIndex(10); //將年份置為當前年份
?
}
}
public static void main(String[] args) {
??? CalenderTrain ct = new CalenderTrain();
}
}
?
}
?五、測試結果
?
六.問題及心得:
在本次實踐中,代碼編寫經常出現錯誤,運行了好幾次才成功,通過結對,我學會了和隊員進行合作,容易提高效率和找到自己的錯誤并改正。
?
?
轉載于:https://www.cnblogs.com/yanyuranqiu/p/4501749.html
總結
以上是生活随笔為你收集整理的实践作业三 结对项目的全部內容,希望文章能夠幫你解決所遇到的問題。