当前位置:编程学习 > JAVA >>

Java中JTable的更新。万年历

我用java写了一个万年历。。。一个弹出对话框的形式,在界面内我加了几个按钮,我想要的结果是按一次按钮可以切换月份,切换了之后可以显示这个月的月历,但是我现在按了按钮之后,表中显示的内容不能更新。还有怎样设置表格单元不能编辑。 JAVA jtable 更新页面 设置表格单元不可编辑 --------------------编程问答-------------------- import java.util.Calendar;  

public class CalendarBean { // 日期类  
      
    int year ,month , nextDay; 
    Calendar calendar = Calendar.getInstance();
      
    public void setYear(int year){  
        this.year = year;  
    }  
      
    public void setMonth(int month){  
        this.month = month;  
    }  
      
    // 根据年月,在二维表格中按真实顺序排位  
    public String[][] getCalendar(){  
        String a[][] = new String[6][7];  
        Calendar 日历 = Calendar.getInstance();  
        日历.set(year,month-1,1);  
          
        int 星期几 = 日历.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=星期几; 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; // 返回二维当前年月数组  
    }  
}  
--------------------编程问答-------------------- import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.plaf.basic.BasicButtonListener;

public class SelectetimeTest extends JDialog implements ActionListener {
/**
 * 
 */
private static final long serialVersionUID = 1L;
private JPanel p;
private JScrollPane sp;
private JTextField tfyear, tfmonth;
private JLabel lbyear, lbmonth;
private JTable tbtime;
private JButton button1, button2, button3;
private Object cells[][];
private String[] cocumnNames = {"日", "一", "二", "三", "四", "五", "六"};

private JRadioButton timequery, linequery;
private JTextField from, arrivel, leavetime;
private  int inputMonth = 0, inputYear = 0;  
    private String strmonth;
private Calendar calendar;
private ListSelectionModel selectionMode;
private int counter = 0;

public SelectetimeTest(JFrame f) {
//初始化信息
super( f, "选择日期", true);
p = new JPanel();
CalendarBean calendarbean = new CalendarBean();
calendar = Calendar.getInstance();
inputYear = calendar.get(Calendar.YEAR);
inputMonth = calendar.get(Calendar.MONTH) + 1;
calendarbean.setYear(inputYear);  
calendarbean.setMonth(inputMonth);  
        cells = calendarbean.getCalendar();  
button1 = new JButton("<<");
button2 = new JButton("今天");
button3 = new JButton(">>");
tfyear = new JTextField(4); 
tfyear.setText(inputYear + "");
tfmonth = new JTextField(2); 
tfmonth.setText(inputMonth + "");
lbyear = new JLabel("年");
lbmonth = new JLabel("月");
tbtime = new JTable(cells, cocumnNames);
sp = new JScrollPane(tbtime);
tbtime.setCellSelectionEnabled(true);
tbtime.isCellEditable(7, 5);
//设置组件坐标及大小
button1.addActionListener(this);

button2.addActionListener(this);

button3.addActionListener(this);

//添加信息到面板
p.add(button1);
p.add(button2);
p.add(button3);
p.add(tfyear);
p.add(lbyear);
p.add(tfmonth);
p.add(lbmonth);
//设置窗体
this.add(p, BorderLayout.NORTH);
this.add(sp);
this.setSize(300, 200);
this.setLocation(800, 350);
this.setResizable(false);
}

public void actionPerformed(ActionEvent e) {
Object source =  e.getSource();
//如果信号源在第一个选择键上
if (source == button3) {
counter++;
inputMonth = (calendar.get(Calendar.MONTH) + 1) + counter;
if (inputMonth > 12) {
if (inputMonth % 12 == 0) {
inputYear = calendar.get(Calendar.YEAR) + ((inputMonth / 12) - 1);
inputMonth = 12;
} else {
inputYear = calendar.get(Calendar.YEAR) + (inputMonth / 12);
inputMonth = inputMonth % 12;
}
}
CalendarBean calendarbean = new CalendarBean();
calendarbean.setYear(inputYear);  
calendarbean.setMonth(inputMonth);  
cells = calendarbean.getCalendar();  
tfyear.setText(inputYear + "");
tfmonth.setText(inputMonth + "");
tbtime.revalidate();

}
//如果信号源在第二个选择键上
/*if (source == selecte2) {
AddLine add = new AddLine();
add.setVisible(true);
this.setVisible(false);
}*/
//如果信号源在第三个选择键上
if (source == button2) {
inputYear = calendar.get(Calendar.YEAR);
inputMonth = calendar.get(Calendar.MONTH) + 1;
CalendarBean calendarbean = new CalendarBean();
calendarbean.setYear(inputYear);  
calendarbean.setMonth(inputMonth);  
cells = calendarbean.getCalendar();  
tfyear.setText(inputYear + "");
tfmonth.setText(inputMonth + "");
tbtime = new JTable(cells, cocumnNames);
}
//如果信号源在更改信息上
if (source == button1) {
counter--;
inputMonth = (calendar.get(Calendar.MONTH) + 1) + counter;
if (0 < inputMonth && inputMonth < 12) {
inputYear = calendar.get(Calendar.YEAR);
inputMonth = inputMonth % 12;

} else if (inputMonth == 0){
inputYear = calendar.get(Calendar.YEAR) - 1;
inputMonth = 12;
} else {
if (inputMonth % 12 == 0) {
inputYear = calendar.get(Calendar.YEAR) - (-(inputMonth / 12) + 1);
inputMonth = 12;
} else {
inputYear = calendar.get(Calendar.YEAR) - (-(inputMonth / 12) + 1);
inputMonth =12 - (-(inputMonth % 12));
}

}
CalendarBean calendarbean = new CalendarBean();
calendarbean.setYear(inputYear);  
calendarbean.setMonth(inputMonth);  
cells = calendarbean.getCalendar();  
tfyear.setText(inputYear + "");
tfmonth.setText(inputMonth + "");
tbtime = new JTable(cells, cocumnNames);
}

//如果信号源在退出登录上
}
public static void main(String [] args) {
JFrame f1 = null;
SelectetimeTest selectetime = new SelectetimeTest(f1);
selectetime.setVisible(true);



}



上面是我的代码,麻烦各位给看下。。。老师要让交作业了。。。
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,