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

📄 newjframe.java

📁 java万年历 一个简单实用的java万年历程序.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*Codeing By Jockey Wu
  程式撰寫  吳建樂*/
import java.io.*;//引入相關物件
import java.util.*;
import java.lang.Thread;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDesktopPane;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.SoftBevelBorder;
 
public class NewJFrame extends javax.swing.JFrame //建立一視窗物件
{
  private JDesktopPane jDesktopPane1;//相關物件宣告
  private JDesktopPane jDesktopPane2;
  private JButton jButton1;
  private JButton jButton2;
  private JLabel jLabel2;
  private JLabel jLabel4;
  private JLabel jLabel3;
  private JLabel jLabel5;
  private JLabel jLabel6;
  private JLabel jLabel7;
  private JLabel jLabel8;
  private JComboBox jComboBox1;
  private JTextField jTextField1;
  private JButton jButton3;
  private JTextArea jTextArea1;
  private JLabel jLabel1;
 
  public static void main(String[] args) throws IOException//主程式開始
  {
    NewJFrame inst = new NewJFrame();
    inst.setVisible(true);
  }//主程式結束
  
  public NewJFrame()//建立視窗開始
  {
    super();
    initGUI();//呼叫GUI函數
  }//建立視窗結束
  
  public int[] getdate()//取得系統日期函數開始
  {
    int[] date_array = new int[3];
    Calendar ca = new GregorianCalendar();  
    date_array[0] = ca.get(Calendar.YEAR);//年
    date_array[1] = ca.get(Calendar.MONTH)+1;//月
    date_array[2] = ca.get(Calendar.DAY_OF_MONTH);//日
    return date_array;//回傳自訂日期陣列
  }//取得系統日期函數結束
 
  public void new_btn()//重新產生日期按鈕函數開始
  {
    jTextArea1.setText("");//清空記事
    int year,month;
    year = Integer.parseInt(jLabel5.getText().substring(0,4));//設定為已選擇的年
    month = Integer.parseInt(jLabel5.getText().substring(7,9));//設定為已選擇的月
    date_btn_create(year,month);//呼叫產生日期按鈕函數
  }//重新產生日期按鈕函數結束
  
  public void date_btn_create(int year,int month)//產生日期按鈕
  {
    int y=0,x=0,x_add=0,y_add=0,week_add=0,date_acc=0,week_of_day=0;
    String syear,smonth,sday,filename;
    syear = String.valueOf(year);
    smonth = String.valueOf(month);
    if (smonth.length() == 1)//若小於10月(一位數)就在前面加0
      smonth = "0"+smonth;
    
    jDesktopPane1.remove(jDesktopPane2);//移除桌面2(日期按鈕附著,也就是把日期按鈕移除)
    jDesktopPane2 = new JDesktopPane();//產生一個新的桌面
    jDesktopPane1.add(jDesktopPane2);
    jDesktopPane2.setBounds(0, 30, 252, 196);//設定大小及位置
    jDesktopPane2.setBackground(new java.awt.Color(148,205,176));//設定顏色
      
    switch(month)//設定月份天數
    {
      case 1://大月31天
      case 3:
      case 5:
      case 7:
      case 8:
      case 10:
      case 12:
        date_acc = 31;
        break;
        
      case 4://小月30天
      case 6:
      case 9:
      case 11:
        date_acc = 30;
        break;
        
      case 2:
        if (leap_year(year))
          date_acc = 29;
        else
          date_acc = 28;
    }
    
    week_of_day = dow(year,month,1);//呼叫星期函數(取得當月第一天為星期幾)
    
    switch(week_of_day)//設定當月1日位置(像素)
    {
      case 0:
        week_add = 0;
        break;
      case 1:
        week_add = 34;
        break;
      case 2:
        week_add = 68;
        break;
      case 3:
        week_add = 102;
        break;
      case 4:
        week_add = 136;
        break;
      case 5:
        week_add = 170;
        break;
      case 6:
        week_add = 204;
        break;
    }
    JButton btn[] = new JButton[date_acc];//建立日期按鈕陣列
    for (int i=0;i<date_acc;i++)//建立日期按鈕陣列回圈
    {
      btn[i] = new JButton();//建立對應日期按鈕
      jDesktopPane2.add(btn[i]);//加到桌面2上
      btn[i].setText(String.valueOf(i+1));//設定按鈕文字為日期
      if ((i-week_of_day>0 && (i+week_of_day)%7==0) || ((i+week_of_day)%7==0 && i != 0))
      {//設定當月第一天日期按鈕位置
        x=0;//X軸座標
        x_add=0;//下一個按鈕座標(X軸)加值
        y++;//Y軸座標
        y_add+=10;//換行座標(Y軸)加值
        week_add=0;//當月第一日按鈕座標加值
      }//下面設定按鈕大小及加值(X為起始10+第幾個按鈕*橫向寬度25+間隔+當月第一天星期幾加值)
      btn[i].setBounds(10+x*25+x_add+week_add, y*20+y_add, 25, 20);//(Y為第幾個按鈕*高度20+換行加值)按鈕寬為25高為20
      btn[i].setFont(new java.awt.Font("Arial",1,12));//設定字體大小及樣式
      btn[i].setBorder(BorderFactory.createTitledBorder(""));//設定按鈕文字不自動調整大小
      int[] now = new int[3];
      now = getdate();//取得當天日期
      if (year == now[0] && month == now[1] && i+1 == now[2])
        btn[i].setBackground(new java.awt.Color(233,238,164));//若為當天則設定按鈕為黃色
      else
        btn[i].setBackground(new java.awt.Color(148,205,176));//若不是當天則設定按鈕為綠色
        
      sday = String.valueOf(i+1);
      filename = syear+smonth+sday;//記事檔案名稱(年+月+日.txt)
      File file=new File(filename+".txt");//建立檔案物件
      if (file.exists())//若當天有記事檔案則設定按鈕字體顏色為藍色
        btn[i].setForeground(new java.awt.Color(0,0,255));
      
      btn[i].addActionListener(new ActionListener() {//設定按鈕監聽(觸發事件函數)
        public void actionPerformed(ActionEvent evt) {
          btnActionPerformed(evt);
        }
      });
      x++;//下一個按鈕X座標加權
      x_add+=9;//下一個按鈕間隔X座標加權
    }
  }
  
