想实现这样一个功能:在弹出的窗口中,每隔一段时间,用画笔graphics.drawString()写入一行信息,我在paint()方法里这样写:
public void paint(Graphics g){ int count = 0;
String str = "";
while(count < 100){
str = result.get(count); //这一行错误
g.drawString(str, infoX, infoY);
count++;
infoY += 5;
}
}
错误如下:
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at MyFrame.paint(MerryMatch.java:195)
麻烦各位,帮忙解答一下这个问题的算法或者思路~
public void paint(Graphics g){ int count = 0;
String str = "";
while(count < 100){
str = result.get(count); //这一行错误
g.drawString(str, infoX, infoY);
count++;
infoY += 5;
}
}
错误如下:
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at MyFrame.paint(MerryMatch.java:195)
麻烦各位,帮忙解答一下这个问题的算法或者思路~
paint方法里面用while循环是不行的 因为paint调用一次不可能把全部的你要写入的信息都写出来解决办法 paint方法只写一次信息 然后把repaint()方法放在线程里面 每隔一段时间就重画楼主明白我的意思吗?
public void paint(Graphics g){int count = 0;
String str = "";str =max+" "+maxid; //俩全局变量
g.drawString(str, infoX, infoY);infoY += 5;
}
结果只重画出一次,两个值都是0,不解。还有个问题就是因为主程序的循环一直在改变这俩变量的值,可能早执行完了,我想想把这俩变量保存到hashMap什么的再取试试吧
public void paint(Graphics g){
int girlId = count;
int boyId = result.get(girlId); //这一行报错了
g.drawString(girlId+"选择了"+boyId, infoX, infoY);
infoY += 5;
count++;
}
public void paint(Graphics g){
int girlId = count;
int boyId = result.get(girlId); //这一行报错了
g.drawString(girlId+"选择了"+boyId, infoX, infoY);
infoY += 5;
count++;
}在调用result.get(girlId)方法之前result是不是为空 也就是你没初始化?