简体   繁体   English

无法使MouseListener工作

[英]Can't get MouseListener to work

Ok, I am trying to use MouseListener for the first time, but I'm not having much luck. 好的,我是第一次尝试使用MouseListener,但是我运气不好。 My program compiles fine but the MouseListener Events don't seem to do anything. 我的程序编译正常,但MouseListener事件似乎没有做任何事情。 Here is my code: 这是我的代码:

import java.awt.color.*;
import java.awt.font.*;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class yo implements MouseListener {

Image image;
JFrame frame = new JFrame();
JLabel heloo = new JLabel("yo");
JPanel panel = new JPanel()
{
    @Override
    public void paintComponent(Graphics g)
    {
        //super.paintComponent(g);
        //ImageIcon i = new ImageIcon("hi.jpg");
        //image = i.getImage();
        //g.drawImage(image,150,150,null);
        //g.drawString("Hello",100,100);
        //g.drawString("Hi",50,50);
    }
};


public yo()
{
    frame.add(panel);
    frame.setTitle("Hello");
    frame.setSize(500,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
    panel.add(heloo);
}

public void mouseClicked (MouseEvent Event)
{
    heloo.setText("Hi");
    System.out.println("Hi");
}
public void mouseEntered (MouseEvent Event)
{System.out.println("Hi");}
public void mouseExited (MouseEvent Event)
{}
public void mousePressed (MouseEvent Event)
{}
public void mouseReleased (MouseEvent Event)
{}

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

By not doing anything I mean that the system doesn't output text to command line or change the JLabel. 通过不做任何事情我的意思是系统不输出文本到命令行或更改JLabel。

Any help on how to get it to work would be great, thanks. 任何关于如何让它工作的帮助都会很棒,谢谢。

ps I'm a noob so, be nice. ps我是个菜鸟,很好。

Read the Swing tutorial on How to Write a MouseListener . 阅读关于如何编写MouseListener的Swing教程。

You didn't add the listener to any component. 您没有将侦听器添加到任何组件。

put

frame.addMouseListener(this);

in the constructor 在构造函数中

You made a yo a MouseListener , but you didn't add it to anything. 你做了yo一个MouseListener ,但你没有将它添加到任何东西。

You need to use .addMouseListener(this); 你需要使用.addMouseListener(this); on each component you want to listen to. 在您要收听的每个组件上。

eg 例如

frame.addMouseListener(this) , or if in a static method frame.addMouseListener(myInstanceOfYo); frame.addMouseListener(this) ,或者如果是静态方法frame.addMouseListener(myInstanceOfYo);

try 尝试

public yo()
{
    frame.add(panel);
    frame.setTitle("Hello");
    frame.setSize(500,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
    panel.add(heloo);


    frame.addMouseListener(this);


}

Edit: 编辑:

I would also suggest you change your test text in each MouseListener method to be unique, so it's easier to see which was called, and when. 我还建议您将每个MouseListener方法中的测试文本更改为唯一,这样可以更容易地查看调用的内容以及何时调用。 Also, make the parameter name start with a lower case letter (Event becomes event), it's just good practice. 另外,让参数名称以小写字母开头(事件成为事件),这只是一个好习惯。

ie

public void mouseClicked (MouseEvent event)
{
    heloo.setText("Hi");
    System.out.println("Clicked.");
}
public void mouseEntered (MouseEvent event)
{
    System.out.println("Entered.");
}
public void mouseExited (MouseEvent event)
{
    System.out.println("Exited.");
}
public void mousePressed (MouseEvent event)
{
    System.out.println("Pressed.");
}
public void mouseReleased (MouseEvent event)
{
    System.out.println("Released.");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM