所以我在java中编写了这段代码。当我按下鼠标按钮时它应该打印“鼠标点击”,但它什么也没做。
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class mainPanel extends JPanel implements MouseListener {
List<Integer> listLoc;
List<ImageIcon> listImg;
ImageIcon objPpc;
ImageIcon ttlSpace;
public mainPanel(){
loadImg();
fillBck(ttlSpace);
addTtl(objPpc, 1);
repaint();
}
public void fillBck(ImageIcon fillingImg){
for(int i = 0; i < 16; i++){
addTtl(fillingImg, i);
}
}
public void loadImg(){
objPpc = new ImageIcon ("img/obj/ppcBuild1.png");
ttlSpace = new ImageIcon ("img/ttl/spaceBuild1.png");
listLoc = new ArrayList<Integer>();
listImg = new ArrayList<ImageIcon>();
}
public void addTtl(ImageIcon addedImg, int addedLoc){
System.out.println("----- addTtl -----");
System.out.println(addedImg);
listImg.add(addedImg);
System.out.println(addedLoc);
listLoc.add(addedLoc);
System.out.println("----- /addTtl -----");
System.out.println("");
}
public void paint (Graphics g){
super.paint(g);
for (int i = 0; i < listImg.size(); i ++) {
g.drawImage((listImg.get(i)).getImage(), (listLoc.get(i) * (this.getWidth() / 16)), 0, (this.getWidth() / 16) , this.getHeight(), null);
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("Mouse Click");
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
java控制台只打印这个,没有错误代码:
----- addTtl -----
img/ttl/spaceBuild1.png
0
----- /addTtl -----
----- addTtl -----
img/ttl/spaceBuild1.png
1
----- /addTtl -----
----- addTtl -----
img/ttl/spaceBuild1.png
2
----- /addTtl -----
----- addTtl -----
img/ttl/spaceBuild1.png
3
----- /addTtl -----
----- addTtl -----
img/ttl/spaceBuild1.png
4
----- /addTtl -----
----- addTtl -----
img/ttl/spaceBuild1.png
5
----- /addTtl -----
----- addTtl -----
img/ttl/spaceBuild1.png
6
----- /addTtl -----
----- addTtl -----
img/ttl/spaceBuild1.png
7
----- /addTtl -----
----- addTtl -----
img/ttl/spaceBuild1.png
8
----- /addTtl -----
----- addTtl -----
img/ttl/spaceBuild1.png
9
----- /addTtl -----
----- addTtl -----
img/ttl/spaceBuild1.png
10
----- /addTtl -----
----- addTtl -----
img/ttl/spaceBuild1.png
11
----- /addTtl -----
----- addTtl -----
img/ttl/spaceBuild1.png
12
----- /addTtl -----
----- addTtl -----
img/ttl/spaceBuild1.png
13
----- /addTtl -----
----- addTtl -----
img/ttl/spaceBuild1.png
14
----- /addTtl -----
----- addTtl -----
img/ttl/spaceBuild1.png
15
----- /addTtl -----
----- addTtl -----
img/obj/ppcBuild1.png
1
----- /addTtl -----
当我在屏幕上按任意鼠标按钮(均匀滚轮[MOUSE3])时,它什么都不做。
我该如何解决?
答案 0 :(得分:6)
您的代码永远不会将任何MouseListener添加到面板中。你应该把这条线放在某个地方:
this.addMouseListener(this);
当您实现MouseListener时,这意味着您能够来侦听鼠标事件。要实际监听鼠标事件,必须将侦听器添加(注册)到某个产生鼠标事件的组件。
答案 1 :(得分:3)