我试着用JavaSwing开发一个日历应用程序,我有两个JComboBox来选择年份和月份,还有一个jbutton。当按下jbutton时,我的日历应该出现并显示所选年份的所选月份。当选择其他年份和月份时,按下按钮,我的日历应该显示新选择的月份。但我的日历只有在第一次点击按钮时才起作用,之后我的日历数据不会改变。下面是calendartable类的代码和一个简单的示例。有人能帮我找到问题吗?
日历表类:
import java.util.Calendar;
import java.util.Date;
import javax.swing.table.AbstractTableModel;
public class Calendartable extends AbstractTableModel{
public static String[] COLUMN_NAMES = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday","Sunday"};
private int tablerows = 0;
private Date startOfWeek;
private Date startDate;
private Date endDate;
public Calendartable(Date start, Date end) {
this.startDate=start;
this.endDate=end;
Calendar cal = Calendar.getInstance();
cal.setTime(startDate);
int i=0;
while (cal.getTime().before(endDate)) {
cal.add(Calendar.DATE, 7);
tablerows++;
}
tablerows++;
if (tablerows > 0) {
cal.setTime(startDate);
while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
cal.add(Calendar.DATE, -1);
}
startOfWeek = cal.getTime();
}
}
@Override
public int getRowCount() {
return tablerows;
}
@Override
public int getColumnCount() {
return 7;
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return Date.class;
}
@Override
public String getColumnName(int column) {
return COLUMN_NAMES[column];
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Date date = null;
if (startOfWeek != null) {
Calendar cal = Calendar.getInstance();
cal.setTime(startOfWeek);
int day = (rowIndex * 7) + columnIndex;
cal.add(Calendar.DATE, day);
if (cal.getTime().before(startDate) || cal.getTime().after(endDate)) {
date = null;
} else {
date = cal.getTime();
}
}
return date;
}
}
再举个例子:
import java.awt.CardLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
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 java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calendar2 {
JFrame frame = new JFrame("Calendar");
JPanel panelCont = new JPanel();
JPanel panelFirst = new JPanel(new GridBagLayout());
JTable table;
JButton showbuttonmonth=new JButton("Show");
JComboBox jcbyear;
JComboBox<String> jcbmonth;
CardLayout cl = new CardLayout();
Object month[]=new Object[12];{
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";
}
Object year[]=new Object[30];{
for(int i=2010;i<=2039;i++)
year[i-2010]=i;
}
public Calendar2() {
panelCont.setLayout(cl);
GridBagConstraints gbc=new GridBagConstraints();
gbc.insets=new Insets(5,5,5,5);
jcbyear=new JComboBox(year);
gbc.insets=new Insets(15,0,0,5);
gbc.gridx=0;
gbc.gridy=2;
panelFirst.add(new JLabel("Year:"),gbc);
gbc.insets=new Insets(0,0,5,5);
gbc.gridx=0;
gbc.gridy=3;
panelFirst.add(jcbyear, gbc);
gbc.insets=new Insets(15,0,0,5);
gbc.gridx=1;
gbc.gridy=2;
panelFirst.add(new JLabel("Month:"),gbc);
jcbmonth=new JComboBox(month);
gbc.insets=new Insets(0,0,5,5);
gbc.gridx=1;
gbc.gridy=3;
panelFirst.add(jcbmonth, gbc);
gbc.gridx=2;
gbc.gridy=3;
panelFirst.add(showbuttonmonth, gbc);
gbc.gridx=1;
gbc.gridy=4;
SimpleDateFormat sdf = new SimpleDateFormat("MM.dd.yyyy");
Date startDate = null;
try {
startDate = sdf.parse("1.1.2010");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Date endDate = null;
try {
endDate = sdf.parse("1.31.2010");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
table = new JTable(new Calendartable(startDate, endDate));
panelFirst.add(new JScrollPane(table), gbc);
panelCont.add(panelFirst, "1");
cl.show(panelCont, "1");
showbuttonmonth.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
int year = Integer.parseInt(String.valueOf(jcbyear.getSelectedItem()));
int month=jcbmonth.getSelectedIndex()+1;
String start=month+".1."+year;
String end=month+".31."+year;
gbc.gridx=1;
gbc.gridy=4;
SimpleDateFormat sdf = new SimpleDateFormat("MM.dd.yyyy");
Date startDate = null;
try {
startDate = sdf.parse(start);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Date endDate = null;
try {
endDate = sdf.parse(end);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
table = new JTable(new Calendartable(startDate, endDate));
panelFirst.add(new JScrollPane(table), gbc);
}
});
}
public static void main(String[] args) {
Calendar2 cal=new Calendar2();
}
}
暂无答案!
目前还没有任何答案,快来回答吧!