简体   繁体   English

java.lang.IllegalArgumentException:无法添加到layout:约束必须是字符串

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

I am having difficulty with CardLayout Manager in my code. 我的代码中的CardLayout Manager遇到了问题。 I can't figure out why i am getting this exception. 我无法弄清楚为什么我会得到这个例外。 I am passing a string in the CardLayout.show() method but still i get this error. 我在CardLayout.show()方法中传递一个字符串但仍然出现此错误。 Please help. 请帮忙。 This is my main class. 这是我的主要课程。

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

This is my outside class from which i am swapping the card. 这是我在外面上课,我正在交换卡片。

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

I need some help regarding this quite urgently actually. 实际上,我迫切需要一些帮助。 :( :(

The error comes from the line 该错误来自该行

this.add(cards);

Since you changed the layout of this into a CardLayout you have to specify a string as second argument. 由于您将此布局更改为CardLayout ,因此必须将字符串指定为第二个参数。

Are you sure you wanted Main to have a CardLayout ? 你确定要MainCardLayout吗? Your panel cards already contains such a layout. 您的面板cards已经包含这样的布局。

暂无
暂无

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

相关问题 获取异常:java.lang.IllegalArgumentException:无法添加到布局:约束必须为字符串(或为null) - getting Exception : java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null) 向JFrame添加JPanel的问题:java.lang.IllegalArgumentException:无法添加到布局:约束必须是字符串(或null) - Issue with adding a JPanel to JFrame: java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null) 线程“ main”中的异常java.lang.IllegalArgumentException:无法添加到布局:约束必须为GridBagConstraint - Exception in thread “main” java.lang.IllegalArgumentException: cannot add to layout: constraints must be a GridBagConstraint QAF:java.lang.IllegalArgumentException:必须定义添加操作的键 - QAF: java.lang.IllegalArgumentException: Key for add operation must be defined java.lang.IllegalArgumentException:地图大小(0)必须> = 1 - java.lang.IllegalArgumentException: Map size (0) must be >= 1 java.lang.IllegalArgumentException:方法不能为null - java.lang.IllegalArgumentException: Method must not be null java.lang.IllegalArgumentException:上下文不得为null - java.lang.IllegalArgumentException: Context must not be null java.lang.IllegalArgumentException:必须为StyledEditorKit - java.lang.IllegalArgumentException: Must be StyledEditorKit java.lang.IllegalArgumentException:目标不能为空 - java.lang.IllegalArgumentException: Target must not be null java.lang.illegalargumentexception 无法将 java.lang.string 字段设置为 java.lang.String - java.lang.illegalargumentexception cannot set java.lang.string field to java.lang.String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM