简体   繁体   English

更改JButton的图标

[英]Changing the icon of a JButton

So I am trying to create a tic tac toe game that uses the possible slots as buttons. 因此,我尝试创建一个使用可能的插槽作为按钮的井字游戏。 At the start of the game, the buttons are set to a blank white image as their icon. 在游戏开始时,按钮被设置为空白的白色图像作为其图标。 When the button is clicked I am trying to have the button icon change to the corresponding image (x or o), however when a button is clicked the image does not change? 单击按钮时,我试图将按钮图标更改为相应的图像(x或o),但是单击按钮时,图像是否不变? Where am I going wrong? 我要去哪里错了?

package game;

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants  ;

public class TicTacToe extends JFrame {

private static final String TITLE = "Tic Tac Toe";
private static final int width = 600;
private static final int height = 800;

private Container content;
private JButton exitbutton;
private JButton initbutton;
private JButton[] spaces = new JButton[9];
private CellButtonHandler[] cellHandler = new CellButtonHandler[9];
private InitButtonHandler inithandler;
private ExitButtonHandler exithandler;
private JLabel turn;
private URL base;
private Image o, x, blank;
private Font myfont = new Font("Times New Roman", Font.BOLD, 78);
private ImageIcon icon = new ImageIcon("data/blankButton.png");
private ImageIcon iconx = new ImageIcon("data/x.png");
private ImageIcon icono = new ImageIcon("data/o.gif");

private boolean gameover;
private boolean turnX;

public TicTacToe() {
    setTitle(TITLE);
    setSize(width, height);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);

    content = getContentPane();
    content.setBackground(Color.black);

    content.setLayout(new GridLayout(4, 3));

    for (int i = 0; i < spaces.length; i++) {
        spaces[i] = new JButton();
        cellHandler[i] = new CellButtonHandler();
        spaces[i].addActionListener(cellHandler[i]);
    }

    exitbutton = new JButton("Exit");
    exithandler = new ExitButtonHandler();
    exitbutton.addActionListener(exithandler);
    initbutton = new JButton("Clear");
    inithandler = new InitButtonHandler();
    initbutton.addActionListener(inithandler);

    turn = new JLabel("X's turn", SwingConstants.CENTER);
    turn.setForeground(Color.white);

    for (int i = 0; i < spaces.length; i++) {
        spaces[i].setBackground(Color.white);
        content.add(spaces[i]);
    }

    content.add(initbutton);
    content.add(turn);
    content.add(exitbutton);

    init();
}

public void init() {
    turnX = true;
    gameover = false;
    spaces[0].setIcon(icon);
    spaces[1].setIcon(icon);
    spaces[2].setIcon(icon);
    spaces[3].setIcon(icon);
    spaces[4].setIcon(icon);
    spaces[5].setIcon(icon);
    spaces[6].setIcon(icon);
    spaces[7].setIcon(icon);
    spaces[8].setIcon(icon);

    turn.setText("X's turn");

    setVisible(true);

}

public static void main(String[] args) {
    TicTacToe game = new TicTacToe();
}

private class CellButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        if (gameover) {
            return;
        }

        JButton pressed = (JButton) (e.getSource());

        if (pressed.getIcon().equals(icono) || pressed.getIcon().equals(iconx)) {
            return;
        }

        if (turnX) {
            pressed.setIcon(iconx);
        } else {
            pressed.setIcon(icono);
        }

        if (checkwinnerO()) {
            gameover = true;
            turn.setText("O Wins!");
            } 

        if(checkwinnerX()){
            gameover = true;
            turn.setText("X Wins!");
        }
    }
}

private class ExitButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        System.exit(0);
    }
}

private class InitButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        init();
    }
}

public boolean checkwinnerO() {
    if (spaces[0].getIcon().equals(icono)
            && spaces[1].getIcon().equals(icono) && spaces[2].getIcon().equals(icono)) {
        return true;
    } else if (spaces[3].getIcon().equals(icono)
            && spaces[4].getIcon().equals(icono) && spaces[5].getIcon().equals(icono)) {
        return true;
    } else if (spaces[6].getIcon().equals(icono)
            && spaces[7].getIcon().equals(icono) && spaces[8].getIcon().equals(icono)) {
        return true;
    } else if (spaces[0].getIcon().equals(icono)
            && spaces[3].getIcon().equals(icono) && spaces[6].getIcon().equals(icono)) {
        return true;
    } else if (spaces[1].getIcon().equals(icono)
            && spaces[4].getIcon().equals(icono) && spaces[7].getIcon().equals(icono)) {
        return true;
    } else if (spaces[2].getIcon().equals(icono)
            && spaces[5].getIcon().equals(icono) && spaces[8].getIcon().equals(icono)) {
        return true;
    } else if (spaces[0].getIcon().equals(icono)
            && spaces[4].getIcon().equals(icono) && spaces[8].getIcon().equals(icono)) {
        return true;
    } else if (spaces[2].getIcon().equals(icono)
            && spaces[4].getIcon().equals(icono) && spaces[6].getIcon().equals(icono)) {
        return true;
    } else 

        return false;
}

