当前位置: 首页 > 知识库问答 >
问题:

为什么我的GUI没有出现?我正在尝试制作一个抽象棋tac toe board但我的GUI没有显示出来

丌官飞章
2023-03-14

我正试图在Java制作一个抽风游戏,但我的GUI就是没有出现。它制作了一个棋盘,你可以按下按钮来玩井字游戏。我刚来Java,所以我不知道哪里出了问题。它没有错误,我使用repl。怎么啦?

import javax.swing.JFrame;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.GridLayout;
public class Main extends JFrame implements ActionListener {
  private static final long serialVersionUID = 1L;
private JPanel panel = new JPanel();
private XOButtons[] buttons = new XOButtons[9];
private int turn = 0;
@Override
public void actionPerformed(ActionEvent actionEvent) {
XOButtons source = (XOButtons)actionEvent.getSource();
if(turn == 0){
source.toggleX();
setTitle("O Turn");
} else if(turn == 1){
source.toggleO();
setTitle("X's Turn");
}
turn = (turn + 1) % 2;
}
 public static void main(String[] args) {
  
   new TicTacToe();
}
  
 public void TicTacToe() {
setTitle("Tic Tac Toe");
setSize(600,600);

setLocation(100,100);
getContentPane().setBackground(Color.CYAN);
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel.setLayout(new GridLayout(3,3,5,5));
panel.setBackground(Color.BLUE);
for(int i=0; i < buttons.length; i++) {
buttons[i] = new XOButtons();
buttons[i].addActionListener(this);
panel.add(buttons[i]);
}
add(panel);
setVisible(true);
}
}

这里是我的第二个Java文件:

import javax.swing.JFrame;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.ImageIcon;
public class XOButtons extends JButton {

private static final long serialVersionUID = 1L;
private ImageIcon xIcon = new ImageIcon(getClass().getResource("x.png"));
private ImageIcon oIcon = new ImageIcon(getClass().getResource("o.png"));
public void toggleX() {
if(getIcon() == null) {
setIcon(xIcon);
} else {
setIcon(null);
}
}
public void toggleO() {
if(getIcon() == null) {
setIcon(oIcon);
} else {
setIcon(null);
}
}
}

另外,我已经加载了x.png和o.png。

共有1个答案

鄢英毅
2023-03-14

在该代码中存在一些问题,并且解决问题的方法不是最优的,但这个问题是由于将类名(tictactoe)转换为main并将构造函数更改为方法而引起的。

这就是修正后的结果。

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

public class TicTacToe extends JFrame implements ActionListener {

    private final JPanel panel = new JPanel();
    private final XOButtons[] buttons = new XOButtons[9];
    private int turn = 0;

    public TicTacToe() {
        setTitle("Tic Tac Toe");
        setSize(600, 600);

        setLocation(100, 100);
        getContentPane().setBackground(Color.CYAN);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        panel.setLayout(new GridLayout(3, 3, 5, 5));
        panel.setBackground(Color.BLUE);
        for (int i = 0; i < buttons.length; i++) {
            buttons[i] = new XOButtons();
            buttons[i].addActionListener(this);
            panel.add(buttons[i]);
        }
        add(panel);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        XOButtons source = (XOButtons) actionEvent.getSource();
        if (turn == 0) {
            source.toggleX();
            setTitle("O Turn");
        } else if (turn == 1) {
            source.toggleO();
            setTitle("X's Turn");
        }
        turn = (turn + 1) % 2;
    }

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

class XOButtons extends JButton {

    private ImageIcon xIcon = null;
    private ImageIcon oIcon = null;

    XOButtons() {
        try {
            xIcon = new ImageIcon(new URL("https://i.stack.imgur.com/in9g1.png"));
            oIcon = new ImageIcon(new URL("https://i.stack.imgur.com/wCF8S.png"));
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        }
    }

    public void toggleX() {
        if (getIcon() == null) {
            setIcon(xIcon);
        } else {
            setIcon(null);
        }
    }

    public void toggleO() {
        if (getIcon() == null) {
            setIcon(oIcon);
        } else {
            setIcon(null);
        }
    }
}
 类似资料:
  • 我正在尝试使用CSS边框在GUI(fxml)上显示墙。getStyleClass()。add()但它们没有出现。 我正在使用一个开关,将相应的样式类添加到单元格中。 Java代码: CSS代码: GUI事后图片:结果

  • 这应该是一个相对简单的问题,但它让我发疯。我正在尝试在JavaFX中创建扫雷器(主要用于练习),但我甚至无法显示一个简单的矩形。我以前运行过一次游戏,但我试图使游戏更加抽象,因此更容易编码,但我遇到了不显示任何问题。 我消除了所有无关的代码,使其尽可能简单。我基本上是在尝试创建一个名为Box的具有特定颜色和大小的矩形,将框添加到窗格中,并显示窗格。为了使Box成为可以在窗格上显示的节点,我使Box

  • 我正在做一个任务,我必须创建一个“理想体重”计算器(有点像BMI计算器)。用户应该输入他们的身高、姓名和他们希望接收答案的测量系统。 IE;测量系统=(M)etric或(I)mperial,其中用户输入一个“M”或一个“I”。这意味着他们输入的高度必须对应于他们选择的单位。 IE;身高=1.5米或59英寸(除了他们不写单位,只写数字)。 我试图修复可能存在的任何语法错误,目前不再有任何错误,但当我

  • 问题内容: 我是Java初学者 我试图制作我的jframe节目,但没有 它不起作用 问题答案: 我认为您没有正确声明您的JFrame。这是创建简单框架的示例:

  • 您将自动执行著名的歌曲“墙上的99瓶XXX”。你将打印这首歌所有99个诗句的歌词。用循环!如果你不知道歌词,用谷歌查一下。 该方案应: a.如果他们不到21岁,或者他们喜欢苏打水,那么歌词是“墙上有99瓶苏打水” B.如果他们超过21岁,那么是“99瓶啤酒” 您必须使用WHILE循环,并且counter变量必须是print语句的一部分! 所以第一节是: 99瓶苏打水挂在墙上 墙上有98瓶苏打水 最