好的,我第一次尝试使用 MouseListener,但我运气不佳。我的程序编译得很好,但 MouseListener 事件似乎没有做任何事情。这是我的代码:
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();
}
}
不做任何事情是指系统不会将文本输出到命令行或更改 JLabel。
任何关于如何让它工作的帮助都会很棒,谢谢。
ps 我是菜鸟,所以请客气。