我不熟悉java。当我开始我的jframe(存储卡游戏)时,我点击两个相同的磁贴只有第一个显示。示例:当我点击时 新的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) {
}
}
答案 0 :(得分:1)
在public void TileClicked(Tile tile)中,当SelectedTile == null时,你只能使用tileShowface 第一次输入此方法时,您可以设置SelectedTile = tile,以便下次输入此方法时使用该语句 if(SelectedTile == null)为false,因此您无法两次调用tileShowface方法 除非您将SelectedTile设置为null
也许您应该使用不同的计数器来确定这是玩家点击的第一个图块还是第二个图块。在对不同的瓷砖进行2次点击后,将此计数器设置为零