2014-05-12 40 views
0

我有一個GUI在我的主類文件在另一個類文件

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

public class TicTac extends JFrame { 
TicTacEvent tictac = new TicTacEvent(this); 
JPanel row1 = new JPanel(); 
JButton[][] boxes = new JButton[4][4]; 
JButton play = new JButton("Play"); 
JButton restart = new JButton("Restart"); 
JTextField blank1 = new JTextField(); 
JTextField blank2 = new JTextField(); 
JOptionPane win = new JOptionPane("Winner"); 
ImageIcon back = new ImageIcon("cardback.jpg"); 

public TicTac() { 
    super ("Tic Tac Toe"); 
    setSize (800,650); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    FlowLayout layout = new FlowLayout(); 
    setLayout(layout); 
    int name = 0; 
    String newname; 

    GridLayout layout1 = new GridLayout(5, 4, 10, 10); 
    row1.setLayout(layout1); 
    for (int x=0; x<=3; x++){ 
     for (int y=0; y<=3; y++){ 
      name = name + 1; 
      newname = Integer.toString(name); 
      boxes[x][y] = new JButton(newname); 
      boxes[x][y].setIcon(back); 
      row1.add(boxes[x][y]); 
     } 
    } 
    row1.add(blank1); 
    row1.add(play); 
    row1.add(blank2); 
    row1.add(restart); 
    add (row1); 

    play.addActionListener(tictac); 
    for (int x=0; x<=3; x++){ 
     for (int y=0; y<=3; y++){ 
      boxes[x][y].addActionListener(tictac); 
     } 
    } 


    setVisible(true); 
} 

public static void main(String[] arguments){ 
    TicTac frame = new TicTac(); 
} 
} 

創建重置的JPanel,我一直在使用它的代碼在這裏

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



