首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

!匿名类中的鼠标事件无响应?感谢了

2012-01-10 
求助!匿名类中的鼠标事件无响应?在线等!!!感谢了importjava.awt.*importjava.awt.event.*importjavax.sw

求助!匿名类中的鼠标事件无响应?在线等!!!感谢了
import   java.awt.*;
import   java.awt.event.*;
import   javax.swing.*;
import   java.util.*;
 
class   Calendarday   extends   JPanel   {
 
private   JPanel   dayPanel;
 
private   static   int   count   =   0;
 
private   JLabel[]   label;
 
GregorianCalendar   d   =   new   GregorianCalendar();
GregorianCalendar   day   =   new   GregorianCalendar();
 
public   Calendarday()   {
//   日期容器开始:

day.set(Calendar.DAY_OF_MONTH,   1);
int   weekday   =   day.get(Calendar.DAY_OF_WEEK);

dayPanel   =   new   JPanel();
//   根据每月的第一天来判断,决定日期表格容器的大小:
if   (weekday   ==   7   ||   weekday==6)   {
dayPanel.setLayout(new   GridLayout(6,   7));
label   =   new   JLabel[42];
for   (int   i   =   0;   i   <   label.length;   i++)   {
label[i]   =   new   JLabel( "       ");
dayPanel.add(label[i]);
}
}   else   {
dayPanel.setLayout(new   GridLayout(5,   7));
label   =   new   JLabel[35];
for   (int   i   =   0;   i   <   label.length;   i++)   {
label[i]   =   new   JLabel( "       ");
dayPanel.add(label[i]);
}
for(JLabel   l:label){
//鼠标效果
l.addMouseListener(new   MouseListener(){
public   void   mousePressed(MouseEvent   event){}
public   void   mouseClicked(MouseEvent   event){}
public   void   mouseExited(MouseEvent   event){}
public   void   mouseEntered(MouseEvent   event){}
public   void   mouseReleased(MouseEvent   event){}

});
l.addMouseMotionListener(new   MouseMotionListener(){
public   void   mouseMoved(MouseEvent   event){
setBackground(Color.BLUE);
}
public   void   mouseDragged(MouseEvent   event){

}
});
}

}
//   日期表格容器结束;
 
printcalendar();
}
 
 
//   输出日期表
private   void   printcalendar()   {
int   today   =   d.get(Calendar.DAY_OF_MONTH);
int   month   =   d.get(Calendar.MONTH);

 
d.set(Calendar.DAY_OF_MONTH,   1);
int   weekday   =   d.get(Calendar.DAY_OF_WEEK);

for   (int   i   =   Calendar.SUNDAY;   i   <   weekday;   i++)   {
label[count].setText( "       ");
count++;
}
System.out.println(today);
 
do   {
int   day   =   d.get(Calendar.DAY_OF_MONTH);
label[count].setText(Integer.toString(day));
if   (day   ==   today)   {
label[count].setText(label[count].getText()   +   "* ");
}   else   {
label[count].setText(label[count].getText()   +   "     ");
}

d.add(Calendar.DAY_OF_MONTH,   1);
weekday   =   d.get(Calendar.DAY_OF_WEEK);
count++;


}   while   (d.get(Calendar.MONTH)   ==   month);
count=0;
}

public   JPanel   getnewtpye(){
return   dayPanel;
}
}
 
 
//顶部星期的类
class   CalendarTop   extends   JPanel{
private   JPanel   weekPanel;

public   CalendarTop(){

//   星期容器
weekPanel   =   new   JPanel();
weekPanel.setLayout(new   GridLayout(1,   7));
 
addLabel( "星期日 ");
addLabel( "星期一 ");
addLabel( "星期二 ");
addLabel( "星期三 ");
addLabel( "星期四 ");
addLabel( "星期五 ");
addLabel( "星期六 ");
 
//add(weekPanel,   BorderLayout.NORTH);
//   星期容器结束;
}
//   添加星期到容器里;
private   void   addLabel(String   str)   {
JLabel   weeklabel   =   new   JLabel(str);
weekPanel.add(weeklabel);
}

public   JPanel   getnewtype(){
return   weekPanel;
}
}
 
 
//界面布局类
class   CalendarFrame   extends   JFrame   {
public   CalendarFrame()   {
setTitle( "日历 ");
Calendarday   dayPanel   =   new   Calendarday();
CalendarTop   topPanel=new   CalendarTop();

//布局管理器;
setLayout(new   BorderLayout());
JPanel   jptop=topPanel.getnewtype();
JPanel   jpday=dayPanel.getnewtpye();
add(jptop,BorderLayout.NORTH);
add(jpday,BorderLayout.CENTER);

pack();

}
}
 
public   class   NewCalendar   {
public   static   void   main(String[]   args)   {
CalendarFrame   frame   =   new   CalendarFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

 


我代码里面有注释鼠标效果的...因为考虑到每次鼠标移动或点击的时候只需要给一个监听器处理.这个监听器又只需要一个.所以我采用了匿名内部类.但是没有任何效果出现.  
PS:没试过不用匿名内部类,换普通的内部类..因为思路上这样是行得通的.  
求助!!!help!!!!  
自学了一个月,想做个小东西给自己,检验一下一个月以来到底学了多少东西和掌握得怎么样.一动手问题就出现了..头疼...  


[解决办法]
你只监听了,事件代码还未写全
[解决办法]
就在你的基础上改了一点, 不过感觉实现方法不好,就当抛砖引玉了
public void mouseMoved(MouseEvent event){
if (event.getSource() instanceof JLabel) {
((JLabel)(event.getSource())).setOpaque(true);
((JLabel)(event.getSource())).setBackground(Color.BLUE);
}
}

热点排行