I would like to create a "wizard" on a JDialog using the CardLayout, triggered by开发者_如何学Python user pressing the New button from the menubar. In Netbeans I have created a JDialog through which I have a series of jPanels in CardLayout format. In my "New" menu item I wrote the following code to initiate the jDialog as follows,
CardLayout cl = (CardLayout) jDialogNew.getLayout();
cl.preferredLayoutSize(jDialogNew);
cl.show(jDialogNew, "card1");
However, the compiler comes up with the following error,
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.awt.BorderLayout cannot be cast to java.awt.CardLayout
If anyone is out there that can take me through creating a wizard on "Netbeans" I'd be eternally grateful
Your jDialogNew has a BorderLayout set as its layout and and not a CardLayout, meaning that when you call getLayout() to try to fit it into a variable that cant hold a BorderLayout an exception is thrown. The classes are different so you cannot cast from one to another, causing a ClassCastException.
A possible solution to this is to set your own layout for the jDialogNew. I dont have code infront of me so I cant check myself, but try looking for a method like setLayout(), and pass in a new layout of your choice.
you can do with following
create JFrame -> Add "CARD LAYOUT"
add JPanels to project. Design JPanels. Customize init code of JFrame. Insert JPanels with this.add(jpanel name)
. for all jpanels setVisible(false) - then setVisible true which jpanel you want to start with.
The way I did it in Netbeans was very easy! All I had to do was to was to introduce a separate JFrame in my resources package (being a part of my overall package) and in that JFrame I created a JPanel with the CardLayout, under which I created all my other JPanels relating to that top JPanel. Now having the JFrame I could set my fixed canvas plus everything else I needed to construct and activate my CardLayout "Wizard" dialogue box! Then I had to call the new JFrame from with my application whenever the event was triggered. It made life a whole lot easier and it works just great!
精彩评论