image - Creating a java swing album -
i'd create java swing photo album can't manage find right way it. think should create 2 arraylist, 1 stock photo objects , 1 stock buttons.
after should find way assign each images buttons , add them panel.
my question : think right way , if so, give me hint? (for last class @ bottom)
here's code @ moment :
main :
import javax.swing.jframe; public class main extends jframe { public static void main(string[] args) { javax.swing.swingutilities.invokelater(new runnable() { public void run() { new albumframe(); } }); } }
albumframe :
import java.awt.borderlayout; import javax.swing.imageicon; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; public class albumframe extends jframe { private jpanel menupanel; private jpanel photopanel; public albumframe(){ super("jphone"); setlayout(new borderlayout()); photopanel = new photopanel(); add(photopanel,borderlayout.center); menupanel = new menupanel(); add(menupanel,borderlayout.south); setsize(480,800); setdefaultcloseoperation(jframe.exit_on_close); setvisible(true); } }
menupanel
import java.awt.dimension; import java.awt.flowlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.io.file; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jfilechooser; import javax.swing.jpanel; import javax.swing.filechooser.filenameextensionfilter; public class menupanel extends jpanel implements actionlistener{ private static final long serialversionuid = 1l; private jbutton backbutton; private jbutton homebutton; private jbutton turnbutton; private jbutton addbutton; final private jfilechooser fc; public menupanel(){ fc = new jfilechooser(); backbutton = new jbutton(new imageicon(getclass().getresource("/images/back.png"))); homebutton = new jbutton(new imageicon(getclass().getresource("/images/home.png"))); turnbutton = new jbutton(new imageicon(getclass().getresource("/images/turn.png"))); addbutton = new jbutton(new imageicon(getclass().getresource("/images/add.png"))); backbutton.setpreferredsize(new dimension(55,55)); homebutton.setpreferredsize(new dimension(55,55)); turnbutton.setpreferredsize(new dimension(55,55)); addbutton.setpreferredsize(new dimension(55,55)); backbutton.addactionlistener(this); homebutton.addactionlistener(this); turnbutton.addactionlistener(this); addbutton.addactionlistener(this); setlayout(new flowlayout(flowlayout.center)); add(backbutton); add(homebutton); add(turnbutton); add(addbutton); } public void actionperformed(actionevent e) { jbutton clicked = (jbutton)e.getsource(); //test moment if(clicked == backbutton){ system.out.println("back"); }else if(clicked == homebutton){ system.out.println("home"); }else if(clicked == turnbutton){ system.out.println("turn"); }else if(clicked == addbutton){ int returnval = fc.showopendialog(menupanel.this); if(returnval == jfilechooser.approve_option){ file file = fc.getselectedfile(); } } } }
photopanel
import java.awt.flowlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.util.arraylist; import javax.swing.jbutton; import javax.swing.jpanel; public class photopanel extends jpanel implements actionlistener { arraylist<photo> album = new arraylist<photo>(); arraylist<jbutton> buttons = new arraylist<jbutton>(); public photopanel(){ setlayout(new flowlayout(flowlayout.center)); } public void actionperformed(actionevent e) { } }
i use separate class photocard
avoid lists:
class photocard { public photocard(photo photo) { add(photo); // add buttons, listeners, etc. } }
that holds necessary data , initializes listeners. , class can added to photopanel
:
photopanel.add(new photocard(...));
Comments
Post a Comment