2013-08-05 34 views
3

我想用swing寫一個Tic Tac Toe程序,但是我似乎遇到了一些麻煩。在我的匿名內部類中,我嘗試爲每個按鈕設置actionListener,但是我無法找到允許我引用按鈕並將它們設置爲X或Y的類型或變量。我嘗試了e .getSource()。setText()在我的匿名類中,但返回錯誤。有什麼想法嗎? 謝謝! 亞歷在ActionListener裏面引用點擊JButton

import javax.swing.*; 

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

public class TicTacToe { 

public JFrame frame; 
public JLabel label; 
public JPanel panel; 

public static int counter; 



public void go() 
{ 
    frame = new JFrame("TicTacToe"); 
    frame.setSize(500, 500); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    panel = new JPanel(); 
    panel.setLayout(new GridLayout(3,3,10,10)); 
    frame.add(BorderLayout.CENTER, panel); 
    label= new JLabel("TIC TAC TOE"); 
    frame.add(BorderLayout.NORTH, label); 

    ; 


    JButton button1 = new JButton("Button 1"); 
    JButton button2 = new JButton("Button 1"); 
    JButton button3 = new JButton("Button 1"); 
    JButton button4 = new JButton("Button 1"); 
    JButton button5 = new JButton("Button 1"); 
    JButton button6 = new JButton("Button 1"); 
    JButton button7 = new JButton("Button 1"); 
    JButton button8 = new JButton("Button 1"); 
    JButton button9 = new JButton("Button 1"); 





    button1.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e) 
     { 


     } 
    }); 

    button2.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) 
     { 

     } 
    }); 

    button3.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) 
     { 

     } 
    }); 

    button4.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) 
     { 

     } 
    }); 

    button5.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) 
     { 

     } 
    }); 

    button6.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) 
     { 

     } 
    }); 

    button7.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) 
     { 

     } 
    }); 

    button8.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) 
     { 

     } 
    }); 

    button9.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) 
     { 

     } 
    }); 

    panel.add(button1); 
    panel.add(button2); 
    panel.add(button3); 
    panel.add(button4); 
    panel.add(button5); 
    panel.add(button6); 
    panel.add(button7); 
    panel.add(button8); 
    panel.add(button9); 
    frame.setVisible(true); 
    panel.setVisible(true); 

} 


public static void main(String[] args) 
{ 
    TicTacToe gui = new TicTacToe(); 
    gui.go(); 

} 


} 

回答

11

記住,ActionListener可以在許多不同類型的組件中使用,所以源參考被一般化。你需要轉換爲回期望值

button9.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) 
    { 
     Object source = e.getSource(); 
     if (source instanceof JButton) { 
      JButton btn = (JButton)source; 
      // Go ahead and do what you like 
     } 
    } 
}); 

雖然我知道你ActionListener,因爲它代表幾乎可以保證該Object的源類型將是一個JButton,我從來不喜歡盲目地轉換對象,但是這我

+1

非常感謝。我真的很感激這種幫助,而且這也創造了奇蹟。 – Amloelxer

+0

另一個問題。以這種方式確定贏家的最佳方式是什麼?假設我做了一個計數器並且有((JButton)源).setText(「O」);或((JButton)源).setText(「X」); – Amloelxer

+0

其實,最好的辦法是建立某種模型。這將允許您檢查遊戲狀態並確定選擇哪些單元格以及由誰選擇。每次玩家選擇一個按鈕,我都會更新模型。這個過程的一部分是爲模型檢查一個勝利者並且引發一個適當的事件 – MadProgrammer

2

如果您收到錯誤,那麼你應該發佈它們,但我假設這是因爲你沒有斷言源對象實際上是一個按鈕。對於你正在做的事情有兩個直接的解決方案。

第一個是因爲您只是爲每個按鈕添加一個動作偵聽器,您可以假定它是動作事件引用的對象。只要注意,按鈕要麼必須是一個實例變量或聲明爲final:

button1.addActionListener(new ActionListener(){ 
     @Override 
     public void actionPerformed(ActionEvent e) 
     { 
      button1.setText("X/Y"); 
     } 
    }); 

其次,這將解決您的錯誤的原因,是聲稱ActionEvent源對象其實是一個按鈕。

button1.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      if (e.getSource() instanceof JButton) { 
       ((JButton) e.getSource()).setText("X/Y"); 
      } 
     } 
    }); 
+1

我唯一要做的評論是確保你強調一個事實,即在你的第一個例子中'JButton'要麼是一個實例變量或'最後';) – MadProgrammer

+0

好點,謝謝。 –

1

我經歷了和清理你的代碼一點點:這是通過檢查源是JButton的實例來完成。我不太清楚爲什麼你的代碼有錯誤,但我沒有得到任何答案。我建議,因爲你有共同的代碼,像我在數組中一樣重用它。我還添加了一個boolean在每次點擊按鈕時在玩家之間切換。

最後,我建議在構造函數中或在構造函數調用的私有方法中設置JFrame(更復雜的用戶界面可能有很多代碼,並且分解代碼是維護代碼的好習慣)就像我下面展示的那樣。

import javax.swing.*; 

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

public class TicTacToe { 
    public static final boolean PLAYER_X = false; 
    public static final boolean PLAYER_O = true; 

    public static int counter; 

    private JFrame frame; 
    private JLabel label; 
    private JPanel panel; 
    private JButton[] buttons; 
    private boolean player; 

    public TicTacToe() { 
     frame = new JFrame("TicTacToe"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     panel = new JPanel(); 
     panel.setPreferredSize(new Dimension(500, 500)); 
     panel.setLayout(new GridLayout(3, 3, 10, 10)); 
     frame.add(BorderLayout.CENTER, panel); 

     label = new JLabel("TIC TAC TOE"); 
     frame.add(BorderLayout.NORTH, label); 

     /* Set the initial player turn to player X */ 
     player = PLAYER_X; 

     /* Create the JButtons */ 
     buttons = new JButton[9]; 

     /* Loop through and set all of them up */ 
     for (int i = 0; i < buttons.length; i++) { 
      buttons[i] = new JButton(); 
      buttons[i].addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        if (e.getSource() instanceof JButton) { 
         ((JButton)e.getSource()).setText(player ? "O" : "X"); /* Set button text */ 
         player = !player; /* Switch turns */ 
        } 
       } 
      }); 

      /* Add all of the buttons to the panel. */ 
      panel.add(buttons[i]); 
     } 

     /* Pack the frame to the contents. Basically a "fit to contents". */ 
     frame.pack(); 
    } 

    public void go() { 
     frame.setVisible(true); 
     panel.setVisible(true); 
    } 

    public static void main(String[] args) { 
     TicTacToe gui = new TicTacToe(); 
     gui.go(); 
    } 
}