[求助]菜鸟的问题 JTextField控件的SetText()里的参数必须是String 类型的 而你传进去的是int型的 所以你把它改成SetText(Integer.toString(int))就OK了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 改是改了编译也通过了但是还是有错误sum和z还有f结果不对,请问再怎么改啊?我今天想了很久了,没想出来,没办法,脑子笨. 只要把 t2.setText(z);改成 t2.setText(String.ValueOf(z));就可以了原因是: setText的参数应该是字符串,而不是整数。 用String.ValueOf( )可以实现类型转换。 程序已经改了,如下:import java.awt.event.*;import java.awt.*;import javax.swing.*;public class z extends JApplet implements ActionListener{ JLabel l1,l2,l3,l4; JTextField t1,t2,t3,t4; JButton toss; public void init() { Container c=getContentPane(); c.setLayout(new FlowLayout()); l1=new JLabel("掷的结果"); c.add(l1); t1=new JTextField(10); c.add(t1); l2=new JLabel("正的总次数"); c.add(l2); t2=new JTextField(10); t2.setEditable(false); c.add(t2); l3=new JLabel("反的总次数"); c.add(l3); t3=new JTextField(10); t3.setEditable(false); c.add(t3); l4=new JLabel("掷的总次数"); c.add(l4); t4=new JTextField(10); t4.setEditable(false); c.add(t4); toss=new JButton("开始掷"); toss.addActionListener(this); c.add(toss); } public void actionPerformed(ActionEvent e) { play(); } public String play() { int r,sum,z=0,f=0; String k=""; sum=z+f; r=0+(int)(Math.random()*2); t1.setText(Integer.toString(r)); if(r==1) { ++z; t2.setText(Integer.toString(z)); t4.setText(Integer.toString(sum)); k+="正"; } else { ++f; t3.setText(Integer.toString(f)); t4.setText(Integer.toString(sum)); k+="反"; } return k; }}但是还有一个问题,在运行的过程中正的次数,反的次数还有总的次数不能累积相加,他们总是在1和0之间徘徊,请问是什么问题?改怎么改?再次辛苦大家了,谢谢. 通过java代码如何把改变xml文件的字符编码格式 如下文:foot变量没有初始化为什么可以进行比较? java默认构造函数的访问权限 java字符串处理 懂swing和文件下载的进 昨天群硕的笔试题(长沙),希望对大家有点帮助 监听器接口的实现方法放在哪个类中好?怎么理解的?????在线等,谢谢!! out.println()在这里是什么意思?不是问System.out.println() 强烈推荐:[原创]不是我的错——为JBuilder的光标错位问题平反! 我问:super [求助]请高手们帮忙 [求助]菜鸟的问题,跪求帮助.
编译也通过了
但是还是有错误
sum和z还有f结果不对,请问再怎么改啊?我今天想了很久了,没想出来,没办法,脑子笨.
原因是: setText的参数应该是字符串,而不是整数。 用String.ValueOf( )可以实现类型转换。
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class z extends JApplet implements ActionListener
{ JLabel l1,l2,l3,l4;
JTextField t1,t2,t3,t4;
JButton toss;
public void init()
{ Container c=getContentPane();
c.setLayout(new FlowLayout());
l1=new JLabel("掷的结果");
c.add(l1);
t1=new JTextField(10);
c.add(t1);
l2=new JLabel("正的总次数");
c.add(l2);
t2=new JTextField(10);
t2.setEditable(false);
c.add(t2);
l3=new JLabel("反的总次数");
c.add(l3);
t3=new JTextField(10);
t3.setEditable(false);
c.add(t3);
l4=new JLabel("掷的总次数");
c.add(l4);
t4=new JTextField(10);
t4.setEditable(false);
c.add(t4);
toss=new JButton("开始掷");
toss.addActionListener(this);
c.add(toss);
}
public void actionPerformed(ActionEvent e)
{ play();
}
public String play()
{ int r,sum,z=0,f=0;
String k="";
sum=z+f;
r=0+(int)(Math.random()*2);
t1.setText(Integer.toString(r));
if(r==1)
{ ++z;
t2.setText(Integer.toString(z));
t4.setText(Integer.toString(sum));
k+="正";
}
else
{ ++f;
t3.setText(Integer.toString(f));
t4.setText(Integer.toString(sum));
k+="反";
}
return k;
}
}
但是还有一个问题,在运行的过程中正的次数,反的次数还有总的次数不能累积相加,他们总是在1和0之间徘徊,请问是什么问题?改怎么改?再次辛苦大家了,谢谢.