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

发布于 2024-12-19 08:48:18 字数 2734 浏览 2 评论 0原文

我在代码中使用 CardLayout Manager 时遇到困难。我不明白为什么我会得到这个异常。我在 CardLayout.show() 方法中传递一个字符串,但仍然收到此错误。请帮忙。这是我的主课。

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

实际上我非常迫切地需要一些帮助。 :(

I am having difficulty with CardLayout Manager in my code. 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. 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. :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清秋悲枫 2024-12-26 08:48:18

该错误来自以下行:

this.add(cards);

既然您将其布局更改为 CardLayout,则必须指定一个字符串作为第二个参数。

您确定希望 Main 有一个 CardLayout 吗?您的面板cards 已经包含这样的布局。

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.

Are you sure you wanted Main to have a CardLayout? Your panel cards already contains such a layout.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文