2014-11-06 77 views
-1

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

我是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()); 
    } 
} 
+1

这将是有益的,如果你提供了你尝试过的东西的细节 – vembutech 2014-11-06 15:40:47

+0

新程序员的一般建议:把你的问题分解成更小的问题,并专注于独立解决问题。在这种情况下,可以将其分解为两个问题:1.用'for'循环生成随机Unicode字符。 2.创建一个'JFrame'并在其中写入文本。然后,将这些问题分解成更小的问题。这将帮助你在小步骤中学习,并提出清晰,简洁的问题。祝你好运! – ZoogieZork 2014-11-06 17:36:35

+0

我搜索了几个小时,但找不到具体的答案,我正在寻找;我想创建一个程序,可以随机生成并显示unicode字符在jframe中使用循环。 – 2014-11-07 16:07:36

回答

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); 
    } 
}