2011-12-03 43 views
3

我在我的代碼中遇到CardLayout Manager問題。我無法弄清楚爲什麼我得到這個例外。我在CardLayout.show()方法中傳遞一個字符串,但仍然出現此錯誤。請幫忙。這是我的主要課程。java.lang.IllegalArgumentException:無法添加到佈局:約束必須是字符串

@SuppressWarnings("serial") 
public class Main extends JFrame implements ActionListener { 

final static String mainMenuPanel = "Main Menu"; 
final static String creditsPanel = "Credits"; 
final static String introPanel = "Introduction"; 

private CardLayout cardLayout = new CardLayout(); 
private JPanel cards = new JPanel(cardLayout); 


public Main(){ 
    //Create and set up the window. 
    super(); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setLayout(new CardLayout()); 
    //this.pack(); 
    this.setVisible(true); 
    this.setSize(new Dimension(800,600)); 
    this.setLocationRelativeTo(null); 
    this.setTitle("Wise Frog Productions."); 
    cards.add(new IntroGamePanel(),introPanel); 
    cards.add(new MainMenu(),mainMenuPanel); 
    this.add(cards); 
    swapView(mainMenuPanel); 

} 
public void swapView(String s){ 
    cardLayout.show(cards,s); 
} 
public void actionPerformed(ActionEvent event){ 

} 

public static void main(String[] args){ 
    javax.swing.SwingUtilities.invokeLater(new Runnable(){ 
     public void run(){ 
      new Main(); 
     } 
    }); 
} 

這是我從外面交換卡片的課程。

public class IntroGamePanel extends JPanel implements MouseInputListener{ 
private Main main; 
ImageIcon beginButtonIcon1 = new ImageIcon(IntroGamePanel.class.getResource("begin_0.gif")); 
ImageIcon beginButtonIcon2 = new ImageIcon(IntroGamePanel.class.getResource("begin_1.gif")); 
JButton beginButton = new JButton("", beginButtonIcon1); 

public IntroGamePanel(){ 
    super(); 
    this.setOpaque(true); 
    this.add(beginButton); 
    this.setPreferredSize(new Dimension(800,600)); 
    beginButton.setPreferredSize(new Dimension(200,36)); 
    beginButton.setLocation(240,40); 
    beginButton.addMouseMotionListener(this); 
    beginButton.addMouseListener(this); 
    beginButton.setEnabled(true); 
} 

@Override 
//This will take us to the main menu screen. 
public void mouseClicked(MouseEvent e) {  
    if(main != null){ 
     main.swapView(Main.mainMenuPanel); 
    } 

} 

@Override 
public void mouseEntered(MouseEvent e) { 
    beginButton.setIcon(beginButtonIcon2);  
} 

@Override 
public void mouseExited(MouseEvent e) { 
    beginButton.setIcon(beginButtonIcon1); 
} 

@Override 
public void mousePressed(MouseEvent e) { 
    //not needed 
} 

@Override 
public void mouseReleased(MouseEvent e) { 
    //not needed 
} 

@Override 
public void mouseDragged(MouseEvent e) { 
    //not needed 
} 

@Override 
public void mouseMoved(MouseEvent e) { 
    //not needed 
} 
public void getMain(Main main){ 
    this.main = main; 
} 
} 

我需要一些實際上很緊急的幫助。 :(

+0

請發佈你的stacktrace以及 –

+0

請在你的問題包括堆棧跟蹤和te請問我們你的程序中的哪些行對應於堆棧跟蹤中的行號。 –

回答

6

這個錯誤來自行

this.add(cards); 

既然你改變這種佈局爲CardLayout,你必須指定一個字符串作爲第二個參數。

你確定你想Main到有一個CardLayout?你的面板cards已經包含這樣的佈局

+0

:|這是一個相當愚蠢的錯誤。不,我不打算讓'Main'擁有'CardLayout'。如果你可以幫助我另一件事,我在從introPanel切換到mainMenuPanel時遇到問題。我已經在網上查了一些東西,但我無法弄清楚如何做到這一點。 – Jha

+0

@Jha你在'IntroGamePanel'裏面調用'main.swapView'。這是一個從未初始化的領域。也許你想把一個引用作爲參數傳遞給你的'IntroGamePanel'的構造函數,然後將它存儲在這個字段中。 – Howard

+0

@Jha:這是一個單獨的問題,不是嗎?爲什麼不接受霍華德的回答,並在別處問別的問題。 –

相關問題