java中如何用匿名类实现事件处理?
发布网友
发布时间:2022-04-26 00:22
我来回答
共1个回答
热心网友
时间:2023-10-24 06:58
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Example6_11
{
JFrame win;
JTextField text;
public Example6_11()
{
win=new JFrame("匿名类作鼠标移动事件适配器");
text=new JTextField(30);
}
public void inFrame()
{
JLabel label=new JLabel("单击并拖曳鼠标");
win.add(label,BorderLayout.NORTH);
win.add(text,BorderLayout.SOUTH);
win.addMouseMotionListener(
new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
String s;
s="鼠标移动:x="+e.getX()+"Y="+e.getY();
text.setText(s);
}
}
);
win.setSize(300,200);
win.setVisible(true);
}
public static void main(String[] args)
{
Example6_11 w=new Example6_11();
w.inFrame();
}
}