java - How to switch back and forth between JFrames -
to start playing buttons , jframe. issue can switch 1 jframe next few lines of code here...
jbutton studentloginbutton = new jbutton("student"); studentloginbutton.setbounds(85, 80, 80, 20); studentloginbutton.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { loginframe.super.setvisible(false); student.setvisible(true); } }); add(studentloginbutton);
but when set student jframe visible , use code here...
jbutton cancelbutton = new jbutton("go back"); cancelbutton.setbounds(205, 80, 80, 20); cancelbutton.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { studentframe.super.setvisible(false); login1.setvisible(true); } }); add(cancelbutton);
to cancel , go not work. nothing shows , application not terminated. solve issue? can provide more code if helps others find solution.
it looks more code here both classes. loginframe.java...
package system; 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.jlabel; import javax.swing.jpasswordfield; import javax.swing.jtextfield; public class loginframe extends jframe implements actionlistener{ private static final long serialversionuid = 1l; studentframe student; public static void main(string[] args) { new loginframe().setvisible(true); } loginframe(){ super(" user login "); setsize(400, 80); setresizable(false); setdefaultcloseoperation(exit_on_close); studentframe student = new studentframe(null); setlayout(new flowlayout()); //student jbutton studentloginbutton = new jbutton("student"); studentloginbutton.setbounds(85, 80, 80, 20); studentloginbutton.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { loginframe.super.setvisible(false); student.setvisible(true); } }); add(studentloginbutton); //dept. staff jbutton deptstaffloginbutton = new jbutton("department staff"); deptstaffloginbutton.setbounds(85, 80, 80, 20); deptstaffloginbutton.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { loginframe.super.setvisible(false); student.setvisible(true); } }); add(deptstaffloginbutton); //instructor jbutton instructorloginbutton = new jbutton("instructor"); instructorloginbutton.setbounds(85, 80, 80, 20); instructorloginbutton.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { loginframe.super.setvisible(false); student.setvisible(true); } }); add(instructorloginbutton); } @override public void actionperformed(actionevent e) { } }
and next is... studentframe.java package system;
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.jlabel; import javax.swing.jpasswordfield; import javax.swing.jtextfield; public class studentframe extends jframe implements actionlistener{ private static final long serialversionuid = 1l; static loginframe login1; public static void main(string[] args) { new studentframe(login1).setvisible(true); } studentframe(loginframe login){ super(" user login "); setsize(600, 600); setresizable(false); setdefaultcloseoperation(exit_on_close); setlayout(null); login1 = login; // loginframe login = new loginframe(); jbutton loginbutton = new jbutton("login"); loginbutton.setbounds(85, 80, 80, 20); loginbutton.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { // loginframe.super.setvisible(false); // student.setvisible(true); } }); add(loginbutton); jbutton cancelbutton = new jbutton("go back"); cancelbutton.setbounds(205, 80, 80, 20); cancelbutton.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { studentframe.super.setvisible(false); login1.setvisible(true); } }); add(cancelbutton); jlabel passlabel = new jlabel("password: "); passlabel.setbounds(10, 50, 80, 25); add(passlabel); jpasswordfield passwordtext = new jpasswordfield(20); passwordtext.setbounds(85, 50, 240, 25); add(passwordtext); jlabel userlabel = new jlabel("user name: "); userlabel.setbounds(10, 10, 80, 25); add(userlabel); jtextfield usernametext = new jtextfield(); usernametext.setbounds(85, 10, 240, 25); add(usernametext); } @override public void actionperformed(actionevent e) { // todo auto-generated method stub } }
it's hard know why current code not working since don't have running example, worry when see static calls, , calls super this, studentframe.super.setvisible(false);
.
but regardless of directly solution problem, , while know isn't answering question, best solution this: don't. don't throw different jframes @ user can quite annoying thing user experience, when there better ways of swapping views, best here being use cardlayout swap jpanel views, , suggest. can find tutorial here: cardlayout tutorial.
also please @ link: the use of multiple jframes, good/bad practice?
another option, if want use login dialog that, use modal jdialog show login window, , make modal off of main application.
for details on either of these solutions, show more pertinent code.
as "side" recommendation, have mention call setbounds
. while null layouts , setbounds()
might seem swing newbies easiest , best way create complex gui's, more swing gui's create more serious difficulties run when using them. won't resize components when gui resizes, royal witch enhance or maintain, fail when placed in scrollpanes, gawd-awful when viewed on platforms or screen resolutions different original one.
on further review of code, believe best option use modal jdialog display login window. behaves similar jframe, uses different constructor (the api can this). main difference in behavior halts calling code calling code sets jdialog visible -- joptionpane (and note is specialized jdialog itself). way calling code know whenever jdialog has been dealt , no longer visible code flow resumes right after had set jdialog visible. way code in main gui can display dialog, , acquire information when user done it.
Comments
Post a Comment