  public boolean leap_year(int year)//判斷閏年函數開始
  {
    boolean leap_year;//4的倍數,若為100的倍數則必須為4的倍數才是閏年
    if (year%4 == 0)
    {
      if (year%100 == 0)
      {
        if (year%400 == 0)
          leap_year = true;
        else
          leap_year = false;
      }
      else
        leap_year = true;
    }
    else
      leap_year = false;
    return leap_year;
  }//判斷閏年函數結束
  
  public int dow(int y,int m,int d)//判斷星期幾
  {
    int[] ww={6, 2, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};//天文星體運行值
    int w;
    w=ww[m-1]+y+(y/4)-(y/100)+(y/400);//閏年設定
    if( ((y%4)==0) && (m<3) )//公式
    {
      w--;
      if((y%100)==0) w++;
      if((y%400)==0) w--;
    }
    return (w+d)%7;//回傳星期幾(0為星期日,1為星期一以此類推)
  }
  
  private void initGUI()//產生視覺化物件函數(Graph User Interface,圖形化使用者介面)
  {
    try
    {
      setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);//設定外框視窗主要功能列為標準(縮到最小,放到最大,關閉)
      {
        this.setTitle("万年行事历");//設定視窗抬頭 
        jDesktopPane1 = new JDesktopPane();//建立一桌面
        getContentPane().add(jDesktopPane1, BorderLayout.CENTER);
        jDesktopPane1.setPreferredSize(new java.awt.Dimension(464, 267));//設定桌面大小
        jDesktopPane1.setBackground(new java.awt.Color(148,205,176));//設定桌面顏色
        {//建立桌面2開始(放日期按鈕的桌面)
          jDesktopPane2 = new JDesktopPane();
          jDesktopPane1.add(jDesktopPane2);
          jDesktopPane2.setBounds(0, 30, 252, 196);
          jDesktopPane2.setBackground(new java.awt.Color(148,205,176));
        }//建立桌面2結束
        {//建立星期日標籤開始
          jLabel1 = new JLabel();
          jDesktopPane1.add(jLabel1);
          jLabel1.setText("Sun");
          jLabel1.setBounds(14, 7, 21, 21);
          jLabel1.setFont(new java.awt.Font("Arial",0,11));
          jLabel1.setForeground(new java.awt.Color(255,0,0));
        }//建立星期日標籤結束
        {//建立記事框開始
          jTextArea1 = new JTextArea(252, 35);
          jDesktopPane1.add(jTextArea1);
          jTextArea1.setText("");//預設內容清空
          jTextArea1.setBounds(252, 42, 175, 182);//設定大小
          jTextArea1.setFont(new java.awt.Font("新細明體",0,11));//設定字體樣式大小
          jTextArea1.setLineWrap(true);//文字過長自動換行
          jTextArea1.setWrapStyleWord(true);//文字過長自動換行
          JScrollPane scrollPane = new JScrollPane(jTextArea1, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
          jDesktopPane1.add(scrollPane);//上面為建立scorollpane(視窗旁邊的移動棒);將記事框建立在scrollpane上
          scrollPane.setBounds(252, 42, 175, 182);//設定大小及位置
        }//建立記事框結束

⌨️ 快捷键说明

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