java - Unable to type text in JTextfield -
so, wrote simple program has 1 jlabel, 1 jtextfield, , 2 jbuttons. problem jtextfield doesn't allow me enter texts. i've been thinking hard problem, appreciated!
/* method: init() */ /** * method has responsibility reading in data base * , initializing interactors @ bottom of window. */ public void init() { setsize(application_width, application_height); setfont("courier-24"); namelabel = new jlabel("name"); add(namelabel, south); namefield = new jtextfield(10); add(namefield, south); namefield.addactionlistener(this); graphbutton = new jbutton("graph"); add(graphbutton, south); clearbutton = new jbutton("clear"); add(clearbutton, south); addactionlisteners(); } /* method: actionperformed(e) */ /** * class responsible detecting when buttons * clicked, have define method respond * button actions. */ public void actionperformed(actionevent e) { if (e.getsource() == namefield) println("graph: " + namefield.gettext()); if (e.getsource() == graphbutton) println("graph"); if (e.getsource() == clearbutton) println("clear"); } /* private instance variables */ private jlabel namelabel; private jtextfield namefield; private jbutton graphbutton; private jbutton clearbutton;
works me...
import java.awt.eventqueue; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jtextfield; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; /** * * @author swhitehead */ public class javaapplication1251 { /** * @param args command line arguments */ public static void main(string[] args) { new javaapplication1251(); } public javaapplication1251() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { ex.printstacktrace(); } jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(new testpane()); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class testpane extends jpanel implements actionlistener { public testpane() { init(); } public void init() { setlayout(new gridbaglayout()); gridbagconstraints gbc = new gridbagconstraints(); gbc.gridwidth = gridbagconstraints.remainder; namelabel = new jlabel("name"); add(namelabel, gbc); namefield = new jtextfield(10); add(namefield, gbc); namefield.addactionlistener(this); graphbutton = new jbutton("graph"); add(graphbutton, gbc); clearbutton = new jbutton("clear"); add(clearbutton, gbc); // addactionlisteners(); } /* method: actionperformed(e) */ /** * class responsible detecting when buttons clicked, have define method respond button actions. */ public void actionperformed(actionevent e) { if (e.getsource() == namefield) { system.out.println("graph: " + namefield.gettext()); } if (e.getsource() == graphbutton) { system.out.println("graph"); } if (e.getsource() == clearbutton) { system.out.println("clear"); } } /* private instance variables */ private jlabel namelabel; private jtextfield namefield; private jbutton graphbutton; private jbutton clearbutton; } }
consider providing runnable example demonstrates problem. not code dump, example of doing highlights problem having. result in less confusion , better responses
Comments
Post a Comment