public boolean checkwinnerX() {
    if (spaces[0].getIcon().equals(iconx)
            && spaces[1].getIcon().equals(iconx) && spaces[2].getIcon().equals(iconx)) {
        return true;
    } else if (spaces[3].getIcon().equals(iconx)
            && spaces[4].getIcon().equals(iconx) && spaces[5].getIcon().equals(iconx)) {
        return true;
    } else if (spaces[6].getIcon().equals(iconx)
            && spaces[7].getIcon().equals(iconx) && spaces[8].getIcon().equals(iconx)) {
        return true;
    } else if (spaces[0].getIcon().equals(iconx)
            && spaces[3].getIcon().equals(iconx) && spaces[6].getIcon().equals(iconx)) {
        return true;
    } else if (spaces[1].getIcon().equals(iconx)
            && spaces[4].getIcon().equals(iconx) && spaces[7].getIcon().equals(iconx)) {
        return true;
    } else if (spaces[2].getIcon().equals(iconx)
            && spaces[5].getIcon().equals(iconx) && spaces[8].getIcon().equals(iconx)) {
        return true;
    } else if (spaces[0].getIcon().equals(iconx)
            && spaces[4].getIcon().equals(iconx) && spaces[8].getIcon().equals(iconx)) {
        return true;
    } else if (spaces[2].getIcon().equals(iconx)
            && spaces[4].getIcon().equals(iconx) && spaces[6].getIcon().equals(iconx)) {
        return true;
    } else 

        return false;

}
}
  1. The image icons are initialized. 图像图标被初始化。
  2. My init method sets each of the buttons icon to the blank image. 我的init方法将每个按钮图标设置为空白图像。
  3. My CellHandlerMethod handles the change between the blank image and the image (x or o) when clicked. 当单击时,我的CellHandlerMethod处理空白图像和图像(x或o)之间的更改。

This output definitely shows a problem with the logic of the game. 此输出肯定表明游戏逻辑存在问题。

井字游戏

Note that it does change from the red square (representing 'blank') to the green circle when the button(s) is(/are) clicked. 请注意,单击按钮时,它的确从红色正方形(表示“空白”)变为绿色圆圈。 But it never flips from Player X to Player Y (or vice-versa). 但是它永远不会从玩家X切换到玩家Y(反之亦然)。

As to why you are not seeing any change, I can only conclude one of 3 things: 至于为什么看不到任何变化,我只能得出以下三点之一:

  1. The images are not where you think they are. 图像不在您认为的位置。
  2. The images are not named what you think they are. 这些图像未命名为您认为的名称。 (EG x.png != X.png || x.PNG ) (EG x.png != X.png || x.PNG
  3. The images are not using an internal format understood by Java. 图像未使用Java理解的内部格式。 Try displaying them directly in a JOptionPane . 尝试直接在JOptionPane显示它们。

Tips 提示

Don't use an ImageIcon to load an image, it does not give feedback. 不要使用ImageIcon加载图像,它不会提供反馈。 Instead use ImageIO.read(..) which provides lots of helpful feedback. 而是使用ImageIO.read(..) ,它提供了很多有用的反馈。

Source 资源

This is the slightly altered source used. 这是使用的稍微改变的来源。 It hot-links to 3 images (so that users of the code can see it work or fail). 它热链接到3张图像(以便代码的用户可以看到它的工作或失败)。

import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;

public class TicTacToe extends JFrame {

private static final String TITLE = "Tic Tac Toe";
private static final int width = 600;
private static final int height = 800;

private Container content;
private JButton exitbutton;
private JButton initbutton;
private JButton[] spaces = new JButton[9];
private CellButtonHandler[] cellHandler = new CellButtonHandler[9];
private InitButtonHandler inithandler;
private ExitButtonHandler exithandler;
private JLabel turn;
private URL base;
private Image o, x, blank;
private Font myfont = new Font("Times New Roman", Font.BOLD, 78);
private ImageIcon icon;
private ImageIcon iconx;
private ImageIcon icono;

private boolean gameover;
private boolean turnX;

public TicTacToe() {
    setTitle(TITLE);
    try {
        URL url = new URL("http://i.stack.imgur.com/F0JHK.png");
        icon = new ImageIcon(url);
        URL urlx = new URL("http://i.stack.imgur.com/T5uTa.png");
        iconx = new ImageIcon(urlx);
        URL urlo = new URL("http://i.stack.imgur.com/yoKxT.png");
        icono = new ImageIcon(urlo);
    } catch (Exception e) {
        e.printStackTrace();
    }
    setSize(width, height);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);

    content = getContentPane();
    content.setBackground(Color.black);

    content.setLayout(new GridLayout(4, 3));

    for (int i = 0; i < spaces.length; i++) {
        spaces[i] = new JButton();
        cellHandler[i] = new CellButtonHandler();
        spaces[i].addActionListener(cellHandler[i]);
    }

    exitbutton = new JButton("Exit");
    exithandler = new ExitButtonHandler();
    exitbutton.addActionListener(exithandler);
    initbutton = new JButton("Clear");
    inithandler = new InitButtonHandler();
    initbutton.addActionListener(inithandler);

    turn = new JLabel("X's turn", SwingConstants.CENTER);
    turn.setForeground(Color.white);

    for (int i = 0; i < spaces.length; i++) {
        spaces[i].setBackground(Color.white);
        content.add(spaces[i]);
    }

    content.add(initbutton);
    content.add(turn);
    content.add(exitbutton);

    init();
}

public void init() {
    turnX = true;
    gameover = false;
    spaces[0].setIcon(icon);
    spaces[1].setIcon(icon);
    spaces[2].setIcon(icon);
    spaces[3].setIcon(icon);
    spaces[4].setIcon(icon);
    spaces[5].setIcon(icon);
    spaces[6].setIcon(icon);
    spaces[7].setIcon(icon);
    spaces[8].setIcon(icon);

    turn.setText("X's turn");

    setVisible(true);

}

public static void main(String[] args) {
    TicTacToe game = new TicTacToe();
}

private class CellButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        if (gameover) {
            return;
        }

        JButton pressed = (JButton) (e.getSource());

        if (pressed.getIcon().equals(icono) || pressed.getIcon().equals(iconx)) {
            return;
        }

        if (turnX) {
            pressed.setIcon(iconx);
        } else {
            pressed.setIcon(icono);
        }

        if (checkwinnerO()) {
            gameover = true;
            turn.setText("O Wins!");
            }

        if(checkwinnerX()){
            gameover = true;
            turn.setText("X Wins!");
        }
    }
}

