2011-05-14 79 views
0

我想创建活动时点击JLabel当playertyp的值是1 我该怎么做? 这是类通过点击jlabel创建活动

public class Draw_Board implements MouseListener,ActionListener 
{ 

    private Point matloc; 
    private ImageIcon partpic; 
    private JLabel partstick; 

    private boolean playerexis; 
    private int playertyp=0; 
    private boolean partexis; 
    private boolean compute; 
    private Part[][] map; 



    public Draw_Board() 
    { 

     int x = 52; 
     int y =49; 

     map=new Part[9][12]; 
     for(int i=0;i<9;i++) 
     { 
      for(int j=0;j<12;j++) 
      { 
       matloc=new Point(); 
       if(i==j && i==0) 
        { 
        partpic = new ImageIcon(getClass().getResource("images/bird.png")); 
        playertyp=1;//the bird equal to player 1 

        } 
       else{ 
        partpic = new ImageIcon(getClass().getResource("images/stone.png")); 
        playertyp=2; 
       } 

       partstick = new JLabel("",partpic,JLabel.CENTER); 
       partstick.addMouseListener(this); 

       matloc.setX(x); 
       matloc.setY(y); 
       x+=61; 
       if(j==11) 
       { 
        x=52; 
        y+=57; 
       } 




       partstick.setLocation(matloc.getX(),matloc.getY()); 
       partstick.setSize(60,60); 

       map[i][j]=new Part(matloc, partpic, partstick, 
         playerexis, playertyp, partexis, compute); 

      // if(i==0 && j==0)System.out.println(partstick.getX()+" "+partstick.getY()); 


      } 
     } 

    } 




    public void draw(Panelmenu p) 
    { 


     for (int i = 0; i < 9; i++) 
     { 
      for (int j = 0; j < 12; j++) 
      { 
       map[i][j].draw(p); 

      } 
     } 

    } 








    public Point getMatloc() { 
     return matloc; 
    } 




    public void setMatloc(Point matloc) { 
     this.matloc = matloc; 
    } 




    public ImageIcon getPartpic() { 
     return partpic; 
    } 




    public void setPartpic(ImageIcon partpic) { 
     this.partpic = partpic; 
    } 




    public JLabel getPartstick() { 
     return partstick; 
    } 




    public void setPartstick(JLabel partstick) { 
     this.partstick = partstick; 
    } 




    public boolean isPlayerexis() { 
     return playerexis; 
    } 




    public void setPlayerexis(boolean playerexis) { 
     this.playerexis = playerexis; 
    } 




    public int getPlayertyp() { 
     return playertyp; 
    } 




    public void setPlayertyp(int playertyp) { 
     this.playertyp = playertyp; 
    } 




    public boolean isPartexis() { 
     return partexis; 
    } 




    public void setPartexis(boolean partexis) { 
     this.partexis = partexis; 
    } 




    public boolean isCompute() { 
     return compute; 
    } 




    public void setCompute(boolean compute) { 
     this.compute = compute; 
    } 




    public Part[][] getMap() { 
     return map; 
    } 




    public void setMap(Part[][] map) { 
     this.map = map; 
    } 






    @Override 
    public void actionPerformed(ActionEvent e) 
    { 


    } 

    @Override 
    public void mouseClicked(MouseEvent e) { 




    } 

    @Override 
    public void mousePressed(MouseEvent e) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void mouseReleased(MouseEvent e) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void mouseEntered(MouseEvent e) { 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public void mouseExited(MouseEvent e) { 
     // TODO Auto-generated method stub 

    } 

} 

回答

1

使用一个JButton,而不是一个JLabel,那么你可以只添加一个ActionListener的按钮。您可以通过使用使按钮看起来像一个标签:

button.setBorderPainted(false); 

当您设置您可以使用setActionCommand(...)方法是单击按钮时控制处理的图标。

阅读Swing教程中有关How to Use Buttons的部分,以获得解释和示例。

我想创建活动只有当playertyp等于1

然后,当你更改图标,你可以从按钮,以便不产生任何事件中删除的ActionListener。

+0

我加addMouseListener(this);对于数组中的所有jlabel,但我只想在playertyp为1时创建事件 – user753709 2011-05-14 15:20:52

0

移动你JLabel的声明if (i == j && i == 0)语句之前,只有当你设置playertyp一个添加鼠标监听:

matloc=new Point(); 
partstick = new JLabel("",partpic,JLabel.CENTER); 

if(i==j && i==0) { 
    partpic = new ImageIcon(getClass().getResource("images/bird.png")); 
    playertyp=1;//the bird equal to player 1 
    partstick.addMouseListener(this); // Add mouse listener only when partype = 1 
} else { 
    partpic = new ImageIcon(getClass().getResource("images/stone.png")); 
    playertyp=2; 
} 
+0

但我希望在j == 0和i == 0中的图片将是defrent – user753709 2011-05-14 15:43:02

0

您可以创建一个JLabel一个实现MouseListener的子类。该子类还将保留创建它的父对象,以便在调用MouseListener的方法时,它将调用父对象中适当的方法。

public class MyJLabel extends JLabel implements MouseListener { 

public MyJLabel(String title) { 
super(title); 
addMouseListener(this); 
} 

public void mouseClicked(MouseEvent e) { 
//YOUR RESPONSE HERE 
} 

public void mousePressed(MouseEvent e) {} 
public void mouseReleased(MouseEvent e) {} 
public void mouseExited(MouseEvent e) {} 
public void mouseEntered(MouseEvent e) {} 

}