java - Trying to change image when JButton pressed -
i trying change image on panel when of jbuttons pressed. have set array of images , need change next image in array once has been pressed. here code:
public class simplegui implements actionlistener { jbutton button = new jbutton("very happy"); jbutton buttontwo = new jbutton("happy"); jbutton buttonthree = new jbutton("neutral"); jbutton buttonfour = new jbutton("sad"); jbutton buttonfive = new jbutton("very sad"); static int[] buttonarray = new int[5]; private static string[] imagelist = { "res/snow.jpg", "res/test-gm.jpg" }; public int = 0; public static void main(string[] args) throws filenotfoundexception { simplegui gui = new simplegui(); gui.go(); file file = new file("out.txt"); fileoutputstream fos = new fileoutputstream(file); printstream ps = new printstream(fos); system.setout(ps); buttonarray[0] = 0; buttonarray[1] = 0; buttonarray[2] = 0; buttonarray[3] = 0; buttonarray[4] = 0; } public void go() { jframe frame = new jframe(); jpanel panel = new jpanel(); panel.setbackground(color.darkgray); panel.setlayout(new boxlayout(panel, boxlayout.y_axis)); button.addactionlistener(this); buttontwo.addactionlistener(this); buttonthree.addactionlistener(this); buttonfour.addactionlistener(this); buttonfive.addactionlistener(this); panel.add(button); panel.add(buttontwo); panel.add(buttonthree); panel.add(buttonfour); panel.add(buttonfive); frame.getcontentpane().add(borderlayout.east, panel); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(650, 600); frame.setvisible(true); imageicon image = new imageicon(imagelist[i]); imageicon image1 = new imageicon(imagelist[i + 1]); jlabel label = new jlabel("", image, jlabel.center); jpanel panel2 = new jpanel(new borderlayout()); panel2.add(label, borderlayout.center); panel2.setlayout(new boxlayout(panel2, boxlayout.y_axis)); frame.add(panel2, borderlayout.center); frame.setvisible(true); } public void actionperformed(actionevent event) { if (event.getsource() == button) { buttonarray[0] = 1; buttonarray[1] = 0; buttonarray[2] = 0; buttonarray[3] = 0; buttonarray[4] = 0; system.out.println("very happy"); } // buttontwo = (jbutton) event.getsource(); if (event.getsource() == buttontwo) { buttonarray[0] = 0; buttonarray[1] = 1; buttonarray[2] = 0; buttonarray[3] = 0; buttonarray[4] = 0; system.out.println("happy"); } // buttonthree = (jbutton) event.getsource(); if (event.getsource() == buttonthree) { buttonarray[0] = 0; buttonarray[1] = 0; buttonarray[2] = 1; buttonarray[3] = 0; buttonarray[4] = 0; system.out.println("neutral"); } // buttonfour = (jbutton) event.getsource(); if (event.getsource() == buttonfour) { buttonarray[0] = 0; buttonarray[1] = 0; buttonarray[2] = 0; buttonarray[3] = 1; buttonarray[4] = 0; system.out.println("sad"); } // buttonfive = (jbutton) event.getsource(); if (event.getsource() == buttonfive) { buttonarray[0] = 0; buttonarray[1] = 0; buttonarray[2] = 0; buttonarray[3] = 0; buttonarray[4] = 1; system.out.println("very sad"); } // system.out.println(arrays.tostring(buttonarray)); // imageicon image = (imagelist[i]); } }
i don't see parts of code supposed do. instead, here's minimal example should asking about: 1 label, , 2 buttons setting different images label.
imageicon[] images = new imageicon[] { new imageicon("foo.gif"), new imageicon("bar.gif"), new imageicon("blub.gif") }; jframe frame = new jframe("test"); frame.getcontentpane().setlayout(new flowlayout()); jlabel label = new jlabel(images[0]); frame.getcontentpane().add(label); jbutton button1 = new jbutton("image 1"); button1.addactionlistener(e -> label.seticon(images[0])); frame.getcontentpane().add(button1); jbutton button2 = new jbutton("image 2"); button2.addactionlistener(e -> label.seticon(images[1])); frame.getcontentpane().add(button2); frame.pack(); frame.setvisible(true);
note using lambda functions (java 8) can same 1 or more "real" actionlistener
classes. important part call label.seticon(theimage)
; part seems missing in code.
if instead want cycle through list or array of pictures, can this:
atomicinteger index = new atomicinteger(0); jbutton buttoncycle = new jbutton("cycle"); buttoncycle.addactionlistener(e -> label.seticon(images[index.getandincrement() % images.length])); frame.getcontentpane().add(buttoncycle);
here, atomicinteger
used can declare local variable , use in lambda. can use regular int
if make member variable of surrounding class.
private int c = 0; ... buttoncycle.addactionlistener(e -> label.seticon(images[c++ % images.length]));
the takeaway is: create counter variable, increment each time button called , set label's icon element count, module size of array.
Comments
Post a Comment