public class TicTacEvent implements ItemListener, ActionListener, Runnable { 

TicTac gui; 
Thread playing; 
Thread restarting; 
ImageIcon a = new ImageIcon("x.jpg"); 
ImageIcon b = new ImageIcon("o.jpg"); 
int clicks = 0; 
int win = 0; 
int winx = 0; 
int winy = 0; 
int cat = 0; 

int[][] check = new int[4][4]; 

public TicTacEvent (TicTac in){ 

    gui = in; 
    for (int row=0; row<=3; row++){ 
    for (int col=0; col<=3; col++){ 
      check[row][col]=0; 
     } 
    } 
} 
public void actionPerformed(ActionEvent event){ 

    String command = event.getActionCommand(); 

if (command.equals("Play")) { 
startPlaying(); 
} else if (command.equals("Restart")) { 
restart(); 
} 

    if (command.equals("1")) { 
     b1(); 
    } 
    if (command.equals("2")) { 
     b2(); 
    } 
    if (command.equals("3")) { 
     b3(); 
    } 
    if (command.equals("4")) { 
     b4(); 
    } 
    if (command.equals("5")) { 
     b5(); 
    } 
    if (command.equals("6")) { 
     b6(); 
    } 
    if (command.equals("7")) { 
     b7(); 
    } 
    if (command.equals("8")) { 
     b8(); 
    } 
    if (command.equals("9")) { 
     b9(); 
    } 
    if (command.equals("10")) { 
     b10(); 
    } 
    if (command.equals("11")) { 
     b11(); 
    } 
    if (command.equals("12")) { 
     b12(); 
    } 
    if (command.equals("13")) { 
     b13(); 
    } 
    if (command.equals("14")) { 
     b14(); 
    } 
    if (command.equals("15")) { 
     b15(); 
    } 
    if (command.equals("16")) { 
     b16(); 
    } 



    gui.blank1.setText("X Wins: " + winx + " Y Wins:" + winy); 
    gui.blank2.setText("Cat Wins(Tie):" + cat); 

} 

void b1() { 
    clicks = clicks + 1; 
    if ((clicks%2)==1){ 
     gui.boxes[0][0].setIcon(a); 
     check[0][0] = 1; 
    } else { 
     gui.boxes[0][0].setIcon(b); 
     check[0][0] = 2; 
    } 
    winner(); 

} 
void b2() { 
    clicks = clicks + 1; 
    if ((clicks%2)==1){ 
     gui.boxes[0][1].setIcon(a); 
     check[0][1] = 1; 
    } else { 
     gui.boxes[0][1].setIcon(b); 
     check[0][1] = 2; 
    } 
    winner(); 
} 
void b3() { 
    clicks = clicks + 1; 
    if ((clicks%2)==1){ 
     gui.boxes[0][2].setIcon(a); 
     check[0][2] = 1; 
    } else { 
     gui.boxes[0][2].setIcon(b); 
     check[0][2] = 2; 
    } 
    winner(); 
} 
void b4() { 
    clicks = clicks + 1; 
    if ((clicks%2)==1){ 
     gui.boxes[0][3].setIcon(a); 
     check[0][3] = 1; 
    } else { 
     gui.boxes[0][3].setIcon(b); 
     check[0][3] = 2; 
    } 
    winner(); 
} 
void b5() { 
    clicks = clicks + 1; 
    if ((clicks%2)==1){ 
     gui.boxes[1][0].setIcon(a); 
     check[1][0] = 1; 
    } else { 
     gui.boxes[1][0].setIcon(b); 
     check[1][0] = 2; 
    } 
    winner(); 
} 
void b6() { 
    clicks = clicks + 1; 
    if ((clicks%2)==1){ 
     gui.boxes[1][1].setIcon(a); 
     check[1][1] = 1; 
    } else { 
     gui.boxes[1][1].setIcon(b); 
     check[1][1] = 2; 
    } 
    winner(); 
} 
void b7() { 
    clicks = clicks + 1; 
    if ((clicks%2)==1){ 
     gui.boxes[1][2].setIcon(a); 
     check[1][2] = 1; 
    } else { 
     gui.boxes[1][2].setIcon(b); 
     check[1][2] = 2; 
    } 
    winner(); 
} 
void b8() { 
    clicks = clicks + 1; 
    if ((clicks%2)==1){ 
     gui.boxes[1][3].setIcon(a); 
     check[1][3] = 1; 
    } else { 
     gui.boxes[1][3].setIcon(b); 
     check[1][3] = 2; 
    } 
    winner(); 
} 
void b9() { 
    clicks = clicks + 1; 
    if ((clicks%2)==1){ 
     gui.boxes[2][0].setIcon(a); 
     check[2][0] = 1; 
    } else { 
     gui.boxes[2][0].setIcon(b); 
     check[2][0] = 2; 
    } 
    winner(); 
} 
    void b10() { 
    clicks = clicks + 1; 
    if ((clicks%2)==1){ 
     gui.boxes[2][1].setIcon(a); 
     check[2][1] = 1; 
    } else { 
     gui.boxes[2][1].setIcon(b); 
     check[2][1] = 2; 
    } 
    winner(); 
} 
      void b11() { 
    clicks = clicks + 1; 
    if ((clicks%2)==1){ 
     gui.boxes[2][2].setIcon(a); 
     check[2][2] = 1; 
    } else { 
     gui.boxes[2][2].setIcon(b); 
     check[2][2] = 2; 
    } 
    winner(); 
} 
       void b12() { 
    clicks = clicks + 1; 
    if ((clicks%2)==1){ 
     gui.boxes[2][3].setIcon(a); 
     check[2][3] = 1; 
    } else { 
     gui.boxes[2][3].setIcon(b); 
     check[2][3] = 2; 
    } 
    winner(); 
} 
    void b13() { 
    clicks = clicks + 1; 
    if ((clicks%2)==1){ 
     gui.boxes[3][0].setIcon(a); 
     check[3][0] = 1; 
    } else { 
     gui.boxes[3][0].setIcon(b); 
     check[3][0] = 2; 
    } 
    winner(); 
} 
    void b14() { 
    clicks = clicks + 1; 
    if ((clicks%2)==1){ 
     gui.boxes[3][1].setIcon(a); 
     check[3][1] = 1; 
    } else { 
     gui.boxes[3][1].setIcon(b); 
     check[3][1] = 2; 
    } 
    winner(); 
} 
     void b15() { 
    clicks = clicks + 1; 
    if ((clicks%2)==1){ 
     gui.boxes[3][2].setIcon(a); 
     check[3][2] = 1; 
    } else { 
     gui.boxes[3][2].setIcon(b); 
     check[3][2] = 2; 
    } 
    winner(); 
} 
    void b16() { 
    clicks = clicks + 1; 
    if ((clicks%2)==1){ 
     gui.boxes[3][3].setIcon(a); 
     check[3][3] = 1; 
    } else { 
     gui.boxes[3][3].setIcon(b); 
     check[3][3] = 2; 
    } 
    winner(); 
} 

public void winner() { 
    /** Check rows for winner */ 

    for (int x=0; x<=3; x++){ 
     if ((check[x][0]==check[x][1])&&(check[x][0]==check[x][2]) && (check[x][0]== check[x][3])) { 

      if (check[x][0] ==1) { 
       JOptionPane.showMessageDialog(null, "X is the winner"); 
       win = 1; 
       winx +=1; 

      } else if (check[x][0]==2){ 
       JOptionPane.showMessageDialog(null, "Y is the winner"); 
       win = 1; 
       winy +=1; 
      } 
     } 
    } 

    /** Check columns for winner */ 
    for (int x=0; x<=3; x++){ 
     if ((check[0][x]==check[1][x])&&(check[0][x]==check[2][x])&& (check[0][x]== check[3][x])) { 
      if (check[0][x]==1) { 
       JOptionPane.showMessageDialog(null, "X is the winner"); 
       win = 1; 
       winx +=1; 


      } else if (check[0][x]==2) { 
       JOptionPane.showMessageDialog(null, "Y is the winner"); 
       win = 1; 
       winy +=1; 

      } 
     } 
    } 
     if ((check[1][1]== 1) && (check[3][3] == 1) && (check[2][2]==1) && (check[0][0]==1) 
      || (check[3][0]==1) &&(check[1][2]==1) &&(check[0][3]==1)) 
     { 
      JOptionPane.showMessageDialog(null, "X is the winner"); 
      win = 1; 
      winx +=1; 

     } else if ((check[1][1]== 2) && (check[3][3] == 2) && (check[2][2]==2) &&   (check[0][0]==2) 
      || (check[3][0]==2) &&(check[1][2]==2) &&(check[0][3]==2)) { 
      JOptionPane.showMessageDialog(null, "Y is the winner"); 
      win = 1; 
      winy +=1; 

     } 

    //} 

    /** Checks if the game is a tie */ 
    if ((clicks==16) && (win==0)) { 
     JOptionPane.showMessageDialog(null, "The game is a tie"); 
     cat =+1; 

    } 


} 

public void startPlaying() { 
    playing = new Thread(this); 
    playing.start(); 
    gui.play.setEnabled(false); 
} 
public void restart() { 
TicTac restartok = new TicTac(); 
restartok.row1.add(restartok.blank1); 
restartok.row1.add(restartok.play); 
restartok.row1.add(restartok.blank2); 
restartok.row1.add(restartok.restart); 
restartok.add (restartok.row1); 

restartok.play.addActionListener(restartok.tictac); 
for (int x=0; x<=3; x++){ 
    for (int y=0; y<=3; y++){ 
     restartok.boxes[x][y].addActionListener(restartok.tictac); 
    } 
} 
} 


public void itemStateChanged(ItemEvent e) { 
// throw new UnsupportedOperationException("Not supported yet."); 
} 

public void run() { 
//restart(); 
    //throw new UnsupportedOperationException("Not supported yet."); 

} 


} 

