Ok, I am trying to use MouseListener for the first time, but I'm not having much luck. My program compiles fine but the MouseListener Events don't seem to do anything. 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.
Any help on how to get it to work would be great, thanks.
ps I'm a noob so, be nice.
Read the Swing tutorial on How to Write a MouseListener .
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.
You need to use .addMouseListener(this);
on each component you want to listen to.
eg
frame.addMouseListener(this)
, or if in a static method 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. 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.");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.