我按网上的鼠标监听器的内部类的方法写的结构,但是不知道为什么用鼠标之后没有结果,麻烦各位大神帮我看看
新人第一次用这个提问,关于提问的方式或者表述不清楚也欢迎大家指正啊
谢谢大家!!!
buttonListener是Map1的内部类
import javax.swing.*;
public class test
{
public static void main(String[] args){
JFrame frame=new Map1();
frame.setVisible(true);
new Map1();
}
}
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.GridLayout;
import java.awt.*;
import java.awt.event.*;
public class Map1 extends JFrame
{
ImageIcon bank1 = new ImageIcon("bank1.jpg");//为按钮加载图片
ImageIcon bank2 = new ImageIcon("bank2.jpg");
ImageIcon plank1 = new ImageIcon("plank1.jpg");
ImageIcon plank1_man = new ImageIcon("plank1_man.jpg");
ImageIcon plank2 = new ImageIcon("plank2.jpg");
ImageIcon plank2_man = new ImageIcon("plank2_man.jpg");
ImageIcon stump1 = new ImageIcon("stump1.jpg");
ImageIcon stump1_man = new ImageIcon("stump1_man.jpg");
ImageIcon stump2 = new ImageIcon("stump2.jpg");
ImageIcon stump2_man = new ImageIcon("stump2_man.jpg");
ImageIcon stump3 = new ImageIcon("stump3.jpg");
ImageIcon stump3_man = new ImageIcon("stump3_man.jpg");
ImageIcon water1 = new ImageIcon("water1.jpg");
ImageIcon water2 = new ImageIcon("water2.jpg");
ImageIcon water3 = new ImageIcon("water3.jpg");
ImageIcon water4 = new ImageIcon("water4.jpg");
JButton[][] button=new JButton[13][9];//创建按钮对象
public int xmove;
public int ymove;
public int xdecide=4;
public int ydecide=12;
public int decide[][]={
{0,0,0,0,0,0,0,0,0},//小人是1
{0,0,0,0,0,0,0,0,0},//木桩子是q2
{2,0,0,0,0,0,0,0,0},//桥是3
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,2,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{2,0,2,3,3,3,2,0,0},
{0,0,0,0,0,0,0,0,0},
{2,0,0,0,2,0,0,0,0},
{3,0,0,0,3,0,0,0,0},
{2,0,0,0,3,0,0,0,0},
{0,0,0,0,3,0,0,0,0},
{0,0,0,0,1,0,0,0,0},
};
public Map1()
{
setSize(300,450);//设置大小
//JButton[][] button=new JButton[13][9];//创建按钮对象
JPanel panel=new JPanel();
setTitle("RiverCrossing");
this.add(panel);
panel.setLayout(new GridLayout(13,9,0,0));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for(int a=0;a<9;a++)
{
button[0][a]=new JButton(bank2);
}
for(int a=0;a<9;a++)
{
button[12][a]=new JButton(bank1);
}
for(int a=1;a<12;a++)
{
for(int b=0;b<9;b++)
{
button[a][b]=new JButton(water1);
}
}
//下面是第一个地图的(这些跟问题没关)
button[2][0]=new JButton(stump1);
button[6][0]=new JButton(stump1);
button[8][0]=new JButton(stump1);
button[10][0]=new JButton(stump1);
button[3][1]=new JButton(water4);
button[5][1]=new JButton(water3);
button[5][2]=new JButton(water4);
button[6][2]=new JButton(stump1);
button[8][2]=new JButton(water4);
button[2][4]=new JButton(water4);
button[4][4]=new JButton(stump1);
button[8][4]=new JButton(stump1);
button[3][6]=new JButton(water3);
button[6][6]=new JButton(stump1);
button[12][4]=new JButton(stump2_man);
button[0][4]=new JButton(stump3);
button[9][0]=new JButton(plank2);
button[9][4]=new JButton(plank2);
button[10][4]=new JButton(plank2);
button[11][4]=new JButton(plank2);
buttonListener btListener[][]=new buttonListener[13][9];//创建监听器
for(int a=0;a<13;a++)
{
for(int b=0;b<9;b++)
{
panel.add(button[a][b]);
button[a][b].addMouseListener(btListener[a][b]);//给监听器加到按钮
//System.out.printf("21");
}
}
}
public class buttonListener implements MouseListener
{
public void mouseReleased(MouseEvent e)
{ System.out.printf("1");
outterloop:if(e.getButton() == MouseEvent.BUTTON1)//左键该执行的操作(移动小人)
{
for(ymove=0;ymove<13;ymove++)//行的位置
{
for(xmove=0;xmove<9;xmove++)//列的位置
{
if(e.getSource()==button[ymove][xmove])//点击的那个按钮的坐标
{
//move(button,ymove,xmove);
ymove=0;
xmove=0;
break outterloop;
}
}
}
}
}
public void mousePressed(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
}
}