java - Swing program to display JButton -
i write swing code display 3 text fields , labels , 1 button, button displays @ top frame. want move bottom of 3 text fields. here code:
import javax.swing.*; import java.awt.*; class textfield { public static void main(string[] args) { jframe jf; jlabel rcno,name,amount; jtextfield rc_no,na_me,am_t; jbutton ok; jpanel p1,p2; jf=new jframe("billing"); rcno=new jlabel("recipt no: "); rc_no=new jtextfield(6); name=new jlabel("student name: "); na_me=new jtextfield(20); amount=new jlabel("amount: "); am_t=new jtextfield(5); ok=new jbutton("ok"); p1=new jpanel(); //p1.setlayout(new flowlayout()); p1.setlayout(new gridlayout(3,2,5,5)); p1.add(rcno); p1.add(rc_no); p1.add(name); p1.add(na_me); p1.add(amount); p1.add(am_t); p2=new jpanel(); p2.setlayout(new flowlayout(flowlayout.right)); //ok.setpreferredsize(new dimension(50,20)); p2.add(ok); p1.setbounds(110,100,200,100); //p2.setbounds(220,220,10,10); jf.add(p1); jf.add(p2); jf.setsize(400,500); jf.setvisible(true); jf.setdefaultcloseoperation(jframe.exit_on_close); } }
some notes on example:
don't use
setxxxsize()orsetbounds(); let layout work.for
p1, considergridbaglayout, shown here, orgrouplayout, shown here.the default layout of
jframeborderlayout; addp2south.swing gui objects should constructed , manipulated only on event dispatch thread.

import javax.swing.*; import java.awt.*; class formtest { public static void main(string[] args) { eventqueue.invokelater(new formtest()::display); } private void display() { jframe jf = new jframe("billing"); jbutton ok = new jbutton("ok"); jlabel rcno = new jlabel("recipt no:", jlabel.right); jtextfield rc_no = new jtextfield(12); jlabel name = new jlabel("student name:", jlabel.right); jtextfield na_me = new jtextfield(12); jlabel amount = new jlabel("amount:", jlabel.right); jtextfield am_t = new jtextfield(12); jpanel p1 = new jpanel(); p1.setlayout(new gridlayout(0, 2, 5, 5)); p1.setborder(borderfactory.createemptyborder(5, 5, 5, 5)); p1.add(rcno); p1.add(rc_no); p1.add(name); p1.add(na_me); p1.add(amount); p1.add(am_t); jpanel p2 = new jpanel(); p2.setlayout(new flowlayout(flowlayout.right)); p2.add(ok); jf.add(p1); jf.add(p2, borderlayout.south); jf.pack(); jf.setvisible(true); jf.setdefaultcloseoperation(jframe.exit_on_close); } }
Comments
Post a Comment