2015-05-14 27 views
0

第一次在Java中JTextArea的文字無形的,沒有滾動

http://i58.tinypic.com/s13gh0.png

編寫GUI這是到目前爲止,當我點擊「座標異常」的字符串數組列表在JTextArea中顯示,但只有通過突出顯示文本才能看到文本。

我也嘗試添加一個滾動條到JTextArea,但沒有運氣。

JTextArea textArea = new JTextArea(); 
textArea.setBounds(10, 79, 172, 339); 
frame.getContentPane().add(textArea); 

JButton btnNewButton_1 = new JButton("Coordinate Anomalies"); 

btnNewButton_1.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     ArrayList<String> anomalies = vessels.coordinateAnomaly(); 
     JScrollPane jp = new JScrollPane(); 
     jp.setViewportView(textArea); 

     for (String a : anomalies) { 
      textArea.append(a + "\n"); 
     } 

     textArea.setBounds(10, 79, 172, 339); 
     frame.getContentPane().add(textArea); 
    } 
}); 

btnNewButton_1.setBounds(10, 45, 172, 23); 
frame.getContentPane().add(btnNewButton_1); 

這是我的代碼(抱歉JANK),可以強調也不過分,我是新來的GUI和任何的建議是真正的讚賞。

+0

[佈局管理器](https://docs.oracle.com/javase/時tutorial/uiswing/layout/visual.html)是你的朋友 – Reimeus

+0

Java GUI必須在不同的語言環境中使用不同的PLAF來處理不同的操作系統,屏幕大小,屏幕分辨率等。因此,它們不利於像素的完美佈局。請使用佈局管理器或[它們的組合](http://stackoverflow.com/a/5630271/418556)以及[white space]的佈局填充和邊框(http://stackoverflow.com/a/17874718/ 418556)。 –

回答

0

感謝@Reimeus的幫助,這是我最終實現的解決方案,它的工作原理。

JTextArea textArea = new JTextArea(); 
JScrollPane jp = new JScrollPane(textArea); 
jp.setBounds(10, 79, 172, 339); 
frame.getContentPane().add(jp); 

JButton btnNewButton_1 = new JButton("Coordinate Anomalies"); 
btnNewButton_1.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent arg0) { 
     textArea.setText(""); 
     ArrayList<String> anomalies = vessels.coordinateAnomaly(); 

     for(String a : anomalies){ 
       textArea.append(a + "\n"); 
      } 
    } 
}); 
btnNewButton_1.setBounds(10, 45, 172, 23); 
frame.getContentPane().add(btnNewButton_1); 
4

組件只能有一個父代。在應用程序啓動,而不是JTextArea

frame.add(new JScrollPane(textArea)); 
+0

我試過了,JtextArea仍然存在,但字符串不再被打印,也沒有滾動。 http://oi57.tinypic.com/np2cg9.jpg –

+0

不要在你的'ActionListener'中添加一個新的滾動條,只需更新一個'textArea' – Reimeus

+0

http://oi57.tinypic.com/2ai0r47.jpg 好的,所以沒有滾動,並且JtextArea在我單擊按鈕之前是不可見的,但是當我單擊按鈕時文本不再是不可見的。只需要確定如何在點擊按鈕之前顯示空的Jtextarea並進行滾動。 –

0

當在已經顯示的圖形用戶界面添加組件添加一個滾動面板,你需要調用RE /中/驗證把一切擺出來,並重繪()得到它顯示器

btnNewButton_1.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     ArrayList<String> anomalies = vessels.coordinateAnomaly(); 
     JScrollPane jp = new JScrollPane(); 
     jp.setViewportView(textArea); 

     for (String a : anomalies) { 
      textArea.append(a + "\n"); 
     } 

     textArea.setBounds(10, 79, 172, 339); 
     frame.getContentPane().add(jp); // changed 
     frame.getContentPane().invalidate(); // added 
     frame.getContentPane().validate(); // added 
     frame.getContentPane().repaint(); // added 
    } 
}); 
+0

所以我嘗試了這一點,但textArea現在消失,當我點擊座標異常 http://i57.tinypic.com/fvvh2x.png –

1

簡單的解決方案:

加入JTextAreaJFrame只是添加滾動窗格中像這樣

JTextArea textarea = new JTextArea(); 
add(new JScrollPane(textarea));