java 不显示imageicon_java – JLabel不会显示图像

我正在使用Java创建一个Tic-Tac-Toe游戏.现在,我有了它,所以当你点击一个按钮,JButton将从JPanel中删除时,将添加一个包含X或O图像的JLabel,并重新绘制JPanel.但是,当我单击按钮时,图像将不会显示,但按钮会消失.

创建按钮和JLabel / Image:

package tictactoe;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.ImageIcon;

public class TicTacToe implements ActionListener

{

private JFrame holder = new JFrame();

private GridLayout layout = new GridLayout(3,3);

private FlowLayout panel = new FlowLayout(FlowLayout.CENTER);

private JPanel p11, p12, p13, p21, p22, p23, p31, p32, p33;

private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9;

private ImageIcon iconX = new ImageIcon("iconX.png");

private JLabel xLabel = new JLabel(iconX);

private ImageIcon iconO = new ImageIcon("iconO.png");

private JLabel oLabel = new JLabel(iconO);

private int turn;

private char s1, s2, s3, s4, s5, s6, s7, s8, s9;

public TicTacToe()

{

paint();

}

private void paint()

{

holder.setLayout(layout);

holder.setSize(300,300);

b1 = new JButton("1");

p11 = new JPanel();

p11.setLayout(panel);

p11.add(b1);

holder.add(p11);

//Same block of code for the next 8 buttons/panels inserted here

holder.setVisible(true);

b1.addActionListener(this);

//Other action listeners inserted here

}

@Override

public void actionPerformed(ActionEvent e)

{

if (e.getSource() == b1)

{

++turn;

p11.remove(b1);

if (turn % 2 == 1) { s1 = 'x'; p11.add(xLabel); }

else if (turn % 2 == 0) { s1 = 'o'; p11.add(oLabel); }

p11.repaint();

}

//Other action events inserted here

}

public static void main(String[] args)

{

TicTacToe game = new TicTacToe();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值