gpt4 book ai didi

java - 当我在java中单击它们时,我想显示相同的类图 block

转载 作者:行者123 更新时间:2023-12-01 13:36:21 24 4
gpt4 key购买 nike

我对java不太了解。当我启动 jframe(存储卡游戏)时,我单击两个相同的图 block ,仅显示第一个图 block 。示例:当我点击新的 OrangeTile,然后在另一个新的 OrangeTile 上,只有第一个保留在tile.showface

package control;

import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.JFrame;

import Tiles.*;


public class Control extends JFrame {

private static final long serialVersionUID = 1L;
public static Control CurrentWindow = null;

private final String title ="Remembory";

private Tile SelectedTile = null;
private int points = 0;

public Control() {

setSize(250,260);
setTitle(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setUpGame();

setVisible(true);

}

private void setUpGame()
{
getContentPane().setLayout(new FlowLayout());
getContentPane().add(new OrangeTile());
getContentPane().add(new OrangeTile());
getContentPane().add(new YellowTile());
getContentPane().add(new YellowTile());
getContentPane().add(new GreenTile());
getContentPane().add(new GreenTile());
getContentPane().add(new RedTile());
getContentPane().add(new RedTile());
getContentPane().add(new BlueTile());
getContentPane().add(new BlueTile());
getContentPane().add(new GrayTile());
getContentPane().add(new GrayTile());
getContentPane().add(new WhiteTile());
getContentPane().add(new WhiteTile());
getContentPane().add(new TurqTile());
getContentPane().add(new TurqTile());

}

private void AddPoint() {
points++;
System.out.println(" + " + points + "Points");

}

public void TileClicked (Tile tile){
if (SelectedTile == null) {
tile.showFace();
SelectedTile = tile;
return;
}
if (SelectedTile == tile) {
tile.hideFace();
SelectedTile = null;
return;
}
if (points == 7){
System.exit(0);
}
if (SelectedTile.getFaceColor() == tile.getFaceColor()) {
AddPoint();
getContentPane().remove(SelectedTile);
getContentPane().remove(tile);
SelectedTile = null;
return;
}
SelectedTile.hideFace();
SelectedTile = null;
}


public static void main(String[] args){
CurrentWindow = new Control();
}

}

package Tiles;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JLabel;

public class Tile extends JLabel implements MouseListener{

private static final long serialVersionUID = 1L;

private Color faceColor = Color.BLACK;
private final static Dimension size = new Dimension(50,50);

public Tile()
{
setMinimumSize(size);
setMaximumSize(size);
setPreferredSize(size);
setOpaque(true);
setBackground(new Color(213,86,31));
addMouseListener(this);
}



public void showFace()
{
setBackground(faceColor);
}
public void hideFace()
{
setBackground(new Color(213,86,31));

}

protected void setFaceColor(Color c)
{
this.faceColor = c;
}

public Color getFaceColor()
{
return this.faceColor;

}


public void mouseClicked(MouseEvent arg0) {
control.Control.CurrentWindow.TileClicked(this);
}


public void mouseEntered(MouseEvent arg0) {

}


public void mouseExited(MouseEvent arg0) {

}


public void mousePressed(MouseEvent arg0) {

}


public void mouseReleased(MouseEvent arg0) {


}

}

最佳答案

在 public void TileClicked (Tiletile) 中,当 SelectedTile == null 时,您只能使用一次tileShowface第一次进入此方法时,您设置 SelectedTile =tile,因此下次进入此方法时,该语句if (SelectedTile == null) 为 false,因此您不能两次调用tileShowface 方法除非您将 SelectedTile 设置回 null

也许您应该使用不同的计数器来了解这是玩家点击的第一个图 block 还是第二个图 block 。在不同的图 block 上单击 2 次后,将此计数器设置回零

关于java - 当我在java中单击它们时,我想显示相同的类图 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21239703/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com