嗨,我正在我的程序中执行鼠标侦听器操作,当用户单击面板时,我正在制作一个颜色 jpanel,它会更改其颜色,但是一行代码会给出一些错误,例如在您的代码中创建一个类 e,但 e 已经被声明为事件类请帮助我这里是我的代码下面。错误在这一行“ panel.addMouseListener(e); ”。
'import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class randomcolor extends JFrame{
JPanel panel;
public randomcolor (){
panel=new JPanel();
panel.setBackground(rancolor());
add(panel);
}
event e =new event();
panel.addMouseListener(e);
public Color rancolor(){
int r= (int) (Math.random()*256);
int g= (int) (Math.random()*256);
int b= (int) (Math.random()*256);
return (new Color(r,g,b));
}
public class event implements MouseListener{
public void mouseClicked(MouseEvent e) {
panel.setBackground(rancolor());
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
public static void main (String args[]){
randomcolor gui=new randomcolor();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
gui.setSize(300, 300);
gui.setTitle("color panel");
}
}