由按钮组成的面板网格

时间:2016-02-12 01:24:45

标签: java swing jbutton grid-layout tic-tac-toe

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

public class Game extends JFrame implements ActionListener {

private JPanel panel;
private JButton[] button;
private int count = 0;
private int sign = 0;


public Game(){
    panel = new JPanel();
    panel.setLayout(new GridLayout(3,3));
    add(panel);
    button = new JButton[9];
    for (int i = 0; i < 9; i++){
        button[i] = new JButton();
        button[i].setPreferredSize(new Dimension(100,100));
        panel.add(button[i]);
        button[i].setEnabled(true);
        button[i].addActionListener(this);
        button[i].setFont(new Font("Arial", Font.PLAIN, 30));
    }

    pack();
    setVisible(true);
}


public void actionPerformed(ActionEvent e){
    count ++;
    int i = 0;
    for(i = 0; i < 9; i++){
        if(button[i] == e.getSource()){
            if(sign % 2 == 0){
                button[i].setText("X");
                button[i].setEnabled(false);
            }
            else {
                button[i].setText("O");
                button[i].setEnabled(false);
            }
            break;
        }
    }

    if(isAWinner(i) && count > 2){

        JOptionPane.showMessageDialog(null, "" + button[i].getText() + "s Win");
        for(int j = 0; j < 9; j++){
            button[j].setText("");
            button[j].setEnabled(true);
        }
        count = 0;
        sign = 0;
        return;
    }



    if(count >= 9){
        JOptionPane.showMessageDialog(null, "Cat's Game!");  
        for(int j = 0; j < 9; j++){  
            button[j].setText("");  
            button[j].setEnabled(true);  
        }  
        count = 0;  
        sign = 0;  
        return;  
    }  

    sign++;  

}  

所以这很好用,tic tac toe本身的游戏没有任何问题 我的问题是当我尝试制作这些游戏的网格时

import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.JFrame;

public class TicTacTest{

public static void main(String[] args) {

    JFrame frame = new JFrame();
    frame.setTitle("Super TicTacToe");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(900,900);
    Container pane = frame.getContentPane();
    pane.setLayout(new GridLayout(3,3));

        Game[] games = new Game[9];

        for(int i = 0; i < 9; i++){

            games[i] = new Game();
            pane.add(games[i]);
        }


        frame.setVisible(true);

    }

}

我认为这样可行,但显然我错了,因为什么都没有出现。

我应该在主要内容中制作9个Tic Tac Toe板?

1 个答案:

答案 0 :(得分:4)

GameJFrame延伸,无法将其添加到其他类型的容器中(这只是愚蠢的)

这个例外会说得那么多......

Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container   
    at java.awt.Container.checkNotAWindow(Container.java:488)   
    at java.awt.Container.addImpl(Container.java:1089)  
    at java.awt.Container.add(Container.java:415)

相反,让GameJPanel延伸,然后将其添加到您想要的任何容器中

像...一样的东西。

TicTacToe

(ps,红色边框仅用于演示目的)

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

public class Test {

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setTitle("Super TicTacToe");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(900, 900);
        Container pane = frame.getContentPane();
        pane.setLayout(new GridLayout(3, 3));

        Game[] games = new Game[9];

        for (int i = 0; i < 9; i++) {

            games[i] = new Game();
            games[i].setBorder(new LineBorder(Color.RED));
            pane.add(games[i]);
        }

        frame.setVisible(true);

    }

    public static class Game extends JPanel implements ActionListener {

        private JButton[] button;
        private int count = 0;
        private int sign = 0;

        public Game() {
            setLayout(new GridLayout(3, 3));
            button = new JButton[9];
            for (int i = 0; i < 9; i++) {
                button[i] = new JButton();
                button[i].setPreferredSize(new Dimension(100, 100));
                add(button[i]);
                button[i].setEnabled(true);
                button[i].addActionListener(this);
                button[i].setFont(new Font("Arial", Font.PLAIN, 30));
            }
        }

        public void actionPerformed(ActionEvent e) {
            count++;
            int i = 0;
            for (i = 0; i < 9; i++) {
                if (button[i] == e.getSource()) {
                    if (sign % 2 == 0) {
                        button[i].setText("X");
                        button[i].setEnabled(false);
                    } else {
                        button[i].setText("O");
                        button[i].setEnabled(false);
                    }
                    break;
                }
            }

            if (isAWinner(i) && count > 2) {

                JOptionPane.showMessageDialog(null, "" + button[i].getText() + "s Win");
                for (int j = 0; j < 9; j++) {
                    button[j].setText("");
                    button[j].setEnabled(true);
                }
                count = 0;
                sign = 0;
                return;
            }

            if (count >= 9) {
                JOptionPane.showMessageDialog(null, "Cat's Game!");
                for (int j = 0; j < 9; j++) {
                    button[j].setText("");
                    button[j].setEnabled(true);
                }
                count = 0;
                sign = 0;
                return;
            }

            sign++;

        }

        public boolean isAWinner(int i) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

    }
}