Sunday, 15 May 2011

swing - Add click event to JButton in gridlayout - JAVA -



swing - Add click event to JButton in gridlayout - JAVA -

update:

thanks helpful people, solution has been found.

i creating buttons below, , adding event:

for(string s: new string[]{"7", "8", "9", "4", "5", "6", "1", "2","3", "0", ".", "=" }) {// alter here, per need jbutton btn = new jbutton(s); numbuttonpanel.add(btn); btn.addactionlistener(new eventlistener()); }

note:

eventlistener class created (scroll downwards answers , can see class there)

i creating calculator using java swing libraries, have numerical buttons within gridlayout.

my question is, given below code, how add together click events jbuttons?

example of jbuttons:

numbuttonpanel.add(new jbutton("8")); numbuttonpanel.add(new jbutton("9")); numbuttonpanel.add(new jbutton("4")); numbuttonpanel.add(new jbutton("5")); numbuttonpanel.add(new jbutton("6"));

as can see, creating them within gridlayout...i not adding created jbuttons.

if help, great!

thanks!

try using loop:

for(string s: new string[]{"8", "9", "4", "5"}) {// alter here, per need jbutton btn = new jbutton(s); numbuttonpanel.add(btn); btn.addactionlistener(new youractionlistener());//<-- alter here, per need }

java swing

No comments:

Post a Comment