java - How can we put value on text field on output screen? -
i want put value in txtf1
@ output screen , it. how can put value on text field on output screen?
import java.awt.color; import java.awt.textfield; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jtextfield; import javax.swing.swingconstants; public class demog extends jpanel implements actionlistener{ private textfield textf, txtf1; public void jhand(){ textf = new textfield(); textf.setsize(40, 40); textf.settext("20"); textf.seteditable(false); textf.setbackground(color.white); textf.setforeground(color.black); //textf.sethorizontalalignment(swingconstants.center); textf.setlocation(15, 15); //textf.addactionlistener(this); txtf1 = new textfield(); txtf1.setsize(40, 40); txtf1.gettext(); txtf1.seteditable(false); txtf1.setbackground(color.white); txtf1.setforeground(color.black); //txtf1.sethorizontalalignment(swingconstants.center); txtf1.setlocation(50, 50); jframe frame = new jframe("demo"); jpanel p = new jpanel(); p.setopaque(true); p.setbackground(color.white); p.setlayout(null); frame.setcontentpane(p); frame.setsize(500,500); frame.setvisible(true); p.add(textf); p.add(txtf1); } public void actionperformed(actionevent evt) { string text = textf.gettext(); system.out.println(text); } public static void main(string... args){ demog g = new demog(); g.jhand(); } }
you have change of code in order work. had problem in code resolved them in following code. see comments learn in swing ;-)
import java.awt.color; import java.awt.dimension; import java.awt.flowlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.joptionpane; import javax.swing.jpanel; import javax.swing.jtextfield; import javax.swing.windowconstants; // use upper case in start of class names: public class demog extends jpanel implements actionlistener { private jtextfield textf, txtf1; public demog() { jhand(); } public void jhand() { setlayout(new flowlayout()); // set layout before add components // can use null layout, have use setbounds() method // placing components. advanced layout see // tutorials gridbaglayout , mixing layouts each other. textf = new jtextfield(); // not mix awt component // swing (j components. see packages) //textf.setsize(40, 40); // use setpreferredsize instead textf.setpreferredsize(new dimension(40, 40)); textf.settext("20"); textf.seteditable(false); // text fields getting data user // if need show user // use jlabel instead. textf.setbackground(color.white); textf.setforeground(color.black); add(textf); txtf1 = new jtextfield(); //txtf1.setsize(40, 40); use setpreferredsize instead txtf1.setpreferredsize(new dimension(40, 40)); txtf1.gettext(); txtf1.seteditable(false); txtf1.setbackground(color.white); txtf1.setforeground(color.black); add(txtf1); jbutton b = new jbutton("click me!"); b.addactionlistener(this); add(b); } public void actionperformed(actionevent evt) { string text = textf.gettext(); joptionpane.showmessagedialog(demog.this, "\"textf\" text is: "+text); } public static void main(string[] args) { jframe frame = new jframe("demo"); frame.setdefaultcloseoperation(windowconstants.exit_on_close); demog p = new demog(); p.setbackground(color.white); frame.setcontentpane(p); frame.setsize(500, 500); frame.setvisible(true); } }
good luck.
Comments
Post a Comment