尝试运行程序时出现Java IllegalArgumentException

时间:2013-12-11 16:44:37

标签: java

我完成了我的java程序,编译时没有任何问题,但是当我尝试在带有JRE 6的MacBook上运行它时,我收到了这个错误。我不是最好读这个,但我相信错误来自我的卡片布局。

java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string
    at java.awt.CardLayout.addLayoutComponent(CardLayout.java:190)
    at java.awt.Container.addImpl(Container.java:1072)
    at java.awt.Container.add(Container.java:363)
    at RPS.<init>(RPS.java:75)
    at RPS.main(RPS.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

它在我的电脑上使用JRE 7运行,但现在它给了我一些问题。这是我的代码。

import javax.swing.JFrame;
import javax.swing.JPanel;

import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;

import java.awt.CardLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;
import java.awt.BorderLayout;

public class RPS extends JFrame {

  ButtonGroup P1choices, P2choices;

  public static void main(String[] args) {
   new RPS();
  }
   public RPS() {
super("Rock, Paper, Scissors");

setSize(300,300);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);

final CardLayout cardLayout = new CardLayout(10,10);
final JPanel cardPanel = new JPanel(cardLayout);

JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();

final JRadioButton P1Rock = new JRadioButton("Rock");
final JRadioButton P1Paper = new JRadioButton("Paper");
final JRadioButton P1Scissors = new JRadioButton("Scissors");

final JRadioButton P2Rock = new JRadioButton("Rock");
final JRadioButton P2Paper = new JRadioButton("Paper");
final JRadioButton P2Scissors = new JRadioButton("Scissors");

JButton nextButton = new JButton("Next");
JButton finish = new JButton("Finish");

P1choices = new ButtonGroup();
P1choices.add(P1Rock);
P1choices.add(P1Paper);
P1choices.add(P1Scissors);

P2choices = new ButtonGroup();
P2choices.add(P2Rock);
P2choices.add(P2Paper);
P2choices.add(P2Scissors);

final JLabel statusLabel = new JLabel(" ");
JLabel P1turn = new JLabel("It is Player 1's turn. Choose:");
JLabel p2turn = new JLabel("It is Player 2's turn. Choose:");

panel1.add(P1turn);
panel1.add(P1Rock);
panel1.add(P1Paper);
panel1.add(P1Scissors);
panel1.add(nextButton);
panel1.setLayout(new GridLayout(5,1));

panel2.add(p2turn);
panel2.add(P2Rock);
panel2.add(P2Paper);
panel2.add(P2Scissors);
panel2.add(finish);
panel2.setLayout(new GridLayout(5,1));

cardPanel.add(panel1);
cardPanel.add(panel2);

add(cardPanel, BorderLayout.CENTER);
add(statusLabel, BorderLayout.NORTH);

nextButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
    cardLayout.next(cardPanel);
}
});
finish.addActionListener(new ActionListener(){
 public void actionPerformed(ActionEvent e){
    if (P1Rock.isSelected() && P2Scissors.isSelected()){
     statusLabel.setText("Player 1 wins! Rock crushes Scissors.");
   } 
    if (P1Rock.isSelected() && (P2Paper.isSelected())) {
     statusLabel.setText("Player 2 wins! Paper covers Rock.");
   }
    if (P1Rock.isSelected() && (P2Rock.isSelected())) {
     statusLabel.setText("It's a tie!");
   }
    if (P1Paper.isSelected() && P2Rock.isSelected()){
     statusLabel.setText("Player 1 wins! Paper covers Rock.");
   }
    if (P1Paper.isSelected() && P2Scissors.isSelected()) {
     statusLabel.setText("Player 2 wins! Scissors cut Paper.");
   }
    if (P1Paper.isSelected() && P2Paper.isSelected()) {
     statusLabel.setText("It's a tie!");
   }
    if (P1Scissors.isSelected() && P2Paper.isSelected()){
     statusLabel.setText("Player 1 wins! Scissors cut Paper.");
   }
    if (P1Scissors.isSelected() && P2Rock.isSelected()) {
     statusLabel.setText("Player 2 wins! Rock crushes Scissors.");
   }
    if (P1Scissors.isSelected() && P2Scissors.isSelected()) {
     statusLabel.setText("It's a tie!");
   }
 }
});
  }
}

请让我知道我可以做些什么来解决这个问题。谢谢!

编辑: 我的家用电脑运行JRE 7,但错误发生在我的学校Macbook上。我相信那些正在运行JRE 6,因为我们无法在没有管理权的情况下进行更新,我们的技术总监也被逮捕了。

2 个答案:

答案 0 :(得分:0)

您正尝试对BorderLayout使用CardLayout约束。

查看CardLayout文档,找出应该使用的约束。

答案 1 :(得分:0)

在向卡片布局添加项目时,您需要传入一个字符串:

cardPanel.add(panel1, "player1");
cardPanel.add(panel2, "player2");

然后,您可以设置显示的卡:

cardLayout.show(cardPanel, "player1");

cardLayout.show(cardPanel, "player2");

(Tutorial)