2014-07-15 22 views
1

我正在嘗試創建一個簡單的Tic-Tac-Toe程序,它使用終端和遊戲之前的用戶界面。我很喜歡這個,所以請放輕鬆。當我嘗試在我調用的方法使用的ActionListener,我得到這個錯誤:Non static variable this cannot be referenced from a static context.如何在Java中使用ActionListener(this)命令

這裏是我的代碼:

import java.util.Random ; 
import java.util.Scanner ; 
import javax.swing.JOptionPane ; 
import javax.swing.JFrame ; 
import javax.swing.JPanel ; 
import java.util.InputMismatchException ; 
import java.awt.BorderLayout ; 
import java.awt.* ; 
import java.awt.event.* ; 
import javax.swing.JTextArea ; 
import javax.swing.JButton ; 
import javax.swing.JRadioButton ; 

class TicTacToe 
{ 
    public int inp1 ; 
    public int inp2 ; 

    public static void main(String []args) 
    { 
     popupintroscreen(); 

    } 
    public static void popupintroscreen() 
    { 

     JTextArea introtext = new JTextArea("Hello and welcome to TicTacToe v.1.0"); 
     introtext.setEditable(false); 
     introtext.setLineWrap(true); 
     introtext.setWrapStyleWord(true); 

     JButton startgamebutton = new JButton("Start Game"); 
     JButton.addActionListener(this); 



     JPanel content = new JPanel(new BorderLayout()); 
     content.add(introtext); 
     content.add(startgamebutton); 

     JFrame introscreen = new JFrame("Tic Tac Toe"); 
     introscreen.setSize(400,400); 
     introscreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     introscreen.setLocationRelativeTo(null); 
     introscreen.add(content); 
     introscreen.setVisible(true); 




    } 
}` 

先謝謝了!

+6

'this'指的是當前對象上下文,但你在其中定義未在'static'方法是有一個對象上下文,所以'this'不存在。 「this」引用的對象(如果首先解決靜態問題)還需要實現ActionListener接口和任何必需的方法,然後才能按照您的需要使用它。 – JonK

+0

好的,謝謝JonK,但你可以用簡單的術語解釋一下嗎?我是一個真正的新手:( – RoboticMoneylender

+1

你知道靜態關鍵字是什麼意思嗎?如果你是面嚮對象語言的新手,那麼你可能會很難找到自己的頭腦 – JonK

回答

1

如果我明白你的問題,然後在這裏,當你定義你的類

class TicTacToe 

您還需要指定TicTacToe實現ActionListener接口(和實現它有一個方法);

class TicTacToe implements ActionListener { 
    @Override 
    public void actionPerformed(ActionEvent e) { 
    System.out.println("TicTacToe.actionPerformed: " + e); 
    } 
    public static void main(String []args) 
    { 
    new TicTacToe().popupintroscreen(); 
    } 
    public void popupintroscreen() { // <-- not static. 
    // ... 
    } 
} 

此外,使用在thispopupintroscreen它不能是一個靜態方法。最後,你需要一個TicTacToe的實例。

+0

我不太明白你在那裏做了什麼。我是否應該將此添加到我的代碼中,並且是必需的SOP?我也想知道@Override的功能是什麼。感謝你的回答。 – RoboticMoneylender

+0

我加了一點解釋。將SOP替換爲您想要在Action上發生的事情。 Override確保你實際實現了* super *方法。如果你得到的簽名是錯誤的,並且不執行正確的方法,那就是所謂的壞事。 –

+1

在這種情況下,這可能不是問題,但請注意,在接口指定的方法上使用'@ Override'註釋是Java 1.6之前的編譯時錯誤。 – JonK

1

那麼,寫代碼的方式本質上是錯誤的。

您在這裏沒有定義任何ActionListener。只需調用addActionListener(this)將不起任何作用。你需要實現ActionListener。替換行 JButton.addActionListener(this); // you have to use the object of JButton - i.e. startgamebutton, not the JButton class here! addActionListener is not a static method of JButton

這種(使用匿名內部類):

startgamebutton.addActionListener(new ActionListener() { 
@Override 
    public void actionPerformed(ActionEvent e) { 
    System.out.println("In actionPerformed"); 
    // other code to handle the event when the button is clicked 
    } 
}); 
+0

啊,謝謝你對JButton的更正,只是我有點心不在焉。感謝代碼人,真的把它解決了。在ActionEvent之後我不必使用e,對吧?因爲我喜歡解釋,儘管冗長,變數的名字。我還在學習。 – RoboticMoneylender

+1

是'e'只是一個參數名稱。根據Java變量命名規則,您可以將其命名爲任何內容。乾杯! – Pat

相關問題