主要出现在 p.add(d1 = new Die() ); p.add(d2 = new Die() ); …… l = new Label ();这几行上面,可是我不了解呀,新手啊,大家多帮助啊
说个小问题(估计是楼主笔误吧),第一句应该是:import java.awt.*;吧,少了一个i.
我已经帮你检查了你的代码了,都改过来了,应该可以运行了.你的错误有百分之30是粗心引起的,还有百分之70是你用变量都不带声名引起的,不知道你这个习惯是从那来的???脚本语言嘛吗???不过你以后不要再犯这样的错误了,一般是没人管你这样非技术性的问题的.改过的代码: import java.awt.*; import java.awt.event.*; class aaa extends Frame implements ActionListener { boolean firstRoll = true; Die d1,d2; Label l; public aaa(String title) { super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
System.exit(0);
} });
Panel p = new Panel();
p.add(d1 = new Die() ); p.add(d2 = new Die() ); add(p, BorderLayout.NORTH); l = new Label (); add(l); l.setAlignment(Label.CENTER); add(l); Button b = new Button("Roll"); b.addActionListener(this); p = new Panel(); p.add(b); add(p,BorderLayout.SOUTH); setSize(200, 200); setVisible(true); }
public void actionPerformed(ActionEvent e) { int value1 = (int) (Math.random() * 6) + 1; int value2 = (int) (Math.random() * 6 ) + 1; d1.draw(value1); d2.draw(value2); int sum = value1 + value2; int oldSum = 0; ///我不知道你这个变量要初始化什么值,我就给它赋0了. if (firstRoll) { l.setText(""); if (sum == 7 || sum == 11) { l.setText("You Win!"); } else if (sum == 2 || sum == 3 || sum == 12) { l.setText("You Lose."); } else { oldSum = sum; firstRoll = false; } return; } if (sum == oldSum) { l.setText("You made your point. You win."); firstRoll = true; return; }
} public static void main(String args) { new aaa("Craps"); }
} class Die extends Canvas { private int nSports = 0; public void draw(int nSports) { this.nSports = nSports; repaint(); } public Dimension getPreferredSize() { return new Dimension(30,30); }
对了,你的代码要还想运行的话,还得改两个地方,我刚才只注意没有编译错误了,没注意还有运行错误. 第一个地方是把你的第一个类,好像是叫Craps吧,在它前面加上public修饰词; 第二个地方就是你的Craps类里的main方法都些错了,应该是这样写: public static void main(String[] args) 你掉了[]这个.这样一来你的程序才能运行.好了,加油吧!一开始都是会错很多的,耐心看一下出错代码,自己多多克服一下,习惯一下就好了.
呜呼久不遇到阳春,高手就是高手。我修改后的代码是这个呀,给老师报个喜,已经能成功的编译运行了,感激涕零ing……import java.awt.*; import java.awt.event.*; class Craps extends Frame implements ActionListener { boolean firstRoll = true; Die d1,d2; Label l; public Craps(String title) { super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
System.exit(0);
} });
Panel p = new Panel();
p.add(d1 = new Die() ); p.add(d2 = new Die() ); add(p, BorderLayout.NORTH); l = new Label (); add(l); l.setAlignment(Label.CENTER); add(l); Button b = new Button("Roll"); b.addActionListener(this); p = new Panel(); p.add(b); add(p,BorderLayout.SOUTH); setSize(200, 200); setVisible(true); }
public void actionPerformed(ActionEvent e) { int value1 = (int) (Math.random() * 6) + 1; int value2 = (int) (Math.random() * 6 ) + 1; d1.draw(value1); d2.draw(value2); int sum = value1 + value2; int oldSum = 0; ///我不知道你这个变量要初始化什么值,我就给它赋0了. if (firstRoll) { l.setText(""); if (sum == 7 || sum == 11) { l.setText("You Win!"); } else if (sum == 2 || sum == 3 || sum == 12) { l.setText("You Lose."); } else { oldSum = sum; firstRoll = false; } return; } if (sum == oldSum) { l.setText("You made your point. You win."); firstRoll = true; return; }
} public static void main(String[] args) { new Craps("Craps"); }
} class Die extends Canvas { private int nSports = 0; public void draw(int nSports) { this.nSports = nSports; repaint(); } public Dimension getPreferredSize() { return new Dimension(30,30); }
p.add(d1 = new Die() );
p.add(d2 = new Die() );
……
l = new Label ();这几行上面,可是我不了解呀,新手啊,大家多帮助啊
import java.awt.*;
import java.awt.event.*;
class aaa extends Frame implements ActionListener {
boolean firstRoll = true;
Die d1,d2;
Label l; public aaa(String title) {
super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
Panel p = new Panel();
p.add(d1 = new Die() );
p.add(d2 = new Die() );
add(p, BorderLayout.NORTH);
l = new Label ();
add(l);
l.setAlignment(Label.CENTER);
add(l);
Button b = new Button("Roll");
b.addActionListener(this);
p = new Panel();
p.add(b);
add(p,BorderLayout.SOUTH);
setSize(200, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
int value1 = (int) (Math.random() * 6) + 1;
int value2 = (int) (Math.random() * 6 ) + 1;
d1.draw(value1);
d2.draw(value2);
int sum = value1 + value2;
int oldSum = 0; ///我不知道你这个变量要初始化什么值,我就给它赋0了.
if (firstRoll) {
l.setText("");
if (sum == 7 || sum == 11) {
l.setText("You Win!");
}
else
if (sum == 2 || sum == 3 || sum == 12) {
l.setText("You Lose.");
}
else {
oldSum = sum;
firstRoll = false;
}
return;
}
if (sum == oldSum) {
l.setText("You made your point. You win.");
firstRoll = true;
return;
}
if (sum == 7) {
l.setText("You lose.");
firstRoll = true;
}
}
public static void main(String args) {
new aaa("Craps");
}
}
class Die extends Canvas {
private int nSports = 0;
public void draw(int nSports) {
this.nSports = nSports;
repaint();
}
public Dimension getPreferredSize() {
return new Dimension(30,30);
}
public void paint(Graphics g) {
int width = this.getSize().width;
int height = this.getSize().height;
g.drawRoundRect(0, 0, width, 1, 5, 5); // 这个方法需要六个参数,但你有个width-1这样的参数,我猜是你粗心写错了吧.
int cx = width / 2;
int cy = height / 2;
switch (nSports) {
case 1: g.fillOval(cx - 2, cy - 2, 4, 4);
break;
case 2: g.fillOval(2, 2, 4, 4);
g.fillOval(width - 6, height - 6, 4, 4);
break;
case 3: g.fillOval(2, 2, 4, 4);
g.fillOval(cx - 2, cy - 2, 4, 4);
g.fillOval(width - 6, height - 6, 4, 4);
break;
case 4: g.fillOval(2, 2, 4, 4);
g.fillOval(width - 6, 2, 4, 4);
g.fillOval(2, height - 6, 4, 4);
g.fillOval(width - 6, height - 6, 4, 4);
break;
case 5: g.fillOval(2, 2, 4, 4);
g.fillOval(width - 6, 2, 4, 4);
g.fillOval(cx - 2, cy - 2, 4, 4);
g.fillOval(2, height - 6 ,4, 4);
g.fillOval(width - 6, height - 6, 4, 4);
break;
case 6: g.fillOval(2, 2, 4, 4);
g.fillOval(width - 6, 2, 4, 4);
g.fillOval(2, cy - 2, 4, 4);
g.fillOval(width - 6, cy - 2, 4, 4);
g.fillOval(2, height - 6, 4, 4);
g.fillOval(width - 6, height - 6, 4, 4);
}
}
}
第一个地方是把你的第一个类,好像是叫Craps吧,在它前面加上public修饰词;
第二个地方就是你的Craps类里的main方法都些错了,应该是这样写:
public static void main(String[] args) 你掉了[]这个.这样一来你的程序才能运行.好了,加油吧!一开始都是会错很多的,耐心看一下出错代码,自己多多克服一下,习惯一下就好了.
import java.awt.event.*;
class Craps extends Frame implements ActionListener {
boolean firstRoll = true;
Die d1,d2;
Label l; public Craps(String title) {
super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
Panel p = new Panel();
p.add(d1 = new Die() );
p.add(d2 = new Die() );
add(p, BorderLayout.NORTH);
l = new Label ();
add(l);
l.setAlignment(Label.CENTER);
add(l);
Button b = new Button("Roll");
b.addActionListener(this);
p = new Panel();
p.add(b);
add(p,BorderLayout.SOUTH);
setSize(200, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
int value1 = (int) (Math.random() * 6) + 1;
int value2 = (int) (Math.random() * 6 ) + 1;
d1.draw(value1);
d2.draw(value2);
int sum = value1 + value2;
int oldSum = 0; ///我不知道你这个变量要初始化什么值,我就给它赋0了.
if (firstRoll) {
l.setText("");
if (sum == 7 || sum == 11) {
l.setText("You Win!");
}
else
if (sum == 2 || sum == 3 || sum == 12) {
l.setText("You Lose.");
}
else {
oldSum = sum;
firstRoll = false;
}
return;
}
if (sum == oldSum) {
l.setText("You made your point. You win.");
firstRoll = true;
return;
}
if (sum == 7) {
l.setText("You lose.");
firstRoll = true;
}
}
public static void main(String[] args) {
new Craps("Craps");
}
}
class Die extends Canvas {
private int nSports = 0;
public void draw(int nSports) {
this.nSports = nSports;
repaint();
}
public Dimension getPreferredSize() {
return new Dimension(30,30);
}
public void paint(Graphics g) {
int width = this.getSize().width;
int height = this.getSize().height;
g.drawRoundRect(0, 0, width - 1, height - 1, 5, 5); // 这个方法需要六个参数,但你有个width-1这样的参数,我猜是你粗心写错了吧.
int cx = width / 2;
int cy = height / 2;
switch (nSports) {
case 1: g.fillOval(cx - 2, cy - 2, 4, 4);
break;
case 2: g.fillOval(2, 2, 4, 4);
g.fillOval(width - 6, height - 6, 4, 4);
break;
case 3: g.fillOval(2, 2, 4, 4);
g.fillOval(cx - 2, cy - 2, 4, 4);
g.fillOval(width - 6, height - 6, 4, 4);
break;
case 4: g.fillOval(2, 2, 4, 4);
g.fillOval(width - 6, 2, 4, 4);
g.fillOval(2, height - 6, 4, 4);
g.fillOval(width - 6, height - 6, 4, 4);
break;
case 5: g.fillOval(2, 2, 4, 4);
g.fillOval(width - 6, 2, 4, 4);
g.fillOval(cx - 2, cy - 2, 4, 4);
g.fillOval(2, height - 6 ,4, 4);
g.fillOval(width - 6, height - 6, 4, 4);
break;
case 6: g.fillOval(2, 2, 4, 4);
g.fillOval(width - 6, 2, 4, 4);
g.fillOval(2, cy - 2, 4, 4);
g.fillOval(width - 6, cy - 2, 4, 4);
g.fillOval(2, height - 6, 4, 4);
g.fillOval(width - 6, height - 6, 4, 4);
}
}
}也许你们不屑一顾,但这是我在java这条路上走出的第一步啊……还有,老师的教诲,我都记下了!!!