Framesize issue while minimising and maximising java swing -
i have set frame size of java swing application to: frame.setsize(700, 500);
whenever maximise window, want ui elements adjust , take full application size. right when maximise, frame occupying full window remains blank, ui elements remain @ same position before. how should change code whenever maximise window, ui elements should occupy window ?
this sample code can app, gridbagconstraint
work re size need set weightx
or weighty
property non zero.
import java.awt.*; import javax.swing.*; public class app extends jframe{ public app(string title){ super(title); gridbaglayout layout = new gridbaglayout(); gridbagconstraints constraints = new gridbagconstraints(); getcontentpane().setlayout(layout); constraints.anchor = gridbagconstraints.west; jlabel l1 = new jlabel("first name:"); constraints.gridx = 0; constraints.gridy = 0; constraints.gridwidth = 1; constraints.gridheight = 1; constraints.weightx = 0; constraints.weighty = 0; constraints.fill = gridbagconstraints.both; constraints.insets = new insets(5,5,5,5); layout.setconstraints(l1, constraints); getcontentpane().add(l1); //jtextfield t1 = new jtextfield(10); jtextfield t1 = new jtextfield(); //removed passing column size. constraints.gridx = 1; constraints.gridy = 0; constraints.weightx = 1; constraints.fill = gridbagconstraints.horizontal; constraints.insets = new insets(5,5,5,5); layout.setconstraints(t1, constraints); getcontentpane().add(t1); setdefaultcloseoperation(exit_on_close); setsize(700,500); } public static void main(string[] args){ new app("app").setvisible(true); } }
Comments
Post a Comment