java的事件监听
❶ 关于java事件监听的问题
我觉得你应该只写提交的监听就可以了,提交后循环全部的按钮看有答案的按钮是否被选中计算出对的提数。要是你每个按钮都要添加事件的话,很多选择题的时候你就烦了
❷ Java事件监听器的Java事件监听器
Java事件监听器即一组动作接口。如键盘事件KeyEvent对应的接口是:
public interface KeyListener extends EventListener {
public void keyPressed(KeyEvent ev);
public void keyReleased(KeyEvent ev);
public void keyTyped(KeyEvent ev);
}
这个监听器定义了3个动作:键盘按下去调用keyPressed方法,键盘抬起来调用keyReleased,键盘被敲击一次调用keyTyped。
事件监听器是需要注册才能使用的,注册方法如下:
public void add<ListenerType>(<ListenerType>listener);
事件监听器也可以注销,注销方法如下:
public void remove<ListenerType>(<ListenerType>listener);
❸ Java 事件监听
不是通过线程实现的,它是通过一种注册--通知机制实现的。在java的设计模式中,有一种模式叫:观察者模式,和这个类似。举个例子,本例子是一个简单的监听当数据发生变化时要做的操作。 1,我们先定义一个接口,可以让多个监听者实现 public int...
❹ JAVA的事件监听问题!
你要设计算法package ch10;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
public class Bounce {
public static void main(String[] args) {
JFrame frame=new BounceFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class BallRunnable implements Runnable
{
public BallRunnable(Ball aBall,Component aComponent)
{
ball=aBall;
component=aComponent;
}
public void run()
{
try
{
for(int i=1;i<STEPS;i++)
{
ball.move(component.getBounds());
component.repaint();
Thread.sleep(DELAY);
}
}
catch(InterruptedException e)
{
}
}
private Ball ball;
private Component component;
public static final int STEPS=1000;
public static int DELAY=3;
}
class Ball
{
public void move(Rectangle2D bounds)
{
x+=dx;
y+=dy;
if(x<bounds.getMinX())
{
x=bounds.getMinX();
dx=-dx;
}
if(x+xsize>=bounds.getMaxX())
{
x=bounds.getMaxX()-xsize;
dx=-dx;
}
if(y<bounds.getMinY())
{
y=bounds.getMinY();
dy=-dy;
}
if(y+ysize>=bounds.getMaxY())
{
y=bounds.getMinY()-ysize;
dy=-dy;
}
}
public Ellipse2D getShape()
{
return new Ellipse2D.Double(x,y,xsize,ysize);
}
private static final int xsize=15;
private static final int ysize=15;
private double x=0;
private double y=0;
private double dx=1;
private double dy=1;
}
class Ballpanel extends JPanel
{
public void add(Ball b)
{
balls.add(b);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
for(Ball b:balls)
{
g2.fill(b.getShape());
}
}
private ArrayList<Ball> balls =new ArrayList<Ball>();
}
class BounceFrame extends JFrame
{
public BounceFrame()
{
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
setTitle("Bounce");
panel=new Ballpanel();
add(panel,BorderLayout.CENTER);
JPanel buttonPanel=new JPanel();
addButton(buttonPanel,"Start",new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
addBall();
}
}
);
addButton(buttonPanel,"Close",new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
);
add(buttonPanel,BorderLayout.SOUTH);
}
public void addButton(Container c,String title ,ActionListener listener)
{
JButton button=new JButton(title);
c.add(button);
button.addActionListener(listener);
}
public void addBall()
{
Ball b=new Ball();
panel.add(b);
Runnable r=new BallRunnable(b,panel);
Thread t=new Thread(r);
t.start();
}
private Ballpanel panel;
public static final int DEFAULT_WIDTH=450;
public static final int DEFAULT_HEIGHT=350;
public static final int STEPS=1000;
public static final int DELAY=3;
}
参考下这段代码
❺ java中的事件监听是怎么回事
不是通过线程实现的,它是通过一种注册--通知机制实现的。在java的设计模式中,有一种模式叫:观察者模式,和这个类似。举个例子,本例子是一个简单的监听当数据发生变化时要做的操作。 1,我们先定义一个接口,可以让多个监听者实现 public interface IDataListen {public void update(Object event,Object msg);}2,实现一监听者 public class DataListen implements IDataListen{ @Override public void update(Object event, Object arg) { // TODO Auto-generated method stub System.out.println("数据发生了变化"); }}3,被监听者 public class DataManager{ private List<IDataListen> listenList = new ArrayList<>(); public void notifyListen(Object event,Object msg){ for(IDataListen dataListen : listenList){ dataListen.update(null, null); } } public void addListen(IDataListen dataListen){ listenList.add(dataListen); } public void updateData(Object msg){ this.notifyListen(null, msg); } public static void main(String[] args) { DataManager dataManager = new DataManager(); IDataListen dataListen1 = new DataListen(); ...