[求助]菜鸟的问题

Mars酱 2004-08-28 12:47:35
代码如下:
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("jieguo");
l2=new JLabel("z's zong cishu");
l3=new JLabel("f's zong cishu");
l4=new JLabel("zong cishu");
t1=new JTextField(10);
t2=new JTextField(10);
t3=new JTextField(10);
t4=new JTextField(10);
toss=new JButton("play");
toss.addActionListener(this);
c.add(l1);c.add(l2);c.add(l3);c.add(l4);c.add(toss);
c.add(t1);c.add(t2);c.add(t3);c.add(t4);
}
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);
if(r==1)
{ ++z;
t2.setText(z);
t4.setText(sum);
k+="true";
}
else
{ ++f;
t3.setText(f);
t4.setText(sum);
k+="false";
}

return k;
}
}
编译时错误的提示如下:
z.java:34: setText(java.lang.String) in javax.swing.text.JTextComponent cannot b
e applied to (int)
t2.setText(z);
^
z.java:35: setText(java.lang.String) in javax.swing.text.JTextComponent cannot b
e applied to (int)
t4.setText(sum);
^
z.java:40: setText(java.lang.String) in javax.swing.text.JTextComponent cannot b
e applied to (int)
t3.setText(f);
^
z.java:41: setText(java.lang.String) in javax.swing.text.JTextComponent cannot b
e applied to (int)
t4.setText(sum);
^
4 errors
请问高手们,怎么修改啊?
辛苦大家了,谢谢了哦~
...全文
123 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mars酱 2004-09-09
  • 打赏
  • 举报
回复
up
Mars酱 2004-08-30
  • 打赏
  • 举报
回复
程序已经改了,如下:
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之间徘徊,请问是什么问题?改怎么改?再次辛苦大家了,谢谢.
yxlleslie 2004-08-30
  • 打赏
  • 举报
回复
只要把 t2.setText(z);改成 t2.setText(String.ValueOf(z));就可以了
原因是: setText的参数应该是字符串,而不是整数。 用String.ValueOf( )可以实现类型转换。
Mars酱 2004-08-30
  • 打赏
  • 举报
回复
顶~
Mars酱 2004-08-28
  • 打赏
  • 举报
回复
改是改了
编译也通过了
但是还是有错误
sum和z还有f结果不对,请问再怎么改啊?我今天想了很久了,没想出来,没办法,脑子笨.
qqqkingppp 2004-08-28
  • 打赏
  • 举报
回复
JTextField控件的SetText()里的参数必须是
String 类型的 而你传进去的是int型的 所以
你把它改成SetText(Integer.toString(int))
就OK了

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