java - How to make the component to respect column and row Weights? -
public class tester { public static class frame extends jframe { public frame() { // layout gridbaglayout layout=new gridbaglayout(); layout.columnweights=new double[] { 0.5, 0.5 }; layout.rowweights=new double[] { 1 }; // frame setlayout(layout); setsize(500,500); setdefaultcloseoperation(jframe.dispose_on_close); // constraints gridbagconstraints c=new gridbagconstraints(); c.fill=gridbagconstraints.both; // panel 1 jpanel p1=new jpanel(); p1.setbackground(color.green); c.gridx=0; c.gridy=0; add(p1,c); // panel 2 jlabel l1=new jlabel("testtesttesttesttesttesttesttesttesttesttesttest" + "testtesttesttesttesttesttesttesttesttesttesttesttest"); l1.setbackground(color.yellow); c.gridx=1; c.gridy=0; add(l1,c); } } public static void main(string[] args) { new frame().setvisible(true); } }
in case l1
takes whole space, want take half, 1 says: layout.columnweights=new double[] { 0.5, 0.5 };
i put c.fill=gridbagconstraints.both;
because want: if frame resized, want component resized, take maximum 50% space.
you add "filler" component "empty" side...
import java.awt.borderlayout; import java.awt.color; import java.awt.eventqueue; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; public class testlayout24 { public static void main(string[] args) { new testlayout24(); } public testlayout24() { eventqueue.invokelater( new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { } gridbagconstraints gbc = new gridbagconstraints(); gbc.weightx = 0.5f; gbc.weighty = 0.1f; gbc.fill = gridbagconstraints.both; jpanel left = new jpanel(); left.setbackground(color.red); jpanel right = new jpanel(); jframe frame = new jframe("test"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new gridbaglayout()); frame.add(left, gbc); frame.add(right, gbc); frame.setsize(200, 200); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } }
or
you use gridlayout
instead...
import java.awt.borderlayout; import java.awt.color; import java.awt.eventqueue; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.gridlayout; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; public class testlayout24 { public static void main(string[] args) { new testlayout24(); } public testlayout24() { eventqueue.invokelater( new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { } jpanel left = new jpanel(); left.setbackground(color.red); jframe frame = new jframe("test"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new gridlayout(0, 2)); frame.add(left); frame.setsize(200, 200); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } }
Comments
Post a Comment