java - GridBagLayout and minimum width -
i want make panel constrained maximum width, reduces width when container becomes shorter.
i used gridbaglayout
behaves weirdly when size becomes short enough.
import java.awt.color; import java.awt.dimension; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import javax.swing.jframe; import javax.swing.jpanel; /** * @author michael nesterenko * */ public class ssce extends jframe { /** * */ public ssce() { setdefaultcloseoperation(jframe.exit_on_close); gridbaglayout gbl = new gridbaglayout(); gbl.columnwidths = new int[] {200, 1}; gbl.columnweights = new double[] {0, 1}; gbl.rowheights = new int[] {10}; gbl.rowweights = new double[] {0}; setlayout(gbl); gridbagconstraints gbc = new gridbagconstraints(); gbc.fill = gridbagconstraints.horizontal; jpanel sizerestrictedpanel = new jpanel(); sizerestrictedpanel.setbackground(color.blue); sizerestrictedpanel.setminimumsize(new dimension(50, 50)); sizerestrictedpanel.setmaximumsize(new dimension(300, 50)); sizerestrictedpanel.setpreferredsize(new dimension(300, 50)); add(sizerestrictedpanel, gbc); jpanel dummy = new jpanel(); dummy.setbackground(color.red); add(dummy, gbc); setpreferredsize(new dimension(600, 200)); pack(); } /** * @param args */ public static void main(string[] args) { new ssce().setvisible(true); } }
when frame width becomes short enough blue panel instantly resized, want resized smoothly window resizing.
you can define gridbagconstraints.ipadx
, gridbagconstraints.ipady
constraints set min width/height
/** * field specifies internal padding of component, how * space add minimum width of component. width of * component @ least minimum width plus * <code>ipadx</code> pixels. * <p> * default value <code>0</code>. * @serial * @see #clone() * @see java.awt.gridbagconstraints#ipady */ public int ipadx; /** * field specifies internal padding, is, how * space add minimum height of component. height of * component @ least minimum height plus * <code>ipady</code> pixels. * <p> * default value 0. * @serial * @see #clone() * @see java.awt.gridbagconstraints#ipadx */ public int ipady;
Comments
Post a Comment