java - Set label layout in panels -
in frame, i'm adding labels for, in panel. problem can't solve is adding label on same line whereas want add \n between 1 , generate column! i've no idea how it. help? thank you!
the key believe layout of container holding jlabels. if give container proper layout such boxlayout oriented along page_axis, or gridlayout(0, 1)
1 column variable number of rows, jlabels stack 1 on top of other.
e.g.,
import java.awt.gridlayout; import javax.swing.*; public class stackinglabels extends jpanel { public static final string[] texts = { "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday" }; public stackinglabels() { setlayout(new gridlayout(0, 1)); (string text : texts) { add(new jlabel(text)); } } private static void createandshowgui() { stackinglabels mainpanel = new stackinglabels(); jframe frame = new jframe("stackinglabels"); frame.setdefaultcloseoperation(jframe.dispose_on_close); frame.getcontentpane().add(mainpanel); frame.pack(); frame.setlocationbyplatform(true); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { createandshowgui(); } }); } }
another thought use jlist display strings rather stacked jlabels.
Comments
Post a Comment