返回信息流//Craps.java
import java.awt.*;
//import java.awt.Container;
import java.awt.event.*;
import java.applet.Applet;
import javax.swing.*;
public class CCraps extends Applet implements ActionListener{
final int WON= 0, LOST = 1,CONTINUE =2 ;
boolean firstRoll = true;
int sumofDice = 0;
int myPoint = 0;
int gameStatus = CONTINUE;
JLabel die1Label,die2Label,sumLabel,pointLabel;
JTextField die1Field,die2Field,sumField,pointField;
JButton rollButton;
public void init()
{
Container container= getContentPane();
container.setLayout(new FlowLayout());
die1Label = new JLabel("Die 1");
container.add(die1Label);
die1Field = new JTextField(10);
die1Field.setEditable(false);
container.add(die1Field);
die2Label = new JLabel("Die 2");
container.add(die2Label);
die2Field = new JTextField(10);
die2Field.setEditable(false);
container.add(die2Field);
sumLabel = new JLabel("Sum is");
container.add(sumLabel);
sumField = new JTextField(10);
sumField.setEditable(false);
container.add(sumField);
pointLabel = new JLabel("Point is");
container.add(pointLabel);
pointField = new JTextField(10);
pointField.setEditable(false);
container.add(pointField);
rollButton = new JButton("Roll Dice");
rollButton.addActionListener(this);
container.add(rollButton);
}
public void actionPerformed(ActionEvent actionEvent)
{
if(firstRoll)
{
sumofDice = rollDice();
switch ( sumofDice )
{
case 7:case 11:
gameStatus = WON;
pointField.setText("");
break;
case 2:case 3:case 12:
gameStatus = LOST;
pointField.setText("");
break;
default:
gameStatus =CONTINUE;
myPoint = sumofDice;
pointField.setText(Integer.toString(myPoint));
firstRoll = false;
break;
}
}
else{
sumofDice = rollDice();
if(sumofDice == myPoint)
gameStatus = WON;
else
if(sumofDice == 7)
gameStatus = LOST;
}
displayMessge();
}
public int rollDice()
{
int die1,die2,sum;
die1= 1 + (int) (Math.random() * 6);
die2= 1 + (int) (Math.random() * 6);
sum = die1 + die2;
die1Field.setText(Integer.toString(die1));
die2Field.setText(Integer.toString(die2));
sumField.setText(Integer.toString(sum));
return sum;
}
public void displayMessge()
{
if(gameStatus == CONTINUE)
showStatus("Roll.again.");
else
{
if(gameStatus == WON)
showStatus("Player wins. " +
"Click Roll Dice to play again.");
else
showStatus("Player loses. " +
"Click Roll Dice to play again");
firstRoll = true;
}
}
}
红色的Container container= getContentPane();表示错误,为什么有这个错误啊??不懂??求指导。。。。。
这是一条镜像帖。来源:北邮人论坛 / java / #19983同步于 2011/9/6
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
请教个菜鸟问题??
zishi
2011/9/6镜像同步4 回复
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
这不错误描述很清楚啊……没有getContentPane()这个方法,Applet没有,继承它的CCraps也没有。
swing中的RootPaneContainer接口定义了getContentPane,JApplet实现了,awt的Applet可没有implements这个接口,它是属于swing的。
换容器吧,extends JApplet。除非你执着于非得使用AWT组件,否则就换用Swing的JApplet。要是非得用AWT,把container去了,直接this调那些方法就完了。
lz去看看Swing和AWT的区别和关系吧,还有Swing重量组件的构成。