2010-10-16 74 views
0

這是我目前有,但它並沒有顯示在兩個textareas結果.. 現在它顯示的是一個只有奇數的textarea。我不知道爲什麼偶數不會顯示。任何幫助表示讚賞謝謝!創建兩個框JFRAME幫助!

static TextFileInput inFile; 
    static String inFileName = "lab12.txt"; 
    static JFrame myFrame; 
    static Container cPane; 
    static TextArea even, odd; 

    public static void main(String[] args) { 
     initialize(); 
     readNumbersFromFile(inFileName); 

    } 
    public static void initialize() { 
     inFile = new TextFileInput(inFileName); 
     even = new TextArea(); 
     odd = new TextArea(); 
     myFrame=new JFrame(); 
     myFrame.setSize(400,400); 
     myFrame.setLocation(200, 200); 
     myFrame.setTitle("test"); 
     myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     myFrame.setVisible(true); 
    } 
    public static void readNumbersFromFile(String fileName){ 
     String[] num = new String[20]; 
     String line; 
      line = inFile.readLine(); 
      for(int i = 0; i < inFile.getLineCount(); i++){ 
       num[i] = line; 
       line = inFile.readLine(); 
       } 


     cPane = myFrame.getContentPane(); 

     cPane.add(even); 
     cPane.add(odd); 
     for(int i = 0; i < inFile.getLineCount(); i++){ 
      if(Integer.parseInt(num[i]) % 2 == 0) 
       even.append(num[i] + "\n"); 
      else 
       odd.append(num[i] + "\n"); 
     }//for 

     myFrame.setVisible(true); 


    }//readSSNsFromFile 
+0

其實它的工作......它只是我必須Streatch的JFRAME ....哇...我花了2個小時試圖弄清楚..這是因爲我不得不拉伸JFRAME .. ..現在有誰知道它爲什麼這樣做? – xiaolin 2010-10-17 00:19:18

回答

0

最好你建立面板,按行和列來劃分面板,然後在面板上添加這個面板。

+0

頂部被賦予..我想只填寫readNumberfromfile方法.. – xiaolin 2010-10-16 23:28:50

0

'cPane'默認會有一個BorderLayout。將組件添加到沒有約束的BorderLayout將導致組件被放入CENTER約束。 BorderLayout的任何區域只能容納一個組件。

更好的通用方法是使用適當的佈局實例化JPanel,將組件添加到JPanel,然後將該JPanel添加到內容窗格或將其設置爲內容窗格。