java - Unsure How to Properly Use Swing Layouts -
sorry vague title, couldn't think of better. i'm trying create jswing application has 4 buttons, , 3 boxes images. on left 4 buttons. first "salestop", second "salesbottom", third "preliminary", , fourth "next". right have 3 boxes of equal size can insert image into, along being able set text next distinguish each picture is. have tried looking cannot seem layouts work wanted. have far.
private jbutton salestop, salesbottom, preliminary, next; private jlabel current; private jpanel salestopex, salesbottomex, preliminaryex, buttonpanel; private jframe mainframe = new jframe(); public int createjpanel() { buttonpanel = new jpanel(); buttonpanel.setpreferredsize(new dimension(150, 1000)); salestop = new jbutton("salestop"); salesbottom = new jbutton("salesbottom"); preliminary = new jbutton("preliminary"); salestop.addactionlistener(new buttonlistener("salestop")); salesbottom.addactionlistener(new buttonlistener("salesbottom")); preliminary.addactionlistener(new buttonlistener("preliminary")); buttonpanel.add(salestop); buttonpanel.add(salesbottom); buttonpanel.add(preliminary); mainframe.setlayout(new borderlayout()); mainframe.add(buttonpanel); mainframe.setdefaultcloseoperation(jframe.dispose_on_close); mainframe.setvisible(true); mainframe.getcontentpane(); return 0; }
use
jpanel
boxlayout
jbuttons
. setboxlayout
vertical.for images, given images same size, can add
imageicon
jlabel
, along text. can align text respectimageicon
anywhere usinglabel.sethorizontaltextposition
,label.setvertialtextposition
. if images different size, can search so, there few answers on how scaleimageicons
. can usejpanel
boxlayout
thesejlabel
. or maybe bettergridlayout
just wrap 2
jpanel
injpanel
borderlayout
. can place either 1 @borderlayout.center
, depending on 1center
put other 1 eitherwest
oreast
the key take advantage of nesting jpanels
different layout managers. can nest many jpanel
need.
see more @ laying out components within container
here's example
import java.awt.*; import java.net.malformedurlexception; import java.net.url; import java.util.logging.level; import java.util.logging.logger; import javax.swing.*; import javax.swing.border.*; public class nestjpanels { imageicon icon; public nestjpanels() { try { icon = new imageicon(new url("http://sstatic.net/programmers/img/apple-touch-icon.png")); } catch (malformedurlexception ex) { logger.getlogger(nestjpanels.class.getname()).log(level.severe, null, ex); } box box = box.createverticalbox(); (int = 0; < 4; i++) { box.add(box.createverticalstrut(10)); box.add(new jbutton("button")); } jpanel imagepanel = new jpanel(new gridlayout(0, 1)); (int = 0; < 3; i++) { jlabel label = new jlabel(icon); label.setborder(new matteborder(0, 0, 2, 0, color.gray)); label.settext("stackoverflow"); imagepanel.add(label); } jpanel panel = new jpanel(new borderlayout()); panel.setborder(new emptyborder(10, 10, 10, 10)); panel.add(box, borderlayout.west); panel.add(imagepanel); jframe frame = new jframe(); frame.add(panel); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { logger.getlogger(nestjpanels.class.getname()).log(level.severe, null, ex); } new nestjpanels(); } }); } }
Comments
Post a Comment