swing - Java EventHandler not working, why does it create new Frame? -
i trying create address book uni project , trying gui able save details input form file. every time click save on gui creates new frame rather executing code have put in handler. still new java , can't see wrong it.
import java.io.bufferedwriter; import java.io.file; import java.io.filewriter; import java.io.ioexception; import java.io.printwriter; public class addressbook { static addressbookgui addressbookgui = new addressbookgui(); static int writecount; file detailsfile = new file("customerdetails.txt"); public static void savedetails() throws ioexception { string title = addressbookgui.txttitle.gettext(); filewriter fw = new filewriter("customerdetails.txt"); bufferedwriter bw = new bufferedwriter(fw); printwriter out = new printwriter(bw); out.println(title); out.close(); writecount++; } }
above handler class, below gui.
import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.io.ioexception; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.joptionpane; import javax.swing.jpanel; import javax.swing.jtextfield; public class addressbookgui { private jlabel lbltitle; public jtextfield txttitle; private jbutton btnsavedetails; private jpanel panel; private jframe frame; public static void main(string[] args) { new addressbookgui(); } public addressbookgui() { createpanel(); addlabels(); addtextfields(); addbuttons(); frame.add(panel); frame.setvisible(true); } public void createpanel() { frame = new jframe(); frame.settitle("address book"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(800,950); frame.setvisible(true); panel = new jpanel(); panel.setlayout(null); } public void addlabels() { lbltitle = new jlabel("title"); lbltitle.setbounds(90,210,140,30); panel.add(lbltitle); } public void addtextfields() { txttitle = new jtextfield(""); txttitle.setbounds(190,210,150,30); panel.add(txttitle); } public void addbuttons() { btnsavedetails = new jbutton("save"); btnsavedetails.setbounds(200,600,80,30); btnsavedetails.addactionlistener(new savedetailsbutton()); panel.add(btnsavedetails); } public class savedetailsbutton implements actionlistener { public void actionperformed(actionevent event) { try { addressbook.savedetails(); } catch (ioexception e) { joptionpane.showmessagedialog(null, "file did not write correctly.", "error", joptionpane.error_message); } } } }
thanks in advance.
Comments
Post a Comment