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

希望高手帮小弟我解答下(注释里的有关问题)

2012-01-16 
希望高手帮我解答下(注释里的问题)importjavax.swing.*importjava.awt.event.*importjava.awt.event.Key

希望高手帮我解答下(注释里的问题)
import   javax.swing.*;
import   java.awt.event.*;
import   java.awt.event.KeyListener;
import   java.util.EventListener;
public   class   chap4_4   implements   KeyListener{

public   JButton   buttonadd;
public   JFrame   main_frame;
JPanel   main_panel;
public   JTextField   textfield;
JLabel   label;
public   JComboBox   combobox;
MyMouseListener   mymouselistener;
public   chap4_4()
{
main_frame=new   JFrame( "chap4_4 ");
main_panel=new   JPanel();
main_frame.add(main_panel);
label=new   JLabel( "输入 ");
textfield=new   JTextField(10);
combobox=new   JComboBox();
buttonadd=new   JButton( "添加 ");

mymouselistener=new   MyMouseListener(this);//和下面有我注释的有什么关联
buttonadd.addMouseListener(mymouselistener);
combobox.addItemListener(new   MyItemListener(this));
main_panel.add(label);
main_panel.add(textfield);
main_panel.add(combobox);
main_panel.add(buttonadd);
main_frame.setSize(300,200);
main_frame.setVisible(true);

}
public   static   void   main(String[]   args)   {



chap4_4   a=new   chap4_4();

}


public   void   keyTyped(KeyEvent   e)   {

}


public   void   keyPressed(KeyEvent   e)   {

}
public   void   keyReleased(KeyEvent   e)   {
}
}

class   MyMouseListener   implements   MouseListener
{
chap4_4   obj;
public   MyMouseListener(chap4_4   obj)
{
this.obj=obj;//请问这里直接都参数传个了对象有什么作用和下面程序代码有什么关联吗(除了mymouselistener=new   MyMouseListener(this);这样的有关联外)
}
public   void   mouseClicked(MouseEvent   e)  
{
String   a=new   String();
a=obj.textfield.getText();//这里的obj是不是类C的对象
obj.combobox.addItem(a);//这里的obj是不是类C的对象
}
public   void   mouseEntered(MouseEvent   e)   //这里的(MouseEvent   e)参数是什么,是不是可以省略,或者与上面MouseListener申请有关系呢
{
JOptionPane.showMessageDialog(null, "鼠标进入 ", " ",1);
}
public   void   mouseExited(MouseEvent   e)
{
JOptionPane.showMessageDialog(null, "鼠标移出 ", " ",1);
}
public   void   mousePressed(MouseEvent   e)  
{
}
public   void   mouseReleased(MouseEvent   e)  
{
}
}
class   MyItemListener   implements   ItemListener
{
chap4_4   obj;
public   MyItemListener(chap4_4   obj)
{
this.obj=obj;
}
public   void   itemStateChanged(ItemEvent   e)  
{
String   a=new   String();
a=(String   )obj.combobox.getSelectedItem();
obj.textfield.setText(a);
}
}

问的问题有点肤浅   希望高手详细解答,非常感谢!!!!!!^_^



[解决办法]
这主要是 awt中的事件模型,也是observer模式,每一个组件都有一个相应的监听器,如果监听到事件则进行相应的处理。像下面的语句就是给button添加一个鼠标单击事件。
mymouselistener=new MyMouseListener(this);
buttonadd.addMouseListener(mymouselistener);
---------------
public MyMouseListener(chap4_4 obj)
{
this.obj=obj;//请问这里直接都参数传个了对象有什么作用和下面程序代码有什么关联吗(除了mymouselistener=new MyMouseListener(this);这样的有关联外)
}

这里主要是通过MyMouseListener的构造函数把chap4_4对象传入进来,这样在类的其他方法中就可以使用chap4_4中的数据和方法了。


---------------
public void mouseEntered(MouseEvent e) //这里的(MouseEvent e)参数是什么,是不是可以省略?
mouseEntered(MouseEvent e)是MouseListener接口中的方法,既然继承的是接口,那么必须要完整的实现这个方法,MouseEvent e 代表当前的鼠标事件。
---------------
public void mouseClicked(MouseEvent e)
{
String a=new String();
a=obj.textfield.getText();//这里的obj是不是类C的对象
obj.combobox.addItem(a);//这里的obj是不是类C的对象
}

obj是类chap4_4的对象


热点排行