java - How to get 2 JPanels within 1 JPanel, resizable, with fixed proportion? -
let's jframe
contains 1 jpanel
. jpanel
divided 2 jpanel
s occupying accordingly 0.75
, 0.25
of jframe
height. want of resizable along window size.
i have no thought how in java.
i'm newbie java. i've read bit layouts, can see how set preferred size in constructor (ceasing resize when number reached) or fixed sizes obtained through setting borders.
jframe
borderlayout
, onto that, add together jpanel
gridbaglayout
. add together other 2 panels onto this.
see laying out components within container , how utilize gridbaglayout more details
import java.awt.color; import java.awt.dimension; import java.awt.eventqueue; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; public class test { public static void main(string[] args) { new test(); } public test() { eventqueue.invokelater(new runnable() { @override public void run() { seek { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } grab (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { ex.printstacktrace(); } jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(new testpane()); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class testpane extends jpanel { public testpane() { setlayout(new gridbaglayout()); gridbagconstraints gbc = new gridbagconstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.fill = gridbagconstraints.both; gbc.weightx = 1; gbc.weighty = 0.75; jpanel top = new jpanel(new gridbaglayout()); top.add(new jlabel("top")); top.setbackground(color.red); add(top, gbc); gbc.gridy++; gbc.weighty = 0.25; jpanel bottom = new jpanel(new gridbaglayout()); bottom.add(new jlabel("bottom")); bottom.setbackground(color.blue); add(bottom, gbc); } @override public dimension getpreferredsize() { homecoming new dimension(200, 200); } } }
java swing user-interface layout-manager
No comments:
Post a Comment