java - How to position/lay out components on JPanel? -
i'm trying position 5 buttons on jframe. 4 buttons should in first row , fifth 1 should in second row. here of buttons appear in 1 row , half of fifth button out of panel. way if use frame.pack(); frame become smaller expected. don't want change size of frame. want locate fifth 1 in second row.
import java.awt.gridbaglayout; import java.awt.gridlayout; import javax.swing.*; public class test { public static void main(string[] args) { jframe frame = new jframe(); frame.setbounds(100, 100, 529, 300); frame.getcontentpane().setlayout(new gridlayout(1, 0, 0, 0)); jpanel panel = new jpanel(); frame.getcontentpane().add(panel); panel.setlayout(new boxlayout(panel, boxlayout.x_axis)); jbutton btnnewbutton_4 = new jbutton("new button"); panel.add(btnnewbutton_4); jbutton btnnewbutton_3 = new jbutton("new button"); panel.add(btnnewbutton_3); jbutton btnnewbutton = new jbutton("new button"); panel.add(btnnewbutton); jbutton btnnewbutton_1 = new jbutton("new button"); panel.add(btnnewbutton_1); jbutton btnnewbutton_2 = new jbutton("new button"); panel.add(btnnewbutton_2); frame.setvisible(true); frame.setdefaultcloseoperation(jframe.exit_on_close); } }
four buttons should in first row , fifth 1 should in second row.
standard , booking examples in oracle tutorial how use layout managers
use gridlayout (two rows) jpanel, jbuttons have same dimension
use gridbaglayout, each of jbuttons have diferrent size or not too
invisible jcomponents or empty jlabel can in complicated variations
Comments
Post a Comment