2011-12-03 7 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; 
} 
} 

これは非常に緊急に役立つものです。あなたはCardLayoutにこのレイアウトを変更したので:(

+0

スタックトレースも投稿してください –

+0

質問にスタックトレースを含めてくださいプログラム内のどの行がスタックトレースの行番号に対応しているのでしょうか? –

答えて

6

エラーは、第二引数として文字列を指定する必要がライン

this.add(cards); 

から来ている。

あなたはあなたにMainを望んでよろしいですCardLayout?あなたのパネルcardsにはすでにそのようなレイアウトが含まれています

+0

:| ...それはかなりばかげた過ちだった。いいえ、私は 'Main'に' CardLayout'を持たせるつもりはありませんでした。別のもので私を助けることができる場合、私はintroPanelからmainMenuPanelへの切り替えに問題があります。私はものをオンラインでチェックアウトしていますが、私はそれを行う方法を理解することができません。 – Jha

+0

@ Jhaあなたは 'IntroGamePanel'の中で' main.swapView'を呼び出しています。それは初期化されていないフィールドです。おそらく、あなたは 'IntroGamePanel'のコンストラクタに引数として参照を渡し、それをこのフィールドに格納したいと思うかもしれません。 – Howard

+0

@ Jha:これは別の質問ですね。どうしてアップアップして、ハワードの答えを受け入れて、その別の質問を別の場所に聞かせてください。 –

関連する問題