4

新しいセッションを開始するのに苦労しています。メニューオプションの新しいゲームを使用すると、新しいインスタンスが取得されます。実際には、現在のインスタンスを新しいインスタンスに置き換えたいだけです。私は多くの異なることを試みましたが、それでもそれを解決することはできません...

これが私のコードです:

package tictactoe;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
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.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TicTacToe2 extends JFrame implements ActionListener {

char[][] game = new char[3][3];
JButton[][] buttons = new JButton[3][3]; 

JButton menuItem      = new JButton();   
JMenu     menu        = new JMenu  ("TicTacToe");
JMenuItem newgame     = new JMenuItem("Start New Game"), 
          exit        = new JMenuItem("Exit"); 

TicTacToe2()
{
    super("Tic Tac Toe");
    setSize(500, 600);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
    setLayout( new BorderLayout());

    JPanel northPanel= new JPanel();
    northPanel.setLayout(new FlowLayout(FlowLayout.LEFT));  
    add("North", northPanel);

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout(3,3, 4, 4));
    add("Center", buttonPanel);
    Font font = new Font("Serif", Font.BOLD, 32);

    for (int row=0; row < game.length; row++)
    {
        for (int col =0; col < game[row].length; col++)
        {
            game[row][col] = ' ';
            JButton b = new JButton(" ");
            b.setFont(font);
            b.setBackground(Color.green);
            b.addActionListener(this);
            buttons[row][col] = b;
            buttonPanel.add(b);
        }
    } 

    menu.add(newgame);  
    menu.add(exit);
    newgame.addActionListener(this); 
    exit.addActionListener(this); 

    JMenuBar bar = new JMenuBar( ); 
    bar.add(menu);  
    setJMenuBar(bar); 

} 
public void actionPerformed(ActionEvent e) {
    Object  menusource = e.getSource();   
    if(menusource ==  newgame){ 
        new TicTacToe2();   
    }else if(menusource ==  exit){
        System.exit(0);
    }  
} 
public static void main(String[] args) {
    TicTacToe2 ttt = new TicTacToe2();

}
}
4

4 に答える 4

4

「新しいゲーム」のメニューを実行するたびに、次の方法で新しいインスタンスを作成します。

        new TicTacToe2();   

インスタンスをリセットするだけの場合reset()は、ゲームの状態を初期状態に設定するメソッドを記述します。

于 2012-10-27T16:22:36.027 に答える
2
public void actionPerformed(ActionEvent e) {
    Object  menusource = e.getSource();   
    if(menusource ==  newgame){ 
        new TicTacToe2();   //What are you doing with this object? 
                            //Should really be resetGame();
    }else if(menusource ==  exit){
        System.exit(0);
    }  
} 

問題になる可能性があります。新しいフレームインスタンスを取得する場所だと思います。配列をデフォルト値にクリアし、スコアをリセットするメソッドを用意してください。

于 2012-10-27T16:22:02.630 に答える
2

クラスの新しいインスタンスを作成する代わりに

new TicTacToe2();

[新しいゲーム]メニュー項目をクリックすると、ボタン配列のテキストをクリアして、新しいゲームの準備をすることができます。

for (int i=0; i < buttons.length; i++ ) {
   for (int j=0; j < buttons[0].length; j++ ) {
      buttons[i][j].setText(" ");
   }
}
于 2012-10-27T16:27:38.337 に答える
0

上記のように、すでに述べたように、次のような新しいインスタンスを作成する必要はありません。

new TicTacToe2()

だから、そこから続けていきます。ifステートメントは次のようになります

if (menusource == newgame) {
     getContentPane.removeAll();
     setContentPane(aFunc);
}

レイアウトを設定し、それにコンポーネントを追加する以下のような関数を作成します。そこに描画ロジックを配置し、setContentペインへの引数として指定します。例えば

 private JPanel aFunc() {
    custSelectPanel.setLayout(null);

    customerTable.setDragEnabled(false);
    customerTable.setFillsViewportHeight(true);

    ......

    cancelButton.setLocation(350, 0);
    cancelButton.setSize(100, 40);
    buttonPanel.add(cancelButton);

    return custSelectPanel;
}

ここでロジックを取得していることを願っています

于 2012-10-27T16:32:25.760 に答える