java - Swing CardLayout Runtime Error -


this program supposed add 2 panels jframe, each switch button allowing user switch between each panel. compiles fine, when try click switch1 button, error:

exception in thread "awt-eventqueue-0" java.lang.classcastexception: java.awt.borderlayout cannot cast java.awt.cardlayout @ tester.actionperformed(tester.java:36) @ javax.swing.abstractbutton.fireactionperformed(unknown source) @ javax.swing.abstractbutton$handler.actionperformed(unknown source) @ javax.swing.defaultbuttonmodel.fireactionperformed(unknown source) @ javax.swing.defaultbuttonmodel.setpressed(unknown source) @ javax.swing.plaf.basic.basicbuttonlistener.mousereleased(unknown source) @ java.awt.component.processmouseevent(unknown source) @ javax.swing.jcomponent.processmouseevent(unknown source) @ java.awt.component.processevent(unknown source) @ java.awt.container.processevent(unknown source) @ java.awt.component.dispatcheventimpl(unknown source) @ java.awt.container.dispatcheventimpl(unknown source) @ java.awt.component.dispatchevent(unknown source) @ java.awt.lightweightdispatcher.retargetmouseevent(unknown source) @ java.awt.lightweightdispatcher.processmouseevent(unknown source) @ java.awt.lightweightdispatcher.dispatchevent(unknown source) @ java.awt.container.dispatcheventimpl(unknown source) @ java.awt.window.dispatcheventimpl(unknown source) @ java.awt.component.dispatchevent(unknown source) @ java.awt.eventqueue.dispatcheventimpl(unknown source) @ java.awt.eventqueue.access$200(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue$4.run(unknown source) @ java.awt.eventqueue$4.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue.dispatchevent(unknown source) @ java.awt.eventdispatchthread.pumponeeventforfilters(unknown source) @ java.awt.eventdispatchthread.pumpeventsforfilter(unknown source) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.run(unknown source)

i did set jframe layout cardlayout when use this.getlayout(), should return cardlayout object. program:

import javax.swing.*; import java.awt.*; import java.awt.event.*;  public class tester extends jframe implements actionlistener {     cardlayout cc = new cardlayout();     jlabel text1 = new jlabel("panel 1");         jlabel text2 = new jlabel("panel 2");     jpanel panel1 = new jpanel();     jpanel panel2 = new jpanel();     jbutton switch1 = new jbutton("switch1");     jbutton switch2 = new jbutton("switch2");     boolean panel = true;      public tester()     {          setsize(100, 100);         setdefaultcloseoperation(exit_on_close);         setlayout(cc);         switch1.addactionlistener(this);         switch2.addactionlistener(this);         panel1.add(switch1);         panel1.add(text1);         panel2.add(switch2);         panel2.add(text2);         add(panel1, "panel 1");         add(panel2, "panel 2");         setvisible(true);     }      public void actionperformed(actionevent evt)     {         if(panel)         {             cardlayout pane = (cardlayout)this.getlayout();             pane.last(this);             panel = false;         }         else         {             cardlayout pane = (cardlayout)this.getlayout();             pane.first(this);             panel = true;         }         repaint();     }      public static void main(string[] args)     {         tester test = new tester();     } } 

the quick fix reference cardlayout , content pane directly. viz.

public void actionperformed(actionevent evt) {     if(panel)     {         //cardlayout pane = (cardlayout)this.getlayout();         cc.last(this.getcontentpane());         panel = false;     }     else     {         //cardlayout pane = (cardlayout)this.getlayout();         cc.first(this.getcontentpane());         panel = true;     }     //repaint(); not necessary either.. } 

a better fix design gui in jpanel (and use cardlayout in that), , create instance of jframe in show panel (instead of extending frame).


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -