用JTable 實現日曆

效果圖:java

 

 

 主要思想:日曆最核心的功能就是能顯示某年某月對應的日期和星期幾。所以只要實現傳入具體的年份和月份,獲得一組存放了日期的數組a[ ]便可。其中數組的大小設置成42,要考慮的問題是當月的第一天對應星期幾。日期數組中的前七個,確定包含了當月的第一天,把這一天找到,將「1」填入,後面的日期依次累加直到加完該月最後一天爲止。數組

 

 

MyCalendar類:獲得用於顯示日期數組a[ ]ide

import java.util.Calendar;

public class MyCalendar {
String day[];
int year = 2020,month=0;

public String[] getDay() {
return day;
}
public void setDay(String[] day) {
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
private boolean isLeapYear() {
if(this.year%4==0 && this.year%100!=0){
return true;
}else 
if(this.year%400==0){
return true;
}else

return false;
}


//得到顯示數組
public String[] getCalendar(){
Calendar calendar=Calendar.getInstance();
String a[]=new String[42];
calendar.set(year,month-1,1);
int weekday=calendar.get(Calendar.DAY_OF_WEEK)-1;
int day=0;
int days = 31;
if (this.month == 4 || this.month == 6 || this.month == 9 || this.month == 11)
days = 30;
if (this.month == 2 && isLeapYear())
days = 29;
if (this.month == 2 && !isLeapYear())
days = 28;

for(int i = weekday,n=1;i< weekday +days;i++){
a[i]=String.valueOf(n);
n++;
}
return a;
}

}

 

 

MyFrame類:創造顯示面板,主要用到JTable。this

public class MyFrame extends JFrame implements ActionListener {
//存儲數據
MyCalendar calendar = new MyCalendar();
JComboBox choiceYear,choiceMonth;
JTable table = null;
JPanel root = new JPanel();
JLabel lyear, lmonth;

private Object[] name = {"日","一","二","三","四","五","六"};
private TableModel tableModel = new DefaultTableModel(name,6);

//    private static int row = 6;
//    private static int column = 7;

public MyFrame(String title) {
super(title);


this.setContentPane(root);
root.setLayout(new BorderLayout());
//年月選擇欄
choiceYear=new JComboBox();
choiceMonth=new JComboBox();
lyear=new JLabel("年");
lmonth=new JLabel("月 ");
for(int i=1990;i<2050;i++)
choiceYear.addItem(i);
choiceYear.addActionListener(this);
for(int i=1;i<=12;i++)
choiceMonth.addItem(i);
choiceMonth.addActionListener(this);

JPanel pNorth=new JPanel();

pNorth.add(choiceYear);
pNorth.add(lyear);
pNorth.add(choiceMonth);
pNorth.add(lmonth);
root.add(pNorth,BorderLayout.NORTH);

// 表格初始化
setYearAndMonth( 1990, 1);



}

//設置年月日
public void setYearAndMonth(int y,int m){
calendar.setYear(y);
calendar.setMonth(m);
String day[]=calendar.getCalendar();
Vector<Object> rowData = new Vector<>();
int row = 0;
int column = 0;
for(int i = 0; i< 42; i++) {
row = i / 7;
column = i % 7;

tableModel.setValueAt(day[i], row, column);

}


// 建立 JTable,直接重寫 isCellEditable(),設爲不可編輯
table = new JTable(tableModel){
@Override
public boolean isCellEditable(int row, int column)
{
return false;
}    
};
JScrollPane scrollPane = new JScrollPane(table);
root.add(scrollPane, BorderLayout.CENTER);

// 添加到主界面    
table.setFillsViewportHeight(true);    
table.setRowSelectionAllowed(true); // 整行選擇
table.setRowHeight(30);    
}
 

public void actionPerformed(ActionEvent e){

//選擇年份
if (e.getSource()==choiceYear){
calendar.setYear((Integer) choiceYear.getSelectedItem());
String day[]=calendar.getCalendar();
Vector<Object> rowData = new Vector<>();
int row = 0;
int column = 0;
for(int i = 0; i< 42; i++) {
row = i / 7;
column = i % 7;

tableModel.setValueAt(day[i], row, column);

}
table = new JTable(tableModel){
@Override
public boolean isCellEditable(int row, int column)
{
return false;
}    
};
JScrollPane scrollPane = new JScrollPane(table);
root.add(scrollPane, BorderLayout.CENTER);

// 添加到主界面    
table.setFillsViewportHeight(true);    
table.setRowSelectionAllowed(true); // 整行選擇
table.setRowHeight(30);    
}
//選擇月份
else if (e.getSource()==choiceMonth){
calendar.setMonth((Integer) choiceMonth.getSelectedItem());
String day[]=calendar.getCalendar();
Vector<Object> rowData = new Vector<>();
int row = 0;
int column = 0;
for(int i = 0; i< 42; i++) {
row = i / 7;
column = i % 7;

tableModel.setValueAt(day[i], row, column);

}
}
table = new JTable(tableModel){
@Override
public boolean isCellEditable(int row, int column)
{
return false;
}    
};
JScrollPane scrollPane = new JScrollPane(table);
root.add(scrollPane, BorderLayout.CENTER);

// 添加到主界面    
table.setFillsViewportHeight(true);    
table.setRowSelectionAllowed(true); // 整行選擇
table.setRowHeight(30);    

}


}

 

 

ShowView類:用於顯示窗口,照抄便可,無需理解。spa

import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ShowView
{
    private static void createGUI()
    {
        
        // 語法:由於MyFrame是JFrame的子類,因此能夠這麼寫
        JFrame frame = new MyFrame("日曆");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        
        // 設置窗口的其餘參數,如窗口大小
        frame.setSize(400, 300);
        
        // 顯示窗口
        frame.setVisible(true);
        
        
    }
    
    public static void main(String[] args)
    {
        // 此段代碼間接地調用了 createGUI()
        
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run()
            {
                createGUI();
            }
        });

    }
}
相關文章
相關標籤/搜索