i working on little game gui, having problem layout.
when run this:
the listview
being covered textarea
, trying next it. in example:
here code have far:
import javafx.application.application; import javafx.geometry.insets; import javafx.geometry.vpos; import javafx.scene.scene; import javafx.scene.control.button; import javafx.scene.control.label; import javafx.scene.control.listview; import javafx.scene.control.textarea; import javafx.scene.layout.gridpane; import javafx.stage.stage; public class test extends application { stage window; listview<string> listview; textarea descarea = new textarea(); textarea actiondescarea = new textarea(); label healthlabel = new label("health:"); label manalabel = new label("mana:"); label healthdisplay = new label("100/100"); label manadisplay = new label("100/100"); button actionbtn = new button("action"); public static void main(string[] args) { launch(args); } @override public void start(stage primarystage) throws exception { window = primarystage; window.settitle("game"); gridpane grid = new gridpane(); grid.setpadding(new insets(10, 10, 10, 10)); grid.setvgap(10); grid.sethgap(10); descarea.setprefwidth(750); descarea.setprefheight(550); descarea.setwraptext(true); descarea.seteditable(false); gridpane.setconstraints(descarea, 0, 0, 1, 2); gridpane.setconstraints(healthlabel, 1, 0); gridpane.setvalignment(healthlabel, vpos.top); gridpane.setconstraints(healthdisplay, 2, 0); gridpane.setvalignment(healthdisplay, vpos.top); gridpane.setconstraints(manalabel, 1, 1); gridpane.setvalignment(manalabel, vpos.top); gridpane.setconstraints(manadisplay, 2, 1); gridpane.setvalignment(manadisplay, vpos.top); listview = new listview<>(); listview.getitems().addall("action 1", "action 2", "action 3", "action 4"); listview.setprefwidth(260); listview.setmaxwidth(260); listview.setprefheight(150); gridpane.setconstraints(listview, 0, 2, 1, 1); actiondescarea.setprefwidth(100); actiondescarea.setprefheight(150); actiondescarea.setmaxwidth(100); actiondescarea.setwraptext(true); actiondescarea.seteditable(false); gridpane.setconstraints(actiondescarea, 0, 2, 2, 1); actionbtn.setprefwidth(260); actionbtn.setprefheight(60); gridpane.setconstraints(actionbtn, 0, 3); grid.getchildren().addall(descarea, healthlabel, healthdisplay, manalabel, manadisplay, listview, actionbtn, actiondescarea); scene scene = new scene(grid, 950, 750); window.setscene(scene); window.show(); }
if point me in right direction, great!
thanks :)
few things take note of :
gridpane.setconstraints(descarea, 0, 0, 1, 2);
here, textarea descarea
has colspan 1 columnindex 0.
gridpane.setconstraints(listview, 0, 2, 1, 1);
here, listview listview
has colspan 1 columnindex 0.
gridpane.setconstraints(actiondescarea, 0, 2, 2, 1);
here, textarea actiondescarea
has colspan 2 columnindex 0.
if want place actiondescarea
next listview
, should have columnindex greater of latter. additionally, if want grow , cover entire width below descarea
, need set hgrow priority.always
, remove maxwidth
gridpane.setconstraints(actiondescarea, 1, 2, 1, 1); gridpane.sethgrow(actiondescarea, priority.always);
additionally, since both listview
, actiondescarea
should under descarea
, colspan should 2.
gridpane.setconstraints(descarea, 0, 0, 2, 2);
solution :
import javafx.application.application; import javafx.geometry.insets; import javafx.geometry.vpos; import javafx.scene.scene; import javafx.scene.control.button; import javafx.scene.control.label; import javafx.scene.control.listview; import javafx.scene.control.textarea; import javafx.scene.layout.gridpane; import javafx.scene.layout.priority; import javafx.stage.stage; public class test extends application { stage window; listview<string> listview; textarea descarea = new textarea(); textarea actiondescarea = new textarea(); label healthlabel = new label("health:"); label manalabel = new label("mana:"); label healthdisplay = new label("100/100"); label manadisplay = new label("100/100"); button actionbtn = new button("action"); public static void main(string[] args) { launch(args); } @override public void start(stage primarystage) throws exception { window = primarystage; window.settitle("game"); gridpane grid = new gridpane(); grid.setpadding(new insets(10, 10, 10, 10)); grid.setvgap(10); grid.sethgap(10); descarea.setprefwidth(750); descarea.setprefheight(550); descarea.setwraptext(true); descarea.seteditable(false); gridpane.setconstraints(descarea, 0, 0, 2, 2); gridpane.setconstraints(healthlabel, 2, 0); gridpane.setvalignment(healthlabel, vpos.top); gridpane.setconstraints(healthdisplay, 3, 0); gridpane.setvalignment(healthdisplay, vpos.top); gridpane.setconstraints(manalabel, 2, 1); gridpane.setvalignment(manalabel, vpos.top); gridpane.setconstraints(manadisplay, 3, 1); gridpane.setvalignment(manadisplay, vpos.top); listview = new listview<>(); listview.getitems().addall("action 1", "action 2", "action 3", "action 4"); listview.setprefwidth(260); listview.setmaxwidth(260); listview.setprefheight(150); gridpane.setconstraints(listview, 0, 2, 1, 1); actiondescarea.setprefwidth(100); actiondescarea.setprefheight(150); actiondescarea.setwraptext(true); actiondescarea.seteditable(false); gridpane.setconstraints(actiondescarea, 1, 2, 1, 1); gridpane.sethgrow(actiondescarea, priority.always); actionbtn.setprefwidth(260); actionbtn.setprefheight(60); gridpane.setconstraints(actionbtn, 0, 3); grid.getchildren().addall(descarea, healthlabel, healthdisplay, manalabel, manadisplay, listview, actionbtn, actiondescarea); scene scene = new scene(grid, 950, 750); window.setscene(scene); window.show(); } }
output :
Comments
Post a Comment