通过无限FOR循环在java中打印随机Unicode字符

问题描述 投票:-1回答:1

我想通过使用无限的for loop在java中的JFRAME中打印随机Unicode字符。

我是java编程的新手,我希望答案不是太复杂。

这是迄今为止的代码 -

import java.awt.*;
import javax.swing.*;
import java.util.*;

public class Matrix extends JFrame{
    private static final int NUMBER_OF_REPS = 0;

    public static void main(String[] args) {
        int a;

        Random rand=new Random();

        for(a=1;a>0;)
        {
            JLabel lbl=new JLabel();
            lbl.setForeground(Color.GREEN);
            lbl.setFont(new Font("MS Arial Unicode",Font.BOLD,12));
            lbl.setText("\\u30"+Integer.toHexString(rand.nextInt(96) + 160));
        }

        JFrame frame=new JFrame();
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.getContentPane().setBackground(Color.BLACK);

        //error! lbl cannot be resolved to a variable
        frame.getContentPane().add(lbl);
        frame.getContentPane().setLayout(new FlowLayout());
    }  
}
java for-loop unicode infinite
1个回答
0
投票

我使用了一个while循环,它们对于无限数字更容易。至于JFRAME,你会想看看另一个答案,因为我对javax.swing并不是很好。*截至目前它只是将它放入控制台

public Random random = new Random();

public static void main(String[] args) {
    while(true) {
        int i = random.nextInt(127);
        System.out.print((char)i);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.