[英]I would like to show the same class tiles when i click on them in java
Im not that known with java. 我不是用Java知道的。 When i start my jframe (memory card game), And i click on two the same tiles only the first one shows. 当我启动我的jframe(记忆卡游戏)时,我单击了两个相同的磁贴,只有第一个显示。 Example: when i click on the new OrangeTile and then on the other new OrangeTile only the first one stays on the tile.showface 例如:当我单击新的OrangeTile然后在另一个新的OrangeTile上时,只有第一个停留在图块上
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) {
}
} }
In public void TileClicked (Tile tile) you can only use tileShowface once when SelectedTile == null The fist time you enter this method you set the SelectedTile = tile so the next time you enter this method the statement if (SelectedTile == null) is false so you can't call the tileShowface method twice unless you set the SelectedTile back to null 在public void TileClicked(平铺图块)中,当SelectedTile == null时,您只能使用tileShowface一次。第一次输入此方法时,请设置SelectedTile = tile,因此下次输入此方法时,如果(SelectedTile == null)为false,因此您不能调用tileShowface方法两次,除非您将SelectedTile设置回null。
Maybe you should use a different counter to know if this is the first tile the player clicks or the second. 也许您应该使用其他计数器来知道这是玩家单击的第一个图块还是第二个。 And after 2 clicks on different tiles set this counter back to zero 在不同的图块上单击2次后,将此计数器设置为零
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.