Java 如何使用Swing定时器在运行时添加Jcomponent

Java 如何使用Swing定时器在运行时添加Jcomponent,java,swing,timer,jcomponent,Java,Swing,Timer,Jcomponent,我有问题,使添加随机文本(随机大小,字体,x,y)到JFrame每秒钟这是我的代码 第一个是JComponent,它期望随机生成的字符串出现 public class RandomTextGenerator extends JComponent{ private String str; private Random r; private Color c; private Font f; private int x; private int y;

我有问题,使添加随机文本(随机大小,字体,x,y)到JFrame每秒钟这是我的代码

第一个是JComponent,它期望随机生成的字符串出现

public class RandomTextGenerator extends JComponent{
    private String str;
    private Random r;
    private Color c;
    private Font f;
    private int x; 
    private int y;
    Graphics g;
    public RandomTextGenerator(String s)
    {

        r = new Random();
        c = new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255));
        f = new Font("random",Font.BOLD,r.nextInt(100));
        x = r.nextInt(100);
        y = r.nextInt(100)+100;
        //setPreferredSize(new Dimension(500,500));
        str = s;
    }
    public void paintComponent(Graphics g)
    {
        g.setColor(c);
        g.setFont(f);
        System.out.println(str);
        g.drawString(str, x, y);
    }
}
另一个是JFrame

public class TextFrame extends JFrame{
    public TextFrame() {
        this.setSize(500, 500);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Random Text Generator");
        this.getContentPane().setBackground(Color.BLACK);
        this.setLayout(new GridLayout(1,1));
        this.add(new RandomTextGenerator("AAAAAAAA"));
        this.add(new RandomTextGenerator("BBBBBBBBBB"));
        this.setVisible(true);
    }
    public void addRandomText(RandomTextGenerator r)
    {
        this.add(r);
        this.add(new RandomTextGenerator("CCC"));
        System.out.println("Method called");
        this.repaint();
    }
}
上述代码

this.add(new RandomTextGenerator("AAAAAAAA"));
this.add(new RandomTextGenerator("BBBBBBBBBB"));
那个些功能很好,可以在屏幕上显示,但问题是文本的方法

this.add(r);
this.add(new RandomTextGenerator("CCC"));
那些根本不起作用

我的主要方法是在这里

public class tfmain {
    public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            System.out.println("Enter Your Message");
            String str = s.nextLine();
            TextFrame tf = new TextFrame();
            Timer t = new Timer(1000,new ActionListener(){

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    tf.addRandomText(new RandomTextGenerator(str));
                }

            });
            t.start();
        }
}
使用这个名为is-execute的代码方法,这意味着计时器实际上可以工作

但是为什么添加Jcomponent不起作用呢??在AddRandomtext中添加Jcomponent永远不会起作用(也不会调用paintComponent)


请给我一些建议。对不起,我的英语不好

f=新字体(“random”,Font.BOLD,r.nextInt(100))
您认为
“random”
的值会起到什么作用?一般提示:1)为了更快地获得更好的帮助,可以发布一个(最简单、完整、可验证的示例)或(简短、独立、正确的示例)。2) 请学习常见的Java命名法(命名约定-例如
EachWordUpperCaseClass
firstWordLowerCaseMethod()
firstWordLowerCaseAttribute
,除非它是
大写常量
),并一致使用它。3)
Graphics不要尝试缓存图形。。。。对象,它将以失败、眼泪和指责而告终。而是使用提供的图形对象,并在要求时对其进行绘制。4) 
public void paintComponent(Graphics g){..
应该是
@Override public void paintComponent(Graphics g){super.paintComponent(g);…
获取a)编译时检查的值和b)要绘制的组件的背景。顺便说一句-我强烈怀疑
RandomTextGenerator
不应该
扩展JComponent
。如果想法是在GUI中随机放置几段文本,我会有一个自定义绘制的组件(或
缓冲图像
)和
随机文本
对象的
列表
(可以在请求时将自身绘制到
图形
对象)。一旦容器的图形对象位于
画图组件(…)
或从图像中,迭代列表并绘制每个
随机文本
。非常感谢您的评论对我帮助很大。我刚上大学二年级,这是我第一次学习java。所以我不知道如何处理GUI对象。。