|
Posted on 2008-08-11 08:26 梦与桥 阅读(1737) 评论(0) 编辑 收藏 所属分类: java基础
1、JTable类生成的对象以该TableModel为参数,并负责将TableModel对象中的数据以表格的形式显示出业。
2、程序演示:
//程序来源:机械工业出版社《Java面向对象编程》葛志春等编著
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Calendar;
import java.util.Date;
/** *//***日历类,提供日历功能*/
public class TableDemo extends JPanel implements
ActionListener,MouseMotionListener,MouseListener{
final private String TITLES[]={"星期日","星期一","星期二","星期三","星期四",
"星期五","星期六"};
final private int ROW_HEIGHT=20;
final private int ROW_WIDTH=40;
private JTable tbMain;//表格
private JLabel lblTitle;//标题
private JButton btnNextMonth;//下个月的按钮
private JButton btnPrevMonth;//上个月的按钮
private JPanel pnlTop;//顶部容器
private int year;//当前显示的年份
private int month;//当前显示的月份
private int day;//当前选择的日期
private int week;//当前选择的星期
private int selectedX=-1;
private int selectedY=-1;
private int clickX=-1;
private int clickY=-1;
public TableDemo(){
Calendar cal=Calendar.getInstance();
cal.setTime(new Date());
year=cal.get(Calendar.YEAR);
month=cal.get(Calendar.MONTH)+1;
day=cal.get(Calendar.DAY_OF_MONTH)-1;
week=cal.get(Calendar.DAY_OF_WEEK)-1;
pnlTop=new JPanel(new FlowLayout(FlowLayout.CENTER));
tbMain=new JTable(7,7);
tbMain.setPreferredSize(new Dimension(ROW_WIDTH*7,ROW_HEIGHT*7));
tbMain.setRowHeight(ROW_HEIGHT);
for(int i=0;i<TITLES.length;i++)
tbMain.setValueAt(TITLES[i], 0, i);
tbMain.addMouseMotionListener(this);
tbMain.addMouseListener(this);
tbMain.setEnabled(false);
tbMain.setCellSelectionEnabled(true);
lblTitle=new JLabel();
btnNextMonth=new JButton(">|");
btnNextMonth.addActionListener(this);
btnPrevMonth=new JButton("|<");
btnPrevMonth.addActionListener(this);
pnlTop.add(btnPrevMonth);
pnlTop.add(lblTitle);
pnlTop.add(btnNextMonth);
setTheTitle();
//布局组件
setLayout(new BorderLayout());
add(pnlTop,BorderLayout.NORTH);
add(tbMain,BorderLayout.CENTER);
showMonth();//设置当前月份
}
//取得指定年份和月份的日期数
public int getDaysofMonth(int year,int month){
int Days[]={31,28,31,30,31,30,31,31,30,31,30,31};
//判断是否闰年
if(year%400==0||(year%4==0&&year%100!=0))
Days[1]=29;
return Days[month-1];
}
//取得指定月份的头一天是星期几
public int getWeekOfFirstDay(int year,int month){
Calendar cal=Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month-1);
cal.set(Calendar.DAY_OF_MONTH, 1);
int result=cal.get(Calendar.DAY_OF_WEEK)-1;
return result;
}
//设置显示的标题
private void setTheTitle(){
String m=String.valueOf(month);
if(m.length()==1)m="0"+m;
lblTitle.setText(String.valueOf(year)+"年"+m+"月");
}
//显示月份
private void showMonth(){
for(int x=0;x<7;x++)
for(int y=1;y<7;y++)tbMain.setValueAt(" ",y, x);
int y=1;
int x=getWeekOfFirstDay(year,month);
int maxDay=getDaysofMonth(year,month);
for(int i=0;i<maxDay;i++){
tbMain.setValueAt(new Integer(i+1),y,x);
x++;
if(x>6){
x=0;
y++;
}
}
repaint();
}
//按钮事件处理
public void actionPerformed(ActionEvent e){
if(e.getSource().equals(btnNextMonth)){
month++;
if(month>12){
month=1;
year++;
}
}
if(e.getSource().equals(btnPrevMonth)){
month--;
if(month<1){
month=12;
year--;
}
}
setTheTitle();
showMonth();
}
//绘制
public void paint(Graphics g){
super.paint(g);
//repaint Table First LINE
g.fillRect(0,pnlTop.getHeight(),getWidth()-1,ROW_HEIGHT-1);
g.setColor(Color.white);
for(int i=0;i<7;i++)
g.drawString((String)(tbMain.getModel().getValueAt(0,i)),
i*ROW_WIDTH,pnlTop.getHeight()+ROW_HEIGHT-3);
Calendar cal=Calendar.getInstance();
cal.set(Calendar.YEAR,year);
cal.set(Calendar.MONTH,month);
cal.set(Calendar.DAY_OF_WEEK_IN_MONTH,day);
int line=cal.get(Calendar.DAY_OF_WEEK_IN_MONTH)+1;
int x=week*tbMain.getWidth()/7;
int y=pnlTop.getHeight()+ROW_HEIGHT*line;
g.setColor(Color.black);
g.drawRect(x+1,y+1, ROW_WIDTH-4,ROW_HEIGHT-4);
if(selectedX>0&&selectedY>=0&&!
tbMain.getModel().getValueAt(clickY,clickX).equals(" ")){
g.setColor(Color.green);
g.drawRect(clickX*ROW_WIDTH+1,(clickY+2)*ROW_HEIGHT-1,
ROW_WIDTH-4,ROW_HEIGHT-4);
}
}
public void mouseDragged(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mouseClicked(MouseEvent e){
if (e.getX() > 0 && e.getY() < getWidth() && e.getY() > ROW_HEIGHT
&& e.getY() < ROW_HEIGHT * 7) {
selectedX = e.getX() / ROW_WIDTH;
selectedY = e.getY() / ROW_HEIGHT;
repaint();
}
}
public void mousePressed(MouseEvent e){
if(e.getX()>0&&e.getY()<getWidth()&&
e.getY()>ROW_HEIGHT&&e.getY()<ROW_HEIGHT*7){
clickX=e.getX()/ROW_WIDTH;
clickY=e.getY()/ROW_HEIGHT;
repaint();
}
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public static void main(String[] args) {
JFrame frm=new JFrame();
frm.getContentPane().setLayout(new BorderLayout());
frm.getContentPane().add(new TableDemo());
frm.setTitle("表格演示");
frm.pack();
frm.setVisible(true);
}
}
|