java - How to close main frame when open new one -
ive created 2 frames main frame home , second 1 selectie on home there button open frame selectie, want when click button main frame home wil dissapear , selectie shown. code button ive make in other package , dont want in same class main (home)
code home:
package view; import java.awt.borderlayout; import java.awt.color; import java.awt.container; import java.awt.dimension; import java.awt.toolkit; import java.awt.event.actionlistener; import java.io.file; import javax.swing.icon; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jdialog; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import controller.homecontroller; import music.playsound; public class home extends jframe { private jlabel label, label1, label2; private jpanel panel; private jbutton logo, logo1, logo2, logo3, logo4, logo5, selectie; private container window = getcontentpane(); private homecontroller controller; public home (){ initgui(); controller = new homecontroller(); } public void addhomelistener(actionlistener a){ selectie.addactionlistener(a); } public void initgui(){ setlayout(null); settitle(""); setpreferredsize(new dimension(800,600)); setdefaultcloseoperation(jframe.exit_on_close); label = new jlabel(); label.setbounds(0, 0, 266, 800); label.setbackground(color.white); label.setopaque(true); window.add(label); label1 = new jlabel(); label1.setbounds(267, 0, 266, 800); label1.setbackground(color.red); label1.setopaque(true); window.add(label1); label2 = new jlabel(); label2.setbounds(533, 0, 266, 800); label2.setbackground(color.white); label2.setopaque(true); window.add(label2); logo = new jbutton(new imageicon("../ajax/src/img/logotje.gif")); logo.setborderpainted(false); logo.setbounds(40, 150, 188, 188); label1.add(logo); logo1 = new jbutton(new imageicon("../ajax/src/img/ster.png")); logo1.setborderpainted(false); logo1.setbounds(10, 50, 82, 82); label1.add(logo1); logo2 = new jbutton(new imageicon("../ajax/src/img/ster.png")); logo2.setborderpainted(false); logo2.setbounds(92, 20, 82, 82); label1.add(logo2); logo3 = new jbutton(new imageicon("../ajax/src/img/ster.png")); logo3.setborderpainted(false); logo3.setbounds(174, 50, 82, 82); label1.add(logo3); logo4 = new jbutton(new imageicon("../ajax/src/img/shirt.png")); logo4.setborderpainted(false); logo4.setbounds(50, 50, 135, 182); label.add(logo4); logo5 = new jbutton(new imageicon("../ajax/src/img/uitshirt.png")); logo5.setborderpainted(false); logo5.setbounds(65, 50, 138, 190); label2.add(logo5); selectie = new jbutton("selectie"); selectie.setbounds(60, 500, 99, 25); selectie.setactioncommand("selectie"); label.add(selectie); pack(); addhomelistener(new homecontroller()); } }
code button:
package controller; import java.awt.event.actionevent; import java.awt.event.actionlistener; import view.home; import view.selectie; public class homecontroller implements actionlistener { public void actionperformed (actionevent e){ selectie selectie = new selectie(); selectie.setvisible(true); } }
please give valid attention @kleopatra , @mkorbel, has say, right in pointing out make things easier.
here had added comments in code, check out :
import java.awt.borderlayout; import java.awt.color; import java.awt.container; import java.awt.dimension; import java.awt.toolkit; import java.awt.event.actionlistener; import java.io.file; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.icon; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jdialog; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; //import controller.homecontroller; //import music.playsound; public class home extends jframe { private jlabel label, label1, label2; private jpanel panel; private jbutton logo, logo1, logo2, logo3, logo4, logo5, selectie; private container window = getcontentpane(); private homecontroller controller; public home (){ initgui(); } public void addhomelistener(actionlistener a){ selectie.addactionlistener(a); } public void initgui(){ setlayout(null); settitle(""); setpreferredsize(new dimension(800,600)); setdefaultcloseoperation(jframe.exit_on_close); label = new jlabel(); label.setbounds(0, 0, 266, 800); label.setbackground(color.white); label.setopaque(true); window.add(label); label1 = new jlabel(); label1.setbounds(267, 0, 266, 800); label1.setbackground(color.red); label1.setopaque(true); window.add(label1); label2 = new jlabel(); label2.setbounds(533, 0, 266, 800); label2.setbackground(color.white); label2.setopaque(true); window.add(label2); logo = new jbutton(new imageicon("../ajax/src/img/logotje.gif")); logo.setborderpainted(false); logo.setbounds(40, 150, 188, 188); label1.add(logo); logo1 = new jbutton(new imageicon("../ajax/src/img/ster.png")); logo1.setborderpainted(false); logo1.setbounds(10, 50, 82, 82); label1.add(logo1); logo2 = new jbutton(new imageicon("../ajax/src/img/ster.png")); logo2.setborderpainted(false); logo2.setbounds(92, 20, 82, 82); label1.add(logo2); logo3 = new jbutton(new imageicon("../ajax/src/img/ster.png")); logo3.setborderpainted(false); logo3.setbounds(174, 50, 82, 82); label1.add(logo3); logo4 = new jbutton(new imageicon("../ajax/src/img/shirt.png")); logo4.setborderpainted(false); logo4.setbounds(50, 50, 135, 182); label.add(logo4); logo5 = new jbutton(new imageicon("../ajax/src/img/uitshirt.png")); logo5.setborderpainted(false); logo5.setbounds(65, 50, 138, 190); label2.add(logo5); selectie = new jbutton("selectie"); selectie.setbounds(60, 500, 99, 25); selectie.setactioncommand("selectie"); label.add(selectie); pack(); /* * making new object again, * when had declared * instance variable. used * 1 declared instance variable.. * send object of home * class, means object of class.. * , know object of * class in default known * this, passing homecontroller class. */ controller = new homecontroller(this); addhomelistener(controller); setvisible(true); } public static void main(string... args) { javax.swing.swingutilities.invokelater(new runnable() { public void run() { new home(); } }); } } class homecontroller implements actionlistener { /* * here declared home class's variable, * use dispose jframe. */ private home home; public homecontroller(home home) { this.home = home; } public void actionperformed (actionevent e){ home.dispose(); selectie selectie = new selectie(); selectie.setvisible(true); } } class selectie extends jframe { public selectie() { initgui(); } public void initgui() { setdefaultcloseoperation(exit_on_close); setlocationbyplatform(true); setsize(300, 300); } }
Comments
Post a Comment