java - How to set JLabel in a specific area in JFrame? -
i trying make desktop application in java swing. trying create image slider in frame , got it. problem in want set specific area imagelabel
in frame. how can this? want set imagelabel
in left side. posting snapshot getting after running program.
here code
public class imageslider extends jpanel implements actionlistener { private static final int max = 20; private static final font sans = new font("sansserif", font.plain, 16); private static final border border = borderfactory.creatematteborder(4, 16, 4, 16, color.blue); private list<string> list = new arraylist<string>(max); private list<imageicon> cache = new arraylist<imageicon>(max); private jlabel imagelabel = new jlabel(); //label = new jlabel( image, swingconstants.center); private jbutton prevbutton = new jbutton(); private jbutton nextbutton = new jbutton(); private jcombobox favorites; public imageslider() { this.setlayout(new borderlayout()); list.add("c.jpg"); list.add("a0.png"); list.add("yellow.png"); list.add("a0.png"); list.add("c.jpg"); (int = 0; < list.size(); i++) cache.add(i, null); imageicon image = new imageicon("e:\\software\\trainpis\\res\\drawable\\a0.png"); jlabel titlelabel = new jlabel(image, swingconstants.center); // titlelabel.settext("imageslider"); titlelabel.sethorizontalalignment(jlabel.center); titlelabel.setfont(new font(font.sans_serif, font.bold, 24)); titlelabel.setborder(border); this.add(titlelabel, borderlayout.north); imagelabel.seticon(getimage(0)); imagelabel.setalignmentx(left_alignment); imagelabel.sethorizontalalignment(jlabel.center); imagelabel.setborder(border); this.add(imagelabel, borderlayout.center); favorites = new jcombobox( list.toarray(new string[list.size()])); favorites.setactioncommand("favs"); favorites.addactionlistener(this); prevbutton.settext("\u22b2prev"); prevbutton.setfont(sans); prevbutton.setactioncommand("prev"); prevbutton.addactionlistener(this); nextbutton.settext("next\u22b3"); nextbutton.setfont(sans); nextbutton.setactioncommand("next"); nextbutton.addactionlistener(this); jpanel controlpanel = new jpanel(); controlpanel.add(prevbutton); controlpanel.add(favorites); controlpanel.add(nextbutton); controlpanel.setborder(border); this.add(controlpanel, borderlayout.south); } public void actionperformed(actionevent ae) { string cmd = ae.getactioncommand(); if ("favs".equals(cmd)) { int index = favorites.getselectedindex(); imageicon image = getimage(index); imagelabel.seticon(image); if (image != null) imagelabel.settext(""); else imagelabel.settext("image not available."); } if ("prev".equals(cmd)) { int index = favorites.getselectedindex() - 1; if (index < 0) index = list.size() - 1; favorites.setselectedindex(index); } if ("next".equals(cmd)) { int index = favorites.getselectedindex() + 1; if (index > list.size() - 1) index = 0; favorites.setselectedindex(index); } } public jbutton getdefault() { return nextbutton; } // return (possibly cached) image having given index. private imageicon getimage(int index) { imageicon image = cache.get(index); if (image != null) return image; string name = "/images/" + list.get(index); url url = imageslider.class.getresource(name); if (url != null) { image = new imageicon(url); } cache.set(index, image); return image; } public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { jframe frame = new jframe(); frame.setdefaultcloseoperation(jframe.exit_on_close); imageslider go = new imageslider(); frame.add(go); frame.settitle("imageslider"); // frame.setsize(400, 300); frame.setextendedstate(jframe.maximized_both); frame.setundecorated(true); frame.setvisible(true); go.getdefault().requestfocusinwindow(); } }); } }
how can achieve goal?
the easiest way achieve put imagelabel
jpanel
flowlayout
. add panel bigger borderlayout
.
so change:
this.add(imagelabel, borderlayout.center);
to like:
jpanel imageconstrain = new jpanel(new flowlayout(swingconstants.left)); imageconstrain.add(imagelabel); this.add(imageconstrain, borderlayout.center);
Comments
Post a Comment