0

こんにちは私はここで新しいです私は私が次のようにGUIを作った小さな三目並べゲームを作っています:

public static void main(String[] args) {

    Frame frame1 =new Frame("TickTacToe");
    frame1.setLayout(null);
    frame1.setBounds(250,150,500,500);
    frame1.setVisible(true);
    frame1.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
            System.exit(0);
        }
    });

    final Button button11 = new Button("");
    button11.addActionListener(null);

    final Button button12 = new Button("");
    button11.addActionListener(null);

    final Button button13 = new Button("");
    button11.addActionListener(null);

    final Button button21 = new Button("");
    button11.addActionListener(null);

    final Button button22 = new Button("");
    button11.addActionListener(null);

    final Button button23 = new Button("");
    button11.addActionListener(null);

    final Button button31 = new Button("");
    button11.addActionListener(null);

    final Button button32 = new Button("");
    button11.addActionListener(null);

    final Button button33 = new Button("");
    button11.addActionListener(null);


    button11.setBounds(100, 100, 80, 70);
    button12.setBounds(100, 200, 80, 70);
    button13.setBounds(100, 300, 80, 70);
    button21.setBounds(200, 100, 80, 70);
    button22.setBounds(200, 200, 80, 70);
    button23.setBounds(200, 300, 80, 70);
    button31.setBounds(300, 100, 80, 70);
    button32.setBounds(300, 200, 80, 70);
    button33.setBounds(300, 300, 80, 70);

    frame1.add(button11); 
    frame1.add(button12); 
    frame1.add(button13); 
    frame1.add(button21); 
    frame1.add(button22); 
    frame1.add(button23); 
    frame1.add(button31); 
    frame1.add(button32);
    frame1.add(button33); 
}

ボタンにアクションリスナーを追加したいのですが、このボイドには別のクラスでも理想的ではないので、プレーヤーのターンチェンジャーなどの関数のループを実行する方法を作成できます。そしてoそれが他の場合私は私が使用する必要があるコードを多かれ少なかれ知っていますが、それ自身のボイド以外の場所からGUIを使用する方法を理解することはできません。私は私が何を探しているのかよくわからないので、どんな助けでも大歓迎です。

4

1 に答える 1

1

まず第一に、このプログラム構造はかなり悪いです。その見た目から、CやBasicなどの手続き型プログラミング言語から来たことがわかります。Javaは、オブジェクト指向アーキテクチャがすべてです。Javaは手続き的に実行できますが、そうすることを意図したものではありません。私が最初に始めることは、main()から抜け出すことです。これを行うための良い方法は次のとおりです。

public class TicTacToe
{
    public TicTacToe()
    {

    }

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

TicTacToeは単純に見えるかもしれませんが、一度に多くのことが行われています。ユーザーがボタンをクリックしているため、ActionListenersが必要です。移動するたびに画面のUIを更新する必要があります。また、各移動が有効であることを確認する必要があります。あなたはすべての動きとはるかの後に勝利をチェックする必要があります。これは、メインの内部で行うことはほとんど不可能です。

一般に、あらゆる種類のコンポーネントをJFrameに直接追加することはお勧めできません。JFrame内にJPanelを配置してから、コンポーネントをJPanelに追加することをお勧めします。

ゲームのクラス構造を作成してみてください。これが私がすることです:

TicTacToe.class --> Checks rules, checks for wins and starts and stops game
Player.class (implements ActionListener) --> Listens for each Player's input
Board.class (extends JPanel) --> this will display the components for the game
Computer.class (extends Player) --> if you wanted to create an AI this is where you would do so

もし私があなたなら、Javaゲーム開発についての本を読んだり、クラスを受講したりします。あなたがJavaを上手に使いたいのなら、それは始めるのに良い場所です。TicTacToeゲームを作成するなどの単純なタスクでも実行する必要がある、多くの重要な知識が不足しています。

于 2012-07-07T15:19:08.960 に答える