Java 通过单击jlabel创建事件
当playertyp的值为1时,我想在单击Java 通过单击jlabel创建事件,java,Java,当playertyp的值为1时,我想在单击JLabel时创建事件 我该怎么做? 这是一节课 public class Draw_Board implements MouseListener,ActionListener { private Point matloc; private ImageIcon partpic; private JLabel partstick; private boolean playerexis; private int p
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
}
}
公共类Draw\u Board实现MouseListener、ActionListener
{
私人点matloc;
私人图像图标;
私人JLabel partstick;
私有布尔playerexis;
私有int playertyp=0;
私有布尔轴;
私有布尔计算;
私有部分[]地图;
公共抽签委员会()
{
int x=52;
int y=49;
map=新零件[9][12];
对于(int i=0;i使用JButton而不是JLabel,那么您可以向按钮添加ActionListener。您可以使用以下命令使按钮看起来像标签:
button.setBorderPainted( false );
设置图标时,可以使用setActionCommand(…)方法控制单击按钮时的处理
请阅读上Swing教程中的部分以获取解释和示例
我只想在playertyp等于1时创建事件
然后,当您更改图标时,可以从按钮中删除ActionListener,这样就不会生成任何事件。将JLabel的声明移动到if(i==j&&i==0)
语句之前,并且仅在将playertyp
设置为1时添加鼠标侦听器:
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;
}
您可以创建JLabel
的子类,该子类实现了MouseListener
。该子类还将保留创建它的父对象,以便在调用MouseListener的mouseClicked
方法时,它将调用父对象中的相应方法
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) {}
}
我添加了addMouseListener(这个);对于数组中的所有jLabel,但我只想在playertyp为1时创建事件,但我希望j==0和i==0中的图片将被解除