但是我的問題是我怎麼會重置GUI,我有一些想法..

構造函數「Public TicTac」創建gui,但我不知道如何通過另一個類文件再次發生。我OOP的理解是,有一個構造函數,我可以通過這樣assumedly創建一個對象

TicTac restartok = new TicTac(); 

打電話,你會認爲我會創造另一個的JPanel/GUI每次我叫重啓();方法,不是?

我的OOP exirence是有限的,所以我沒有線索在哪裏開始或找出什麼邏輯是不正確的。謝謝。

+0

您正通過構造函數將TicTac對象傳遞給TicTacEvent。我認爲,您可以通過重新啓動功能重新使用同一個對象。我對此沒有把握,創建事件和EventListener檢查觀察者模式將對您有所幫助 –

+0

如果您創建GUI模型並將其與GUI視圖分開,那麼您可以通過重新初始化模型字段來重新啓動GUI模型。沒有必要重新創建視圖。該模式被稱爲[模型/視圖/控制器模式](http://en.wikipedia.org/wiki/Model_View_Controller)。 –

+0

@PramodPP你能澄清一下嗎? – Alivesi

回答

0

就可以重置遊戲,只是重置clicks爲0和按鈕到其原始狀態,就像它們創建:

boxes[x][y] = new JButton(newname); 
boxes[x][y].setIcon(back); 

除此之外,你有許多的東西在你的代碼來解決。這裏有一些:

  • 您的行boxes[x][y].addActionListener(tictac);是在它自己的循環中它應該在初始循環中。沒有理由在相同的數組上迭代兩次。
  • 你的佈局決定可能會更好(取決於你想要得到的究竟是)。
  • invokeLater方法中使用TicTac frame = new TicTac();啓動GUI。
  • 爲每個具有獨特功能的按鈕創建一個ActionListener。在你的情況下,播放1,重啓1,所有板按鈕1。
  • 您正在使用event.getActionCommand,但您沒有在任何按鈕上撥打setActionCommand
  • 當您檢查按鈕(動作命令名稱),使用if else或更好 - 使用switch語句時,不要使用多個if
  • 請勿爲每個按鈕使用單獨的功能。使用1個接收按鈕作爲參數的函數。
  • 當您可以創建本地變量時,請勿創建字段。

如果您需要澄清請給我留言。

編輯:這是一些修改過的代碼,重置按鈕(我無法阻止自己糾正代碼的其他部分,雖然還有更多的事情要做)。

public class TicTac extends JFrame { 

    static JButton[][] boxes = new JButton[4][4]; 
    static int[][] check = new int[4][4]; 
    static JTextField blank1 = new JTextField(); 
    static JTextField blank2 = new JTextField(); 
    static int turns = 1; 

    static ImageIcon back = new ImageIcon("cardback.jpg"); 
    static ImageIcon x = new ImageIcon("x.jpg"); 
    static ImageIcon o = new ImageIcon("o.jpg"); 

    public TicTac() { 

     JPanel gamePanel = new JPanel(new GridLayout(5, 4, 10, 10)); 

     for (int x = 0; x<=3; x++) { 
      for (int y = 0; y<=3; y++) { 
       boxes[x][y] = new JButton(""+x+y, back); 
       boxes[x][y].addActionListener(new XOActionListenr(x, y)); 
       gamePanel.add(boxes[x][y]); 
      } 
     } 

     JButton play = new JButton("Play"); 
     JButton restart = new JButton("Restart"); 
     play.addActionListener(new PlayActionListenr()); 
     restart.addActionListener(new RestartActionListenr()); 

     setLayout(new FlowLayout()); 
     gamePanel.add(blank1); 
     gamePanel.add(play); 
     gamePanel.add(blank2); 
     gamePanel.add(restart); 
     add(gamePanel); 

     setTitle("Tic Tac Toe"); 
     pack(); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setVisible(true); 
    } 

    private void winner() { 

     // Check for winner 
    } 

    private class XOActionListenr implements ActionListener { 

     private int row, col; 

     private XOActionListenr(int row, int col) { 

      this.row = row; 
      this.col = col; 
     } 

     public void actionPerformed(ActionEvent e) { 

      if (turns % 2 == 0) { 
       boxes[row][col].setText("O"); 
       boxes[row][col].setIcon(o); 
       check[row][col] = 2; 
      } 
      else { 
       boxes[row][col].setText("X"); 
       boxes[row][col].setIcon(x); 
       check[row][col] = 1; 
      } 
      winner(); 
      turns++; 
     } 
    } 

    private class PlayActionListenr implements ActionListener { 

     public void actionPerformed(ActionEvent e) { 

      // Not sure why you would need the "play" button at all, 
      // but insert here what it's supposed to do 
     } 
    } 

    private class RestartActionListenr implements ActionListener { 

     public void actionPerformed(ActionEvent e) { 

      for (int x = 0; x<=3; x++) { 
       for (int y = 0; y<=3; y++) { 
        boxes[x][y].setText(""+x+y); 
        boxes[x][y].setIcon(back); 
       } 
      } 
     } 
    } 

    public static void main(String[] arguments) { 

     SwingUtilities.invokeLater(new Runnable() { 

      public void run() { 

       new TicTac(); 
      } 
     }); 
    } 
} 
+0

@ user18035551感謝您付出努力,您的評論。我試過在我的重啓();方法已經 'clicks = 0; int name = 0; 字符串新名稱; (int y = 0; y <= 3; y ++){ name = name + 1;(int x = 0; x <= 3; x ++){ newname = Integer.toString(name); gui.boxes [x] [y] = new JButton(newname); gui.boxes [x] [y] .setIcon(back);然而,它並沒有經過,我添加了'else throw IllegalArgumentException(「未知的命令:」+命令);然後,我們添加了一個新的IllegalArgumentException(「未知的命令:」+命令)。 }''來檢查它是否沒有經過,但它似乎是。 – Alivesi

+0

謝謝,我會慢慢嘗試解決你所說的,具有諷刺意味的是,這是我的老師代碼,不是我的,我的任務是增加贏/輸計數器和板的大小。有點卡住Catch 22,不能多學習,因爲沒有交給這個看未來的課,哈哈。 – Alivesi

+0

@Alivesi如果帖子中的代碼是你的老師的代碼,請立即停止上課,並提出無能(不是開玩笑)的投訴。至於代碼,你不需要再次添加按鈕,只需設置現有的文本和圖像。 – user1803551

0

您不需要一次又一次重新創建整個GUI。你需要做的是清除文本域以及用戶迄今爲止所做的任何選擇。因此,您需要創建一個單獨的方法,在其中您將重置GUI中每個按鈕/文本字段的狀態。

例如:要清除textfield您將撥打setText("")方法。

希望這會有所幫助。

+0

對於按鈕,它們被設置爲圖片,這仍然會工作嗎? I'ltry – Alivesi

+0

是的,您可以重置您的按鈕上的圖片以及 – Sanjeev

相關問題