private class ExitButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        System.exit(0);
    }
}

private class InitButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        init();
    }
}

public boolean checkwinnerO() {
    if (spaces[0].getIcon().equals(icono)
            && spaces[1].getIcon().equals(icono) && spaces[2].getIcon().equals(icono)) {
        return true;
    } else if (spaces[3].getIcon().equals(icono)
            && spaces[4].getIcon().equals(icono) && spaces[5].getIcon().equals(icono)) {
        return true;
    } else if (spaces[6].getIcon().equals(icono)
            && spaces[7].getIcon().equals(icono) && spaces[8].getIcon().equals(icono)) {
        return true;
    } else if (spaces[0].getIcon().equals(icono)
            && spaces[3].getIcon().equals(icono) && spaces[6].getIcon().equals(icono)) {
        return true;
    } else if (spaces[1].getIcon().equals(icono)
            && spaces[4].getIcon().equals(icono) && spaces[7].getIcon().equals(icono)) {
        return true;
    } else if (spaces[2].getIcon().equals(icono)
            && spaces[5].getIcon().equals(icono) && spaces[8].getIcon().equals(icono)) {
        return true;
    } else if (spaces[0].getIcon().equals(icono)
            && spaces[4].getIcon().equals(icono) && spaces[8].getIcon().equals(icono)) {
        return true;
    } else if (spaces[2].getIcon().equals(icono)
            && spaces[4].getIcon().equals(icono) && spaces[6].getIcon().equals(icono)) {
        return true;
    } else

        return false;
}

public boolean checkwinnerX() {
    if (spaces[0].getIcon().equals(iconx)
            && spaces[1].getIcon().equals(iconx) && spaces[2].getIcon().equals(iconx)) {
        return true;
    } else if (spaces[3].getIcon().equals(iconx)
            && spaces[4].getIcon().equals(iconx) && spaces[5].getIcon().equals(iconx)) {
        return true;
    } else if (spaces[6].getIcon().equals(iconx)
            && spaces[7].getIcon().equals(iconx) && spaces[8].getIcon().equals(iconx)) {
        return true;
    } else if (spaces[0].getIcon().equals(iconx)
            && spaces[3].getIcon().equals(iconx) && spaces[6].getIcon().equals(iconx)) {
        return true;
    } else if (spaces[1].getIcon().equals(iconx)
            && spaces[4].getIcon().equals(iconx) && spaces[7].getIcon().equals(iconx)) {
        return true;
    } else if (spaces[2].getIcon().equals(iconx)
            && spaces[5].getIcon().equals(iconx) && spaces[8].getIcon().equals(iconx)) {
        return true;
    } else if (spaces[0].getIcon().equals(iconx)
            && spaces[4].getIcon().equals(iconx) && spaces[8].getIcon().equals(iconx)) {
        return true;
    } else if (spaces[2].getIcon().equals(iconx)
            && spaces[4].getIcon().equals(iconx) && spaces[6].getIcon().equals(iconx)) {
        return true;
    } else

        return false;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM