简体   繁体   English

是否需要在Netbeans上创建向导(jDialog与CardLayout)?

[英]Need to create a WIZARD on Netbeans (jDialog vs CardLayout)?

I would like to create a "wizard" on a JDialog using the CardLayout, triggered by user pressing the New button from the menubar. 我想使用CardLayout在JDialog上创建一个“向导”,由用户按下菜单栏中的“新建”按钮触发。 In Netbeans I have created a JDialog through which I have a series of jPanels in CardLayout format. 在Netbeans中,我创建了一个JDialog,通过它我有一系列CardLayout格式的jPanel。 In my "New" menu item I wrote the following code to initiate the jDialog as follows, 在我的“新建”菜单项中,我编写了以下代码来启动jDialog,如下所示:

 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 如果有人可以带我在“Netbeans”上创建一个向导,我将永远感激不尽

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. 您的jDialogNew将BorderLayout设置为其布局而不是CardLayout,这意味着当您调用getLayout()以尝试将其放入不能保存BorderLayout的变量时,会抛出异常。 The classes are different so you cannot cast from one to another, causing a ClassCastException. 这些类是不同的,因此您无法从一个类转换为另一个类,从而导致ClassCastException。

A possible solution to this is to set your own layout for the jDialogNew. 一个可能的解决方案是为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. 我没有代码在我面前因此我无法检查自己,但尝试寻找像setLayout()这样的方法,并传入您选择的新布局。

you can do with following 你可以做以下

create JFrame -> Add "CARD LAYOUT"

add JPanels to project. 将JPanels添加到项目。 Design JPanels. 设计JPanels。 Customize init code of JFrame. 自定义JFrame的初始化代码。 Insert JPanels with this.add(jpanel name) . 使用this.add(jpanel name)插入JPanel。 for all jpanels setVisible(false) - then setVisible true which jpanel you want to start with. 对于所有jpanel setVisible(false)-然后setVisible true,要开始使用哪个jpanel。

The way I did it in Netbeans was very easy! 我在Netbeans中做到的方式非常简单! 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. 我所要做的就是在我的资源包中引入一个单独的JFrame(作为我整个包的一部分),在那个JFrame中,我创建了一个带有CardLayout的JPanel,在其下我创建了与该顶层相关的所有其他JPanel 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! 现在有了JFrame,我可以设置我的固定画布以及我需要构建和激活我的CardLayout“向导”对话框的所有其他内容! Then I had to call the new JFrame from with my application whenever the event was triggered. 然后,无论何时触发事件,我都必须从应用程序中调用新的JFrame。 It made life a whole lot easier and it works just great! 它使生活变得更加轻松,并且效果很好!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM