Java 随机数赢得';t出现在JTextField中

Java 随机数赢得';t出现在JTextField中,java,swing,random,awt,Java,Swing,Random,Awt,我有一个问题。我创建了一个程序,可以添加两个随机数。我正试图将一个Math.random()放在JTextField中,但它不会出现。顺便说一下,这是我的代码: 公共类RandomMathGame扩展JFrame{ public RandomMathGame(){ super("Random Math Game"); int random2; JButton lvl1 = new JButton("LEVEL 1"); JButton lvl2 = new JBu

我有一个问题。我创建了一个程序,可以添加两个随机数。我正试图将一个
Math.random()
放在
JTextField
中,但它不会出现。顺便说一下,这是我的代码:

公共类RandomMathGame扩展JFrame{

public RandomMathGame(){
    super("Random Math Game");
    int random2;
    JButton lvl1 = new JButton("LEVEL 1");
    JButton lvl2 = new JButton("LEVEL 2");
    JButton lvl3 = new JButton("LEVEL 3");
    JLabel line1 = new JLabel("Line 1: ");
    final JTextField jtf1 = new JTextField(10);
    JLabel line2 = new JLabel("Line 2: ");
    final JTextField jtf2 = new JTextField(10);
    JLabel result = new JLabel("Result: ");
    final JTextField jtf3 = new JTextField(10);
    JButton ans = new JButton("Answer");
    JLabel score = new JLabel("Score: ");
    JTextField jtf4 = new JTextField(3);
    JLabel itm  = new JLabel("Number of Items: ");
    JTextField items = new JTextField(3);
    FlowLayout flo = new FlowLayout();
    setLayout(flo);
    add(lvl1);
    add(lvl2);
    add(lvl3);
    add(line1);
    add(jtf1);
    add(line2);
    add(jtf2);
    add(result);
    add(jtf3);
    add(ans);
    add(score);
    add(jtf4);
    add(itm);
    add(items);
    setSize(140,400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

    lvl1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            int i, j = 10;
            int i1 = Integer.valueOf(jtf1.getText());
            int i2 = Integer.valueOf(jtf2.getText());
            int i3 = i1 + i2;
            final int random1 = (int)(Math.random() * 10 + 1);

            for (i = 0; i <= j + 1; i++){
                try{
                    jtf1.setText(String.valueOf(random1));
                    jtf2.setText(String.valueOf(random1));
                    jtf3.setText(String.valueOf(i3));
                }catch(Exception ex){
                    ex.printStackTrace();
                }
            }
        }
    });
}
公共游戏(){
超级(“随机数学游戏”);
int-2;
JButton lvl1=新JButton(“1级”);
JButton lvl2=新JButton(“2级”);
JButton lvl3=新JButton(“3级”);
JLabel line1=新的JLabel(“第1行:”);
最终JTextField jtf1=新JTextField(10);
JLabel line2=新的JLabel(“第2行:”);
最终JTextField jtf2=新JTextField(10);
JLabel结果=新的JLabel(“结果:”);
最终JTextField jtf3=新JTextField(10);
JButton ans=新JButton(“答案”);
JLabel分数=新的JLabel(“分数:”);
JTextField jtf4=新的JTextField(3);
JLabel itm=新的JLabel(“项目数:”);
JTextField项=新的JTextField(3);
FlowLayout flo=新的FlowLayout();
setLayout(flo);
添加(lvl1);
添加(lvl2);
添加(lvl3);
添加(第1行);
添加(jtf1);
添加(第2行);
添加(jtf2);
添加(结果);
添加(jtf3);
添加(ans);
加(分);
增加(jtf4);
添加(itm);
增加(项目);
设置大小(140400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(真);
lvl1.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
int i,j=10;
int i1=Integer.valueOf(jtf1.getText());
int i2=Integer.valueOf(jtf2.getText());
int i3=i1+i2;
最终int random1=(int)(Math.random()*10+1);

对于(i=0;i更新循环中的文本字段不会产生您可能想要的动画显示;只会看到最后一次更新。相反,使用a定期更新字段。可能会找到相关示例,并且。

我认为lvl1操作从未执行过。您能试着调试它吗?我尝试过。但没有出现随机数。
random1
是否正确生成?您是否看到在调试模式下?
random1
已生成,但没有出现整数。我认为您的逻辑存在一些问题。可能您在
integer处出现异常。value of
行或for循环未在设置值的位置运行。请再次检查您的逻辑在中,进行调试,您可以破解它。。