1. 程式人生 > >自己用java寫的一個swing版的萬年曆

自己用java寫的一個swing版的萬年曆

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.LinkedList;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;


public class Test12Swing extends JFrame {
static String str;
static LinkedList link = new LinkedList();
private JButton left;
private JLabel title;
private JLabel[] date = new JLabel[49];
static Calendar calendar = Calendar.getInstance();
private JButton right;
{
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
Test12Swing.kalendar(calendar);


}


public Test12Swing() {
init();
listener();
}


private void listener() {
left.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
leftChange(calendar);
}


});
right.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
rightChange(calendar);
}


});
}


private void rightChange(Calendar calendar) {
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
Test12Swing.kalendar(calendar);
title.setText(str);
for (int i = 0; i < 49; i++) {
date[i].setText(" ");
}
for (int i = 0; i < link.size(); i++) {
date[i].setText((String) link.get(i));
}


}


private void leftChange(Calendar calendar) {
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
Test12Swing.kalendar(calendar);
title.setText(str);
for (int i = 0; i < 49; i++) {
date[i].setText("");
}
for (int i = 0; i < link.size(); i++) {
date[i].setText((String) link.get(i));
}


}


private void init() {
this.setTitle("日曆");
// 設定介面大小
this.setSize(800, 600);
// 居中
this.setLocationRelativeTo(null);
// 設定面板中的元件
this.setContentPane(createContentPane());
}


private Container createContentPane() {
// 新建面板
JPanel panel = new JPanel();
// 設定此容器的佈局管理器(流水,網格,邊框),此處為邊框
panel.setLayout(new BorderLayout());
panel.add(createNorthPane(), BorderLayout.NORTH);
panel.add(createCenterPane(), BorderLayout.CENTER);
// 設定邊框間距,逆時針
// panel.setBorder(new EmptyBorder(10, 10, 10, 10));
return panel;
}


private Component createNorthPane() {
// 新建面板
JPanel panel = new JPanel();
left = new JButton("<");
right = new JButton(">");
title = new JLabel(str, JLabel.CENTER);
// 設定此容器的佈局管理器(流水,網格,邊框),此處為邊框
panel.setLayout(new BorderLayout());
panel.add(left, BorderLayout.WEST);
panel.add(title, BorderLayout.CENTER);
panel.add(right, BorderLayout.EAST);
// 設定邊框間距,逆時針
panel.setBorder(new EmptyBorder(10, 10, 10, 10));
return panel;
}


private Component createCenterPane() {
JPanel panel = new JPanel();
// GridLayout() 網格佈局
panel.setLayout(new GridLayout(7, 7));
for (int i = 0; i < 49; i++) {
date[i] = new JLabel("", JLabel.CENTER);
}
for (int i = 0; i < link.size(); i++) {
date[i].setText((String) link.get(i));
panel.add(date[i]);
}
for (int i = link.size(); i < 49; i++) {
panel.add(date[i]);
}
return panel;
}


private static void kalendar(Calendar calendar) {
link.clear();
String[] month = { "01", "02", "03", "04", "05", "06", "07", "08",
"09", "10", "11", "12" };
str = "* * * * * * * * * *  " + calendar.get(Calendar.YEAR) + "年"
+ month[calendar.get(Calendar.MONTH)]
+ "月  * * * * * * * * * *";
link.add("日");
link.add("一");
link.add("二");
link.add("三");
link.add("四");
link.add("五");
link.add("六");
calendar.set(Calendar.DAY_OF_MONTH, 1);
for (int i = 7; i < 7 - 1 + calendar.get(Calendar.DAY_OF_WEEK); i++) {
link.add(" ");
}
int count = 1;
for (int i = 7 - 1 + calendar.get(Calendar.DAY_OF_WEEK); i < 7 - 1
+ calendar.get(Calendar.DAY_OF_WEEK)
+ calendar.getActualMaximum(Calendar.DAY_OF_MONTH); i++) {
if (count < 10) {
link.add("" + count);
} else {
link.add(count + "");
}
count++;


}
}


public static void main(String[] args) {
Test12Swing test = new Test12Swing();
test.setVisible(true);
// 設定預設關閉選項
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}
}