java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string

Ich habe Schwierigkeiten mit CardLayout Manager in meinem code. Ich kann nicht herausfinden, warum bekomme ich diese exception. Ich bin übergeben Sie eine Zeichenfolge in das CardLayout.show() - Methode, aber trotzdem bekomme ich diesen Fehler. Bitte helfen Sie. Das ist meine main-Klasse.

@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();
        }
    });
}

Dies ist außerhalb meiner Klasse, von dem ich am tauschen der Karte.

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;
}
}

Ich brauche einige Hilfe in Bezug auf diese ganz dringend sogar. 🙁

  • Bitte posten Sie Ihre stacktrace als gut.
  • Bitte fügen Sie die Stapel-Ablaufverfolgung, die in Ihrer Frage und sagen Sie uns, welche Zeilen in Ihrem Programm entspricht den Zeilennummern in stack-trace.
InformationsquelleAutor Jha | 2011-12-03
Schreibe einen Kommentar