java - GridBagLayout align textField? -


i want shorten text field doesn't stretch to end of jframe how looks now: enter image description here

how control width of textfield does't streatch tried setpreferedsize() , setsize() yet didn't work??

@override         public void run() {              jframe frame = new jframe("test calculator");             frame.setvisible(true);             frame.setdefaultcloseoperation(jframe.exit_on_close);             frame.setlocationrelativeto(null);             frame.setsize(500, 500);              jpanel panel = new jpanel(new gridbaglayout());             frame.getcontentpane().add(panel, borderlayout.north);             gridbagconstraints c = new gridbagconstraints();              jlabel testlabel = new jlabel("enter score test 1: ");               c.gridx = 0;             c.gridy = 0;             c.anchor = gridbagconstraints.west;             c.fill = gridbagconstraints.both;             c.insets = new  insets(40, 15, 15, 0);             panel.add(testlabel , c);               jtextfield txtfield1 = new jtextfield("textfield");             c.gridx = 1;             c.gridy = 0;             c.fill = gridbagconstraints.horizontal;             c.weightx = .5;              panel.add(txtfield1 , c);         } 

you're telling layout text field must fill horizontally, that's does. replace

c.fill = gridbagconstraints.horizontal; 

by

c.fill = gridbagconstraints.none; 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -