Cj1m
Cj1m

Reputation: 815

ActionListener Problems in Java

I have been trying to fix my code for a few hours and I still can't get rid of this error. In the code below, One.addActionListener(this) and Two.addActionListener(this) both have red lines under this saying 'Cannot use this in a static context'. Please help me out if you can. Thanks!

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

Upvotes: 1

Views: 299

Answers (2)

sanbhat
sanbhat

Reputation: 17622

You cannot use this in a static method (in your case main method). Because, this represents current instance of the class on which the method is getting executed.

Since static methods are such methods which can be invoked without an instance, this doesn't always makes sense. So java gives compilation error Cannot use this in a static context

you should change your code to

One.addActionListener(main); 

Since main is an already created instance

Upvotes: 4

BackSlash
BackSlash

Reputation: 22233

Within a static method you can only use static variables or local variables, you can't use instance variables, and this represents a TheMain instance, so you can't use it in the main method, which is static. You should change

addActionListener(this);

to

addActionListener(main);

Upvotes: 3

Related Questions