如何在JTextArea上打印

0 java

我正在学习用Java编程,我正在创建我的第一个GUI应用程序.它关于创建100个随机数.我首先在cmd上这样做:

public class RandomNumbers {

public static void main(String[] args){

        float n = 100;
        float m = 1513;
        float a = 19713;
        float x = 177963;
        float c = 1397;
        float r;
        float i;

        for(i=0;i<=n;i++){
            r = (a*x+c)%m;
            x = r;
            r = r/m;

            System.out.println(r);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

出于某种原因,当我尝试在文本区域打印100个随机数时,它只打印我一个.这是代码:

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

public class GUIRandomNumbers extends JFrame implements ActionListener{

        public JTextArea area;
    public JScrollPane scroll;
    public JButton button;

    public RandomNumbers(){
        setLayout(null);
        area = new JTextArea();
        area.setEditable(false);
        scroll = new JScrollPane(area);
        scroll.setBounds(10, 10, 400, 300);
        add(scroll);

        button = new JButton("Generate");
        button.setBounds(10, 650, 100, 25);
        add(button);
        button.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e){
        float n = 100;
        float m = 1513;
        float a = 19713;
        float x = 177963;
        float c = 1397;
        float r;
        float i;
        if(e.getSource()==button){
            for(i=0;i<=n;i++){
                r = (a*x+c)%m;
                x = r;
                r = r/m;

                area.setText(String.valueOf(r));
            }

        }
    }


    public static void main(String[] args) {


        RandomNumbers p1 = new RandomNumbers();
        p1.setBounds(0, 0, 500, 750);
        p1.setVisible(true);

    }   

}
Run Code Online (Sandbox Code Playgroud)

可能是什么问题呢?我真的很感谢你的帮助.

提前致谢.

buc*_*rt2 5

当你这样做

area.setText(String.valueOf(r));

它用新文本覆盖文本区域上的文本.

你应该使用

area.append(String);
Run Code Online (Sandbox Code Playgroud)

方法而不是.