1

我已经尝试修复我的代码几个小时,但我仍然无法摆脱这个错误。在下面的代码中,在“不能在静态上下文中使用它”One.addActionListener(this)下方Two.addActionListener(this)都有红线。this如果可以的话,请帮助我。谢谢!

    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    
    }    
}
4

2 回答 2

4

您不能thisstatic方法中使用(在您的案例main方法中)。因为,this表示正在执行该方法的类的当前实例。

由于static方法是可以在没有实例的情况下调用的方法, 因此this并不总是有意义。所以java给出编译错误Cannot use this in a static context

您应该将代码更改为

One.addActionListener(main); 

因为main是一个已经创建的实例

于 2013-07-19T12:07:33.117 回答
3

在静态方法中只能使用静态变量或局部变量,不能使用实例变量,并且this表示一个TheMain实例,所以不能在main方法中使用,即static. 你应该改变

addActionListener(this);

addActionListener(main);
于 2013-07-19T12:07:50.120 回答