2014-06-20 189 views
0

我有一個jInternalPanejDesktopPane內。 jDesktopPane位於具有BorderLayout佈局的jPanel之內。如何獲取jInternalPane的卡片佈局?

在我的內部窗格中,我試圖以編程方式切換卡片。我有以下的,相應的代碼,打破:

public void switchCards() { 
    CardLayout cl = (CardLayout)(internalFrame1.getLayout()); 
    cl.show(internalFrame1, "card1"); //Where card1 is a jPanel 
} 

然而,在錯誤跟蹤,我可以看到以下內容:

javax.swing.plaf.basic.BasicInternalFrameUI$Handler cannot be cast to java.awt.CardLayout

有人可以請指出我在正確的方向來妥善處理這個錯誤?我很想學習如何做到這一點!

非常感謝提前。

+0

放的JPanel到的JInternalFrame – mKorbel

回答

2

您將在JInternalFrame的內容窗格上使用CardLayout,而不是內部框架本身。

只需將內容窗格的佈局:

Container contentPane = internalFrame.getContentPane(); 
contentPane.setLayout(new CardLayout()); 

contentPane.add(panel1, "Card1"); 
contentPane.add(panel2, "Card2"); 

然後你switchCards()方法是:

Container contentPane = internalFrame.getContentPane(); 
CardLayout cl = (CardLayout)(contentPane.getLayout()); 
cl.show(contentPane, "card1"); //Where card1 is a jPanel 
+0

很抱歉這麼晚纔回復!我正在旅途中。感謝您解決我的任何困惑。你的代碼完整地解釋了我錯過的東西,現在我明白了。 謝謝你的時間! – Andrew