2013-07-19 42 views
1

我一直在努力解決我的代碼幾個小時,我仍然無法擺脫這個錯誤。在下面的代碼中,One.addActionListener(this)Two.addActionListener(this)都在this的下方有紅線,表示'不能在靜態上下文中使用它'。如果可以,請幫助我。謝謝!Java中的ActionListener問題

import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import java.awt.event.WindowEvent; 
    import java.awt.event.WindowListener;  
    import javax.swing.JButton; 
    import javax.swing.JFrame; 

    public class TheMain extends JFrame implements ActionListener, WindowListener { 

     int input1 = 0; 
     int input2 = 0; 

     public static void main(String[] args) { 
      TheMain main = new TheMain(); 
      JButton One = new JButton("1"); 
      One.setSize(10, 10); 
      One.addActionListener(this);  
      JButton Two = new JButton("2"); 
      Two.setSize(10, 10); 
      Two.addActionListener(this);  
    } 

    public TheMain(){ 
    JButton One = new JButton("1"); 
    One.setSize(10, 10); 
    One.addActionListener(this);    

    JButton Two = new JButton("2"); 
    Two.setSize(10, 10); 
    Two.addActionListener(this);   

    JFrame frame = new JFrame("window"); 
    frame.setSize(200, 250); 
    frame.setVisible(true); 
    frame.add(One); 
    frame.add(Two); 
    } 

    public void actionPerformed(ActionEvent e) { 
     if(input1 != 0){ 
      if(input2 != 0){ 
       System.out.println("Max 2 numbers!"); 
      }else{ 
       input2 = 1; 
      } 
     }else{ 
      input1 = 1; 
     }   
    } 

    public void actionPerformed1(ActionEvent e) { 
     if(input1 != 0){ 
      if(input2 != 0){ 
       System.out.println("Max 2 numbers!"); 
      }else{ 
       input2 = 2; 
      } 
     }else{ 
      input1 = 2; 
     }   
    } 

    @Override 
    public void windowOpened(WindowEvent e) { 
     // TODO Auto-generated method stub  
    } 

    @Override 
    public void windowClosing(WindowEvent e) { 
     // TODO Auto-generated method stub  
    } 

    @Override 
    public void windowClosed(WindowEvent e) { 
     // TODO Auto-generated method stub  
    } 

    @Override 
    public void windowIconified(WindowEvent e) { 
     // TODO Auto-generated method stub  
    } 

    @Override 
    public void windowDeiconified(WindowEvent e) { 
     // TODO Auto-generated method stub  
    } 

    @Override 
    public void windowActivated(WindowEvent e) { 
     // TODO Auto-generated method stub  
    } 

    @Override 
    public void windowDeactivated(WindowEvent e) { 
     // TODO Auto-generated method stub  
    }  
} 
+3

瞭解更多關於面向對象的編程。 'this'必須指向靜態上下文中沒有實例的當前類的實例。 –

+1

只需在''this'處寫'main'來代替'this',因爲您已經在'main'方法內引用了該類:-) –

+1

您已經在構造函數中創建按鈕,那麼爲什麼要再次創建它們?你的類擴展了JFrame,所以你爲什麼要創建一個新的JFrame來添加按鈕? – ChadNC

回答

3

在你可以只使用靜態變量或局部變量的靜態方法,你可以不使用實例變量,並且this代表TheMain實例,因此您不能在main方法中使用它,即static。你應該改變

addActionListener(this); 

addActionListener(main); 
+0

謝謝,當我運行這個時,只有一個大的2個按鈕,但沒有1個按鈕。你知道我該如何解決這個問題嗎? – Cj1m

+0

@ user1952565這是由於佈局經理。閱讀[如何使用佈局管理器](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) – BackSlash

+0

好吧,我已經解決了,但一個和兩個按鈕都與同一個actionlister ,我該如何解決這個問題? – Cj1m

4

你不能在一個static方法使用this(你的情況main法)。因爲,this表示當前實例該方法正在執行。

由於static方法是這樣的方法,可以調用沒有實例,this並不總是有意義的。所以Java提供了編譯錯誤Cannot use this in a static context

你應該更改您的代碼

One.addActionListener(main); 

由於main是一個已經創建的實例

+0

謝謝,當我運行這個時,只有一個大的2個按鈕,但沒有1個按鈕。你知道我該如何解決這個問題嗎? – Cj1m

+0

@ user1952565:'JFrame'的默認佈局是'BorderLayout',所以當你編寫'frame.add(One)'時,它在內部被視爲'frame.add(One,BorderLayout.CENTER)',因此你添加了兩個組件在同一位置,這是不可能的,因爲在任何給定的時間只有一個組件可以被添加到任何位置。所以試試這個'frame.add(One,BorderLayout.PAGE_START);和frame.add(兩個,BorderLayout.PAGE_END);'然後'frame.pack(),然後是frame.setVisible(true)'。閱讀[LayoutManagers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) –

+0

好的,謝謝我已經解決了,但一個和兩個按鈕都是同一個actionlister,如何可以我解決這個問題? – Cj1m