为什么我的 GUI 不显示?我正在尝试制作井字游戏板,但我的 GUI 没有显示

时间:2021-04-28 13:35:21

标签: java swing user-interface

我正在尝试用 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 个答案:

答案 0 :(得分:1)

在该代码中存在一些问题并且不是最佳解决方法,但是这个问题是由于将类名 (TicTacToe) 变成 Main 并将构造函数更改为方法。

这是修正后的结果。

enter image description here

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);
        }
    }
}