(可以选择上下月,年份月份分别选择,然后进行显示)
源代码:
package Thread;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
public class PermanentCalendar1 extends JFrame {
private JPanel contentPane;
private Calendar calendar=new GregorianCalendar();
private JTable table;
private JLabel currentLabel;
private JComboBox yearComboBox;
private JLabel yearLabel;
private JComboBox monthComboBox;
private JLabel monthLabel;
private String[] yearArr=new String[60];
private String[] monthArr=new String[12];
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Throwable e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
PermanentCalendar1 frame = new PermanentCalendar1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public PermanentCalendar1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel=new JPanel();
panel.setLayout(new GridLayout(1,3,5,5));
contentPane.add(panel,BorderLayout.NORTH);
JPanel panel_1=new JPanel();
panel.add(panel_1);
JButton lastButton=new JButton("上个月");
lastButton.setFont(new Font("微软雅黑", Font.PLAIN, 17));
panel_1.add(lastButton);
lastButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
do_lastButton_actionListener(e);
}
});
/*currentLabel=new JLabel();
panel.add(currentLabel);
currentLabel.setHorizontalAlignment(SwingConstants.CENTER );
*/
//年月的选择和显示
JPanel centerPanel=new JPanel();
panel.add(centerPanel);
centerPanel.setLayout(new BorderLayout(0,0));
currentLabel=new JLabel();
currentLabel.setHorizontalAlignment(SwingConstants.CENTER);
currentLabel.setVerticalAlignment(SwingConstants.CENTER);
centerPanel.add(currentLabel,BorderLayout.SOUTH);
for(int i=0;i<monthArr.length;i++){
if(i<9){
monthArr[i]=""+0+(i+1);
}else{
monthArr[i]=""+(i+1);
}
}
for(int i=0;i<yearArr.length;i++){
yearArr[i]=""+(1970+i);
}
JPanel comboBoxPanel=new JPanel();
centerPanel.add(comboBoxPanel,BorderLayout.CENTER);
comboBoxPanel.setLayout(new GridLayout(2,1,5,5) );
JPanel yearPanel=new JPanel();
comboBoxPanel.add(yearPanel);
yearPanel.setLayout(new BorderLayout(0,0));
yearComboBox=new JComboBox();
yearComboBox.setModel(new DefaultComboBoxModel(yearArr));
yearPanel.add(yearComboBox,BorderLayout.CENTER);
yearComboBox.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
yearItemChanged(e);
}
});
yearLabel=new JLabel("年");
yearLabel.setFont(new Font("微软雅黑", Font.PLAIN, 15));
yearLabel.setHorizontalAlignment(SwingConstants.CENTER);
yearPanel.add(yearLabel,BorderLayout.EAST);
JPanel monthPanel=new JPanel();
monthPanel.setLayout(new BorderLayout(0,0));
comboBoxPanel.add(monthPanel);
monthComboBox=new JComboBox();
monthComboBox.setModel(new DefaultComboBoxModel(monthArr));
monthComboBox.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
monthItemChanged(e);
}
});
monthPanel.add(monthComboBox,BorderLayout.CENTER);
monthLabel=new JLabel("月");
monthLabel.setFont(new Font("微软雅黑", Font.PLAIN, 15));
monthLabel.setHorizontalAlignment(SwingConstants.CENTER);
monthPanel.add(monthLabel,BorderLayout.EAST);
//年月的选择
JPanel panel_2=new JPanel();
panel.add(panel_2);
JButton nextButton=new JButton("下个月");
nextButton.setFont(new Font("微软雅黑", Font.PLAIN, 17));
panel_2.add(nextButton);
nextButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
do_nextButton_actionListener(e);
}
});
JPanel panel_3=new JPanel();
contentPane.add(panel_3,BorderLayout.CENTER);
panel_3.setLayout(new BoxLayout(panel_3,BoxLayout.X_AXIS));
JScrollPane scrollPane=new JScrollPane();
panel_3.add(scrollPane);
table=new JTable();
scrollPane.setViewportView(table);
table.setCellSelectionEnabled(true);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setRowHeight(25);
currentLabel.setText(updateLabel(0));
centerComboBox(calendar);
updateTable(calendar);
}
private String updateLabel(int increment){
calendar.add(Calendar.MONTH,increment);
SimpleDateFormat formatter=new SimpleDateFormat("yyyy年 MM月");
return formatter.format(calendar.getTime());
}
private String updateLabel1(int increment){
calendar.add(Calendar.YEAR,increment);
SimpleDateFormat formatter=new SimpleDateFormat("yyyy年 MM月");
return formatter.format(calendar.getTime());
}
private void updateTable(Calendar calendar){
String[] weeks=new DateFormatSymbols().getShortWeekdays();
String[] realWeeks=new String[7];
for(int i=1;i<weeks.length;i++){
realWeeks[i-1]=weeks[i].substring(2,3);
}
int today=calendar.get(Calendar.DATE);
int monthDays=calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH,1);
int weekday=calendar.get(Calendar.DAY_OF_WEEK);
int firstDay=calendar.getFirstDayOfWeek();
int whiteDay=weekday-firstDay;
Object[][] days=new Object[6][7];
for(int i=1;i<=monthDays;i++){
days[(i-1+whiteDay)/7][(i-1+whiteDay)%7]=i;
}
DefaultTableModel model=(DefaultTableModel)table.getModel();
model.setDataVector(days,realWeeks);
table.setModel(model);
table.setRowSelectionInterval(0, (today-1+whiteDay)/7);
table.setColumnSelectionInterval(0, (today-1+whiteDay)%7);
}
private void centerComboBox(Calendar calendar){
int year=calendar.get(Calendar.YEAR);
int month=calendar.get(Calendar.MONTH);
monthComboBox.setSelectedIndex(month);
for(int i=0;i<yearArr.length;i++){
String getYear=(String) yearComboBox. getItemAt(i);
String yearString=""+year;
while((yearString).equals(getYear)){
yearComboBox.setSelectedIndex(i);
return;
}
/*
if((yearString).equals(getYear)){
yearComboBox.setSelectedIndex(i);
return;
}else{
continue;
}
*/
}
}
protected void do_lastButton_actionListener(ActionEvent e){
currentLabel.setText(updateLabel(-1));
updateTable(calendar);
}
protected void do_nextButton_actionListener(ActionEvent e){
currentLabel.setText(updateLabel(1));
centerComboBox(calendar);
updateTable(calendar);
}
protected void monthItemChanged(ItemEvent e){
//获取月份
String getMonth=(String)monthComboBox.getSelectedItem();
int month=Integer.parseInt(getMonth);
int currentMonth=calendar.get(Calendar.MONTH)+1;
int increment=month-currentMonth;
currentLabel.setText(updateLabel(increment));
updateTable(calendar);
}
protected void yearItemChanged(ItemEvent e){
//Calendar calendar1=new GregorianCalendar();
//获取年份
String getYear=(String)yearComboBox.getSelectedItem();
int year=Integer.parseInt(getYear);
int currentYear=calendar.get(Calendar.YEAR);
int increment=year-currentYear;
currentLabel.setText(updateLabel1(increment));
updateTable(calendar);
}
}
结果:
似乎显示农历有点复杂,以后有时间再去添加吧!