i have been trying fix code few hours , still can't rid of error. in code below, one.addactionlistener(this) , two.addactionlistener(this) both have red lines under this saying 'cannot use in static context'. please me out if 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 1 = new jbutton("1"); one.setsize(10, 10); one.addactionlistener(this); jbutton 2 = new jbutton("2"); two.setsize(10, 10); two.addactionlistener(this); } public themain(){ jbutton 1 = new jbutton("1"); one.setsize(10, 10); one.addactionlistener(this); jbutton 2 = 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 } }
within static method can use static variables or local variables, can't use instance variables, , this represents themain instance, can't use in main method, static. should change
addactionlistener(this); to
addactionlistener(main);
Comments
Post a Comment