今天做java c/s程序的时候要用到一个日期选择的控件,NetBeans里面没有,所以自己写了。因为让单独的表格单元被选择不知道用哪个方法,,,所以,有知道的希望可以帮忙完善下
import java.util.Calendar;
public class CalendarBean {
int year = 2014, month = 5, nextDay;
public void setYear(int year) {
this.year = year;
}
public int getYear() {
return year;
}
public void setMonth(int month) {
this.month = month;
}
public int getMonth() {
return month;
}
public String[][] getCalendar() {
String a[][] = new String[6][7];
Calendar rili = Calendar.getInstance();
rili.set(year, month - 1, 1);
int weekday = rili.get(Calendar.DAY_OF_WEEK) - 1;
int day = 0;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
day = 31;
}
if (month == 4 || month == 6 || month == 9 || month == 11) {
day = 30;
}
if (month == 2) {
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
day = 29;
} else {
day = 28;
}
}
nextDay = 1;
for (int k = 0; k < 6; k++) {
if (k == 0) {
for (int j = weekday; j < 7; j++) {
a[k][j] = "" + nextDay;
nextDay++;
}
} else {
for (int j = 0; j < 7 && nextDay <= day; j++) {
a[k][j] = "" + nextDay;
nextDay++;
}
}
}
return a;
}
}
上面是一个日历形成的class
import java.awt.event.*;
import java.util.Calendar;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
*
* @author Administrator
*/
public class ChooseDateFrame extends JDialog implements ActionListener {
JPanel p;
JScrollPane jsp;
JLabel YearLabel, MonthLabel;
JComboBox MonthComboBox;
JButton OkButton;
JTable DateTable;
JSpinner YearSpinner;
Calendar calendar;
CalendarBean calendarBean1;
ListSelectionModel selectionMode;
int year, month;
String rili[][];
String day = "";
String name[] = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
String all = "";
JTextField tf;
String date;
public ChooseDateFrame(JFrame f, JTextField tf) {
p = new JPanel();
this.tf = tf;
p.setLayout(null);
YearLabel = new JLabel("YEAR:");
YearLabel.setBounds(0, 0, 45, 25);
MonthLabel = new JLabel("MONTH:");
MonthLabel.setBounds(120, 0, 55, 25);
MonthComboBox = new JComboBox();
YearSpinner = new JSpinner();
Calendar calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
YearSpinner.setValue(year);
YearSpinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
// 加逻辑代码
year = (int) YearSpinner.getValue();
month = MonthComboBox.getSelectedIndex() + 1;
calendarBean1.setYear(year);
calendarBean1.setMonth(month);
rili = calendarBean1.getCalendar();
System.out.println(year + "|" + month);
p.remove(jsp);
DateTable = new JTable(rili, name); // 使用数组rili和name创建table
DateTable = new JTable(rili, name);
DateTable.setColumnSelectionAllowed(true);
DateTable.setCursor(new java.awt.Cursor(
java.awt.Cursor.DEFAULT_CURSOR));
DateTable.getTableHeader().setReorderingAllowed(false);
jsp = new JScrollPane(DateTable);
p.add(jsp);
jsp.setBounds(0, 30, 290, 125);
}
});
YearSpinner.setBounds(45, 0, 65, 25);
for (int i = 1; i < 13; i++) {
MonthComboBox.addItem(new Integer(i));
}
MonthComboBox.setSelectedIndex(calendar.get(Calendar.MONTH));
MonthComboBox.addActionListener(this);
MonthComboBox.setBounds(170, 0, 50, 25);
calendarBean1 = new CalendarBean();
calendarBean1.setYear(year);
calendarBean1.setMonth(month + 1);
rili = calendarBean1.getCalendar();
DateTable = new JTable(rili, name);
DateTable.setColumnSelectionAllowed(true);
DateTable.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
DateTable.getTableHeader().setReorderingAllowed(false);
jsp = new JScrollPane(DateTable);
jsp.setBounds(0, 30, 290, 125);
OkButton = new JButton("确定");
OkButton.setBounds(220, 0, 60, 25);
OkButton.addActionListener(this);
p.add(YearLabel);
p.add(MonthLabel);
p.add(MonthComboBox);
p.add(YearSpinner);
p.add(OkButton);
p.add(jsp);
this.add(p);
this.setSize(310, 200);
setResizable(false);
setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == OkButton) {
int year = (int) YearSpinner.getValue();
int month = MonthComboBox.getSelectedIndex() + 1;
int rows[] = DateTable.getSelectedRows();
int columns[] = DateTable.getSelectedColumns();
if (rows.length > 1 || columns.length > 1) {
JOptionPane.showMessageDialog(this, "不能选择多个日期!", "错误",
JOptionPane.INFORMATION_MESSAGE);
} else {
for (int i = 0; i < rows.length; i++) {
for (int j = 0; j < columns.length; j++) {
day = day
+ (String) DateTable.getValueAt(rows[i],
columns[j]);
}
}
}
if (day.equals("")) {
JOptionPane.showMessageDialog(this, "您还没选择日期!", "错误",
JOptionPane.INFORMATION_MESSAGE);
} else {
date = YearSpinner.getValue() + "-"
+ (MonthComboBox.getSelectedIndex() + 1) + "-" + day;
day = "";
getText(tf);
this.setVisible(false);
}
}
if (e.getSource() == MonthComboBox) {
year = (int) YearSpinner.getValue();
month = MonthComboBox.getSelectedIndex() + 1;
calendarBean1.setYear(year);
calendarBean1.setMonth(month);
rili = calendarBean1.getCalendar();
System.out.println(year + "|" + month);
p.remove(jsp);
DateTable = new JTable(rili, name); // 使用数组rili和name创建table
DateTable.setColumnSelectionAllowed(true);
DateTable.setCursor(new java.awt.Cursor(
java.awt.Cursor.DEFAULT_CURSOR));
DateTable.getTableHeader().setReorderingAllowed(false);
jsp = new JScrollPane(DateTable);
p.add(jsp);
jsp.setBounds(0, 30, 290, 125);
}
}
public void getText(JTextField tf) {
System.out.println(date);
tf.setText(date);
}
}
上面是在窗口上写一个按钮,通过按钮实现将指定文本框的值设为“YYYY-MM-DD”日期格式。其中传入的参数分别是JFrame f(按钮所在窗口), JTextField tf(要生成日期的文本框)
import java.awt.event.*;
import java.util.Calendar;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
*
* @author Administrator
*/
public class ChooseDateDialog extends JDialog implements ActionListener {
JPanel p;
JScrollPane jsp;
JLabel YearLabel, MonthLabel;
JComboBox MonthComboBox;
JButton OkButton;
JTable DateTable;
JSpinner YearSpinner;
Calendar calendar;
CalendarBean calendarBean1;
int year, month;
String rili[][];
String day = "";
String name[] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
String all = "";
JTextField tf;
String date;
public ChooseDateDialog(JDialog f, JTextField tf) {
p = new JPanel();
this.tf = tf;
p.setLayout(null);
YearLabel = new JLabel("YEAR:");
YearLabel.setBounds(0, 0, 45, 25);
MonthLabel = new JLabel("MONTH:");
MonthLabel.setBounds(120, 0, 55, 25);
MonthComboBox = new JComboBox();
YearSpinner = new JSpinner();
Calendar calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
YearSpinner.setValue(year);
YearSpinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
//加逻辑代码
year = (int) YearSpinner.getValue();
month = MonthComboBox.getSelectedIndex() + 1;
calendarBean1.setYear(year);
calendarBean1.setMonth(month);
rili = calendarBean1.getCalendar();
System.out.println(year + "|" + month);
p.remove(jsp);
DateTable = new JTable(rili, name); //使用数组rili和name创建table
DateTable = new JTable(rili, name);
DateTable.setColumnSelectionAllowed(true);
DateTable.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
DateTable.getTableHeader().setReorderingAllowed(false);
jsp = new JScrollPane(DateTable);
p.add(jsp);
jsp.setBounds(0, 30, 290, 125);
}
});
YearSpinner.setBounds(45, 0, 65, 25);
for (int i = 1; i < 13; i++) {
MonthComboBox.addItem(new Integer(i));
}
MonthComboBox.setSelectedIndex(calendar.get(Calendar.MONTH));
MonthComboBox.setBounds(170, 0, 45, 25);
int year1 = calendar.get(Calendar.YEAR);
int month1 = calendar.get(Calendar.MONTH);
calendarBean1 = new CalendarBean();
calendarBean1.setYear(year1);
calendarBean1.setMonth(month1 + 1);
String rili[][];
String name[] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
rili = calendarBean1.getCalendar();
DateTable = new JTable(rili, name);
DateTable.setColumnSelectionAllowed(true);
DateTable.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
DateTable.getTableHeader().setReorderingAllowed(false);
jsp = new JScrollPane(DateTable);
jsp.setBounds(0, 30, 290, 125);
OkButton = new JButton("确定");
OkButton.setBounds(220, 0, 60, 25);
OkButton.addActionListener(this);
p.add(YearLabel);
p.add(MonthLabel);
p.add(MonthComboBox);
p.add(YearSpinner);
p.add(OkButton);
p.add(jsp);
this.add(p);
this.setSize(310, 200);
setResizable(false);
setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == OkButton) {
int year = (int) YearSpinner.getValue();
int month = MonthComboBox.getSelectedIndex() + 1;
int rows[] = DateTable.getSelectedRows();
int columns[] = DateTable.getSelectedColumns();
if (rows.length > 1 || columns.length > 1) {
JOptionPane.showMessageDialog(this, "不能选择多个日期!", "错误", JOptionPane.INFORMATION_MESSAGE);
} else {
for (int i = 0; i < rows.length; i++) {
for (int j = 0; j < columns.length; j++) {
day = day + (String) DateTable.getValueAt(rows[i], columns[j]);
}
}
}
if (day.equals("")) {
JOptionPane.showMessageDialog(this, "您还没选择日期!", "错误", JOptionPane.INFORMATION_MESSAGE);
} else {
date = YearSpinner.getValue() + "-" + (MonthComboBox.getSelectedIndex() + 1) + "-" + day;
day = "";
getText(tf);
this.setVisible(false);
}
}
if (e.getSource() == MonthComboBox) {
int year = (int) YearSpinner.getValue();
int month = MonthComboBox.getSelectedIndex() + 1;
calendarBean1.setYear(year);
calendarBean1.setMonth(month);
rili = calendarBean1.getCalendar();
System.out.println(year + "|" + month);
p.remove(jsp);
DateTable = new JTable(rili, name); //使用数组rili和name创建table
DateTable.setColumnSelectionAllowed(true);
DateTable.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
DateTable.getTableHeader().setReorderingAllowed(false);
jsp = new JScrollPane(DateTable);
p.add(jsp);
jsp.setBounds(0, 30, 290, 125);
}
}
public void getText(JTextField tf) {
System.out.println(date);
tf.setText(date);
}
}
这个是在对话框中的按钮上使用的,,,功能与上面个一样,通过点击按钮,将指定文本框的值设为“YYYY-MM-DD”格式的时间,其中传入的参数分别是JDialog f(按钮所在对话框), JTextField tf(要生成日期的文本框)