create event by clicking at the jlabel
I want to create event when clicking on JLabel
when the value of playertyp is 1
how can i do that?
this is the class
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
}
}
Use a JButton instead of a JLabel, then you can just add an ActionListener to the button. You can make the button look like a label by using:
button.setBorderPainted( false );
When you set the Icon you can use the setActionCommand(...) method to control processing when the button is clicked.
Read the section from the Swing tutorial on How to Use Buttons for an explanation and example.
i want to create event only when the playertyp equal to 1
Then when you change the Icon, you can remove the ActionListener from the button so that no event is generated.
Move the declaration of your JLabel
before the if (i == j && i == 0)
statement and only add the mouse listener when you set playertyp
to one:
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;
}
You could create a subclass of a JLabel
that implements a MouseListener
. The subclass will also keep the parent object that created it, so that when the MouseListener 's mouseClicked
method is invoked it will call the appropriate method in the parent object.
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) {}
}
精彩评论