📄 newjframe.java
字号:
{//建立清除按鈕開始
jButton1 = new JButton();
jDesktopPane1.add(jButton1);
jButton1.setText("清除");
jButton1.setBounds(252, 231, 63, 21);
jButton1.setFont(new java.awt.Font("新細明體",0,11));
jButton1.setBorder(BorderFactory.createTitledBorder(""));
jButton1.addActionListener(new ActionListener() {//設定清除按鈕監聽函數
public void actionPerformed(ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
}//建立清除按鈕結束
{//建立儲存按鈕開始
jButton2 = new JButton();
jDesktopPane1.add(jButton2);
jButton2.setText("储存");
jButton2.setBounds(364, 231, 63, 21);
jButton2.setFont(new java.awt.Font("新細明體",0,11));
jButton2.setBorder(BorderFactory.createTitledBorder(""));
jButton2.addActionListener(new ActionListener() {//設定儲存按鈕監聽函數
public void actionPerformed(ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
}//建立儲存按鈕結束
{//建立星期一~星期六標籤開始
jLabel2 = new JLabel();
jDesktopPane1.add(jLabel2);
jLabel2.setText("Mon Tue Wed Thu Fri Sat");
jLabel2.setBounds(49, 7, 189, 21);
jLabel2.setFont(new java.awt.Font("Arial",0,11));
}//建立星期一~星期六標籤結束
{//建立查詢年份輸入框開始
jTextField1 = new JTextField();
jDesktopPane1.add(jTextField1);
jTextField1.setText("");
jTextField1.setBounds(14, 231, 63, 21);
jTextField1.setFont(new java.awt.Font("Arial",0,12));
}//建立查詢年份輸入框結束
{//建立狀態標籤開始
jLabel6 = new JLabel();
jDesktopPane1.add(jLabel6);
jLabel6.setText("TEST");
jLabel6.setBounds(357, 7, 70, 21);
jLabel6.setFont(new java.awt.Font("新細明體",0,11));
jLabel6.setForeground(new java.awt.Color(0,0,255));
}//建立狀態標籤開始
{//建立隱藏日期按鈕暫存標籤開始
jLabel7 = new JLabel();
jDesktopPane1.add(jLabel7);
jLabel7.setText("");
jLabel7.setBounds(0, 0, 0, 0);//設定大小為0
}//建立隱藏日期按鈕暫存標籤結束
{//建立目前選擇日期標籤開始
jLabel8 = new JLabel();
jDesktopPane1.add(jLabel8);
jLabel8.setText("目前无选择日期");
jLabel8.setBounds(252, 28, 175, 14);
jLabel8.setFont(new java.awt.Font("新細明體",0,11));
}//建立目前選擇日期標籤結束
{//建立月份下拉選單開始
ComboBoxModel jComboBox1Model = new DefaultComboBoxModel
(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" });//內容設定1~12
jComboBox1 = new JComboBox();
jDesktopPane1.add(jComboBox1);
jComboBox1.setModel(jComboBox1Model);
jComboBox1.setBounds(105, 231, 40, 21);
jComboBox1.setFont(new java.awt.Font("Arial",0,11));
}//建立月份下拉選單結束
{//建立"月"標籤開始
jLabel3 = new JLabel();
jDesktopPane1.add(jLabel3);
jLabel3.setText("月");
jLabel3.setBounds(155, 231, 14, 21);
jLabel3.setFont(new java.awt.Font("新細明體",0,12));
}//建立"月"標籤結束
{//建立"年"標籤開始
jLabel4 = new JLabel();
jDesktopPane1.add(jLabel4);
jLabel4.setText("年");
jLabel4.setFont(new java.awt.Font("新細明體", 0, 12));
jLabel4.setBounds(84, 231, 14, 21);
}//建立"年"標籤結束
{//建立查詢按鈕開始
jButton3 = new JButton();
jDesktopPane1.add(jButton3);
jButton3.setText("查询");
jButton3.setBounds(180, 231, 35, 21);
jButton3.setBorder(BorderFactory.createTitledBorder(""));
jButton3.setFont(new java.awt.Font("新細明體",0,11));
jButton3.addActionListener(new ActionListener() {//查詢按鈕監聽函數
public void actionPerformed(ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
}//建立查詢按鈕結束
{//建立目前年月位置標籤開始
int[] now = new int[3];
now = getdate();//預設為當年當月
String year5,smonth;
year5 = String.valueOf(now[0]);
smonth = String.valueOf(now[1]);
if (smonth.length() == 1)
smonth = "0"+smonth;
jLabel5 = new JLabel();
jDesktopPane1.add(jLabel5);
jLabel5.setText(year5+" 年 "+smonth+" 月");
jLabel5.setBounds(252, 7, 120, 21);
jLabel5.setForeground(new java.awt.Color(255,255,255));//設定字體為白色
}//建立目前年月位置標籤結束
{//預設為當年當月日期按鈕產生開始
int[] now = new int[3];
now = getdate();
date_btn_create(now[0],now[1]);//產生日期按鈕
}//預設為當年當月日期按鈕產生結束
}
pack();
this.setSize(444, 296);
} catch (Exception e) {//例外處理
e.printStackTrace();
}
}
private void jButton1ActionPerformed(ActionEvent evt)//清除按鈕按下觸發事件
{
jTextArea1.setText("");//清除記事內容
String year,month,day,filename,insert_str;
year = jLabel5.getText().substring(0,4);
month = jLabel5.getText().substring(7,9);
day = jLabel7.getText();
filename = year+month+day;
File file=new File(filename+".txt");//刪除當日記事檔案
file.delete();
new_btn();//重新產生按鈕
jLabel6.setText("行事曆已清除");//設定相關訊息
jLabel7.setText("");
jLabel8.setText("目前無選擇日期");
}
private void jButton2ActionPerformed(ActionEvent evt)//儲存按鈕按下觸發事件開始
{
String year,month,day,filename,insert_str;
year = jLabel5.getText().substring(0,4);
month = jLabel5.getText().substring(7,9);
day = jLabel7.getText();
filename = year+month+day;
insert_str = jTextArea1.getText();//記事內容
if (insert_str.length() != 0 && day.length() != 0)//若記事框內有文字且有選擇日期則儲存記事檔案
{
try
{
FileWriter fw=new FileWriter(filename+".txt");//啟用檔案寫入
BufferedWriter bfw=new BufferedWriter(fw);//啟用緩衝區寫入
bfw.write(insert_str); //將Textarea內容寫入緩衝區裡
bfw.flush();//將緩衝區資料寫到檔案
fw.close();//關閉檔案
jLabel6.setText("行事历已记录");//設定相關訊息
jLabel7.setText("");
jLabel8.setText("目前无选择日期");
new_btn();
}catch(IOException e)
{
e.printStackTrace();
}
}
else//若無記事內容或無選擇日期
{
if (day.length() == 0)
jLabel6.setText("日期未選擇");//設定相關訊息
else
jLabel6.setText("行事曆無內容");
}
}//儲存按鈕按下觸發事件結束
private void jButton3ActionPerformed(ActionEvent evt)//查詢按鈕按下觸發事件開始
{
String syear,smonth;
try
{
jTextArea1.setText("");
jLabel6.setText("查詢日期");
syear = jTextField1.getText();
smonth = String.valueOf(jComboBox1.getSelectedIndex() + 1);
if (smonth.length() == 1)
smonth = "0"+smonth;
if (syear == "" || Integer.parseInt(syear)<1582)//若未輸入年份就觸發例外(1582年以前曾經改曆過,結果會不準確)
{
int[] now = new int[3];
now = getdate();
syear = String.valueOf(now[0]);//若選擇年份小於1582年則預設為當年
jLabel6.setText("請選1582以上");
}
jLabel5.setText(syear+" 年 "+smonth+" 月");
date_btn_create(Integer.parseInt(syear),Integer.parseInt(smonth));
jLabel7.setText("");
jLabel8.setText("目前無選擇日期");
}catch(NumberFormatException e)//例外處理設定為當年及選擇的月份
{
int[] now = new int[3];
now = getdate();
syear = String.valueOf(now[0]);
smonth = String.valueOf(jComboBox1.getSelectedIndex() + 1);
if (smonth.length() == 1)
smonth = "0"+smonth;
jLabel5.setText(syear+" 年 "+smonth+" 月");
jLabel6.setText("請選1582以上");
date_btn_create(Integer.parseInt(syear),Integer.parseInt(smonth));
jLabel7.setText("");
jLabel8.setText("目前無選擇日期");
}
}//查詢按鈕按下觸發事件結束
private void btnActionPerformed(ActionEvent evt)//日期按鈕按下觸發事件開始
{
jTextArea1.setText("");
String year,month,btn_date,filename,read_str;
year = jLabel5.getText().substring(0,4);//取得年
month = jLabel5.getText().substring(7,9);//取得月
btn_date = evt.getActionCommand();//取得按下按鈕文字(日)
filename = year+month+btn_date;
jLabel7.setText(btn_date);
try
{
FileReader fr = new FileReader(filename+".txt");//讀取選擇日期記事檔案
BufferedReader bfr = new BufferedReader(fr);//將檔案讀到緩衝區
boolean flag=false;//旗標
while((read_str = bfr.readLine())!=null) // 每次讀取一行,直到檔案結束
{
if (flag)//從第二行開始每一行第一個位置加入斷行
jTextArea1.append("\n");
jTextArea1.append(read_str);//加入該行訊息
flag=true;
}
jLabel6.setText("当天记事");
jLabel8.setText("已选择"+year+"年"+month+"月"+btn_date+"日");
fr.close();
}catch(FileNotFoundException e)//如果沒有指定的記事檔案就印出當日無行事曆(例外處理)
{
jLabel6.setText("当日无行事历");
jLabel8.setText("已选择"+year+"年"+month+"月"+btn_date+"日");
}catch(IOException e)//例外處理
{
e.printStackTrace();
}
}//日期按鈕按下觸發事件結束
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -