这段java小程序里的这一句哪里错了

importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener... import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
public class Dice {
int d1=0,d2=0;
float ratio = 1.0f;
static int win = 0;
static int lose = 0;
Random r = new Random();
int sum;
Frame frame;
TextArea area;
Button button;
String s = "Now you got :";
public static void main(String[] args) {
new Dice().buildGUI();
createThread c1 = new createThread();// 就是这句!!!
Thread t = new Thread(c1);
t.start();
}
public void buildGUI() {
frame = new Frame("Dice Game");
area = new TextArea();
//button = new Button("Try");
//button.addActionListener(new pressListener());
frame.add(BorderLayout.CENTER, area);
//frame.add(BorderLayout.SOUTH, button);
frame.setVisible(true);
frame.setSize(300, 300);
frame.setLocation(100, 100);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(-1);
}
});
}
public void createNums() {
d1 = r.nextInt(7);
d2 = r.nextInt(7);
area.append(s + d1 + " and " + d2 + " so you");
judge(d1, d2);
}
public void judge(int d1, int d2) {
sum = d1 + d2;
ratio = (float) lose / win;
if (sum > 7) {
win++;
area.append(" win! " + win + " " + ratio + '\n');
} else {
lose++;
area.append(" lose... " + lose + " " + ratio + '\n');
}
}
/*public class pressListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
createNums();
}
}*/
}
class createThread implements Runnable {
Dice d = new Dice();
public void run() {
try {
Thread.sleep(50);
d.createNums();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
展开
 我来答
飘飘天写
2013-04-18 · 超过15用户采纳过TA的回答
知道答主
回答量:37
采纳率:0%
帮助的人:34.6万
展开全部
这行代码[area.append(s + d1 + " and " + d2 + " so you");]中area为null。
area你是通过方法buildGUI()来初始化的,在新的thread中你没有调用该方法,你是在main方法中对一个匿名Dice调用的[new Dice().buildGUI()]。

下面是改过的,把main中Dice引用传给thread。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
public class Dice {
int d1 = 0, d2 = 0;
float ratio = 1.0f;
static int win = 0;
static int lose = 0;
Random r = new Random();
int sum;
Frame frame;
TextArea area;
Button button;
String s = "Now you got :";
public static void main(String[] args) {
Dice dice = new Dice();
dice.buildGUI();
createThread c1 = new createThread(dice);// 就是这句!!!
Thread t = new Thread(c1);
t.start();
}
public void buildGUI() {
frame = new Frame("Dice Game");
area = new TextArea();
// button = new Button("Try");
// button.addActionListener(new pressListener());
frame.add(BorderLayout.CENTER, area);
// frame.add(BorderLayout.SOUTH, button);
frame.setVisible(true);
frame.setSize(300, 300);
frame.setLocation(100, 100);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(-1);
}
});
}
public void createNums() {
d1 = r.nextInt(7);
d2 = r.nextInt(7);
area.append(s + d1 + " and " + d2 + " so you");
judge(d1, d2);
}
public void judge(int d1, int d2) {
sum = d1 + d2;
ratio = (float) lose / win;
if (sum > 7) {
win++;
area.append(" win! " + win + " " + ratio + '\n');
} else {
lose++;
area.append(" lose... " + lose + " " + ratio + '\n');
}
}
/*
* public class pressListener implements ActionListener { public void actionPerformed(ActionEvent e) { createNums();
* } }
*/
}
class createThread implements Runnable {
Dice d = new Dice();

public createThread(Dice d) {
this.d = d;
}
public void run() {
try {
Thread.sleep(50);
d.createNums();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式