简体   繁体   English

当我在Java中单击它们时,我想显示相同的类图块

[英]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.

相关问题 当我想使用 java.io.File 类时,为什么会出现错误“找不到符号”? - Why I am getting error "cannot find symbol" when I would like to use java.io.File class? 当我获得Java库时,如何使用它们并在Java中使用它? - When I get Java Libraries how would I use them and use it in Java? 我正在尝试在 android 工作室制作类似钢琴瓷砖的东西,我希望在以相同速度按下播放按钮时瓷砖开始出现 - I'm trying to make something like Piano Tiles in android studio and I want the tiles to start coming when play button is pressed with same speed 在 Intellij 中,当我创建新的 package 并右键单击新建后 - > java class 不显示 - In Intellij, when I create new package and after right click new -> java class doesn't show 当我单击一个按钮时,我只想激活其周围的按钮。 这怎么可能? - When i click a button i would like to activate only its surrounding buttons. How is this possible? 如果我在 java 上导入并使用像这样的“扫描仪”,为什么我的热水器不显示一个框让我写在上面 - Why my therminal dont show a box to i write on them if i import and use the “Scanner” like this on java 单击按钮时,如何使用JAVAFX使标签显示数字递增 - Using JAVAFX how would i make a label show numbers incrementing when i click a button 我想在java中做printFrequency方法 - I would like to do printFrequency method in java Java - 不打印我想要的东西? - Java - Not printing what I would like? 当您单击每个树节点时,我想将信息添加到jpanel jscrollpane中 - I would like to put the information to add in jpanel jscrollpane when you click each tree node
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM