2014-11-06 37 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); 
    } 
}