无法使用鼠标功能
Cannot get mouse functions to work
我正在尝试编写一个程序,通过在圆圈改变颜色时单击它来测量反应时间,但我无法使用任何鼠标功能。知道如何编写代码以便在鼠标单击时跟踪鼠标的坐标吗?这是我现在尝试编译程序时的错误。 MouseListener 是抽象的;无法实例化
MouseListener circle = new MouseListener();
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
public class Stopwatch
{
public static int windowX = 900;
public static int windowY = 700;
public static void main(String args[])
{
GfxApp gfx = new GfxApp();
gfx.setSize(900,700);
gfx.addWindowListener(new WindowAdapter() {public void
windowClosing(WindowEvent e) {System.exit(0);}});
gfx.show();
}
}
class GfxApp extends Frame
{
public void paint(Graphics g)
{
Timer.drawgreen(g);
Timer.pause();
Timer.drawred(g);
}
}
class Timer
{
public static int circlex = 300;
public static int circley = 200;
public static int radius = 250;
public static void drawgreen(Graphics g)
{
g.setColor(new Color(0,255,0));
g.fillOval(circlex, circley, radius, radius);
}
public static void drawred(Graphics g)
{
g.setColor(new Color(255,0,0));
g.fillOval(circlex, circley, radius, radius);
}
public static void pause()
{
Random rand = new Random();
int k = rand.nextInt(301);
for(double i = 0; i < 500 + k; i += .000001)
{
// delay
}
}
}
class Mouse
{
MouseListener circle = new MouseListener();
public void mouseClicked (MouseEvent e)
{
}
public void mouseEntered (MouseEvent e)
{
}
public void mouseExited (MouseEvent e)
{
}
public void mousePressed (MouseEvent e)
{
}
public void mouseReleased (MouseEvent e)
{
}
}
我认为您必须将鼠标侦听器实现到您的框架 class 并在那里使用它。
MouseListener 不是 class 接口,因此无法实例化。
这是 KeyListener 的例子
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class Window extends JFrame {
private static final long serialVersionUID = -8255319694373975038L;
public static boolean change = false;
KPanel kp = new KPanel();
KeyListen kl = new KeyListen();
Rect r[];
public Window() {
JFrame w = new JFrame("okno");
w.setDefaultCloseOperation(EXIT_ON_CLOSE);
w.setVisible(true);
w.setSize(500, 500);
w.setResizable(false);
w.getContentPane().add(kp);
w.addKeyListener(kl);
}
}
class KeyListen implements KeyListener {
@Override
public void keyPressed(KeyEvent evt) {
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
您只想跟踪一组坐标吗?如果是这样,那么下面的内容可能会有所帮助...
double xClick = 0;
double yClick = 0;
private MouseListener click = new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
//Get the Mouse Location and store it.
double xClick = e.getX(); //mouse's X location
double yClick = e.getY(); //mouse's Y location
}
};
然后您需要检测 xClick 和 yClick 变量以及它们是否与圆形相交。
if(ellipse.contains(e.getX(), e.getY())){
//Do Something in here...
}
如果您想跟踪所有鼠标点击位置,请使用双精度数组或 Point2D 数组。
希望这对您有所帮助。
同时使用 mouseReleased 或 mousePressed 进行点击检测。 MouseClicked 仅在用户在按下和释放之间没有移动时才有效。
我正在尝试编写一个程序,通过在圆圈改变颜色时单击它来测量反应时间,但我无法使用任何鼠标功能。知道如何编写代码以便在鼠标单击时跟踪鼠标的坐标吗?这是我现在尝试编译程序时的错误。 MouseListener 是抽象的;无法实例化 MouseListener circle = new MouseListener();
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
public class Stopwatch
{
public static int windowX = 900;
public static int windowY = 700;
public static void main(String args[])
{
GfxApp gfx = new GfxApp();
gfx.setSize(900,700);
gfx.addWindowListener(new WindowAdapter() {public void
windowClosing(WindowEvent e) {System.exit(0);}});
gfx.show();
}
}
class GfxApp extends Frame
{
public void paint(Graphics g)
{
Timer.drawgreen(g);
Timer.pause();
Timer.drawred(g);
}
}
class Timer
{
public static int circlex = 300;
public static int circley = 200;
public static int radius = 250;
public static void drawgreen(Graphics g)
{
g.setColor(new Color(0,255,0));
g.fillOval(circlex, circley, radius, radius);
}
public static void drawred(Graphics g)
{
g.setColor(new Color(255,0,0));
g.fillOval(circlex, circley, radius, radius);
}
public static void pause()
{
Random rand = new Random();
int k = rand.nextInt(301);
for(double i = 0; i < 500 + k; i += .000001)
{
// delay
}
}
}
class Mouse
{
MouseListener circle = new MouseListener();
public void mouseClicked (MouseEvent e)
{
}
public void mouseEntered (MouseEvent e)
{
}
public void mouseExited (MouseEvent e)
{
}
public void mousePressed (MouseEvent e)
{
}
public void mouseReleased (MouseEvent e)
{
}
}
我认为您必须将鼠标侦听器实现到您的框架 class 并在那里使用它。 MouseListener 不是 class 接口,因此无法实例化。
这是 KeyListener 的例子
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class Window extends JFrame {
private static final long serialVersionUID = -8255319694373975038L;
public static boolean change = false;
KPanel kp = new KPanel();
KeyListen kl = new KeyListen();
Rect r[];
public Window() {
JFrame w = new JFrame("okno");
w.setDefaultCloseOperation(EXIT_ON_CLOSE);
w.setVisible(true);
w.setSize(500, 500);
w.setResizable(false);
w.getContentPane().add(kp);
w.addKeyListener(kl);
}
}
class KeyListen implements KeyListener {
@Override
public void keyPressed(KeyEvent evt) {
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
您只想跟踪一组坐标吗?如果是这样,那么下面的内容可能会有所帮助...
double xClick = 0;
double yClick = 0;
private MouseListener click = new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
//Get the Mouse Location and store it.
double xClick = e.getX(); //mouse's X location
double yClick = e.getY(); //mouse's Y location
}
};
然后您需要检测 xClick 和 yClick 变量以及它们是否与圆形相交。
if(ellipse.contains(e.getX(), e.getY())){
//Do Something in here...
}
如果您想跟踪所有鼠标点击位置,请使用双精度数组或 Point2D 数组。
希望这对您有所帮助。
同时使用 mouseReleased 或 mousePressed 进行点击检测。 MouseClicked 仅在用户在按下和释放之间没有移动时才有效。