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(); ...