java - Swing JFrame button action listener not working -
i want move 1 jframe
jframe
after pressing jbutton
named login
. attaching both frame code below, if there correction or 1 want give me instruction or guideline can give.
import java.awt.dimension; import java.awt.rectangle; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.*; public class login implements actionlistener { private jframe jframe = null; private jpanel jcontentpane = null; private jlabel id = null; private jtextfield userid = null; private jlabel pass = null; private jpasswordfield password = null; private jbutton login = null; private jframe getjframe() { if (jframe == null) { jframe = new jframe(); jframe.setsize(new dimension(346, 301)); jframe.setresizable( false ); jframe.settitle("shree datta digambar"); jframe.setdefaultcloseoperation(jframe.exit_on_close); jframe.setcontentpane(getjcontentpane()); } return jframe; } private jpanel getjcontentpane() { if (jcontentpane == null) { pass = new jlabel(); pass.setbounds(new rectangle(21, 105, 116, 27)); pass.settext("password"); id = new jlabel(); id.setbounds(new rectangle(21, 54, 119, 26)); id.settext("enter user id"); jcontentpane = new jpanel(); jcontentpane.setlayout(null); jcontentpane.add(id, null); jcontentpane.add(getuserid(), null); jcontentpane.add(pass, null); jcontentpane.add(getpassword(), null); jcontentpane.add(getlogin(), null); } return jcontentpane; } private jtextfield getuserid() { if (userid == null) { userid = new jtextfield(); userid.setbounds(new rectangle(164, 53, 140, 26)); } return userid; } private jpasswordfield getpassword() { if (password == null) { password = new jpasswordfield(); password.setbounds(new rectangle(165, 106, 137, 27)); } return password; } private jbutton getlogin() { if (login == null) { login = new jbutton(); login.setbounds(new rectangle(165, 169, 136, 27)); login.settext("login"); login.addactionlistener((actionlistener) this); } return login; } public static void main(string[] args) { login l=new login(); jframe f=new jframe(); f=l.getjframe(); f.setlocation(300,150); f.setvisible(true); } public void actionperformed(actionevent e) { if(e.getsource()==login) { if(userid.gettext().equals("")||password.gettext().equals("")) { joptionpane.showmessagedialog(null, "please enter details"); } else { if(userid.gettext().equals("digambar")&&password.gettext().equals("dalvi")) { joptionpane.showmessagedialog(null,"welcome customer details of papers"); getjframe().dispose(); jmenubar menubar = new jmenubar(); mainmenu f = new mainmenu(); } else { joptionpane.showmessagedialog(null,"sorry please enter valid details"); userid.settext(""); password.settext(""); } } } } }
second frame code is
import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jframe; import javax.swing.jmenu; import javax.swing.jmenubar; import javax.swing.jmenuitem; public class mainmenu extends jframe implements actionlistener { public static void main(string[] args) { mainmenu f=new mainmenu(); f.setvisible(true); f.setsize(646,401); f.setresizable( false ); f.settitle("shree datta digambar"); f.setdefaultcloseoperation(jframe.exit_on_close); jmenubar menubar = new jmenubar(); f.setjmenubar(menubar); jmenu customer=new jmenu("customer"); menubar.add(customer); jmenu paper=new jmenu("paper"); menubar.add(paper); jmenu edit=new jmenu("edit"); menubar.add(edit); jmenu view=new jmenu("view"); menubar.add(view); jmenu bill=new jmenu("bill"); menubar.add(bill); jmenu help=new jmenu("help"); menubar.add(help); jmenuitem newitem = new jmenuitem("new customer"); customer.add(newitem); jmenuitem deleteitem = new jmenuitem("delete customer"); customer.add(deleteitem); jmenuitem addpaperitem = new jmenuitem("add paper"); paper.add(addpaperitem); jmenuitem deletepaperitem = new jmenuitem("delete paper"); paper.add(deletepaperitem); jmenuitem customer_detail = new jmenuitem("customer detail"); edit.add(customer_detail); jmenuitem paper_detail = new jmenuitem("paper detail"); edit.add(paper_detail); jmenuitem perticular_customer_detail = new jmenuitem("perticular customer detail"); view.add(perticular_customer_detail); jmenuitem customer_bill = new jmenuitem("customer bills"); bill.add(customer_bill); newitem.addactionlistener(new mainmenu()); deleteitem.addactionlistener(new mainmenu()); addpaperitem.addactionlistener(new mainmenu()); deletepaperitem.addactionlistener(new mainmenu()); customer_detail.addactionlistener(new mainmenu()); paper_detail.addactionlistener(new mainmenu()); perticular_customer_detail.addactionlistener(new mainmenu()); customer_bill.addactionlistener(new mainmenu()); customer.addactionlistener(new mainmenu()); paper.addactionlistener(new mainmenu()); edit.addactionlistener(new mainmenu()); view.addactionlistener(new mainmenu()); bill.addactionlistener(new mainmenu()); help.addactionlistener(new mainmenu()); } @override public void actionperformed(actionevent e) { throw new unsupportedoperationexception("not supported yet."); } }
you can use anonymous inner classes jmenuitem,
jmenuitem newitem = new jmenuitem("new customer"); newitem.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { mainmenu.this.dispose(); new_customer nn = new new_customer() {}; } });
or
let mainmenu implement actionlistener interface (class mainmenu implements actionlistener
)
jmenuitem newitem = new jmenuitem("new customer"); newitem.addactionlistener(this); newitem.setactioncommand("new customer"); public void actionperformed(actionevent e) { if(e.getactioncommand().equals("new customer")) { system.out.println("clicked new customer menu"); mainmenu.this.dispose(); new_customer nn = new new_customer() {}; } }
Comments
Post a Comment