第一次在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和任何的建議是真正的讚賞。
[佈局管理器](https://docs.oracle.com/javase/時tutorial/uiswing/layout/visual.html)是你的朋友 – Reimeus
Java GUI必須在不同的語言環境中使用不同的PLAF來處理不同的操作系統,屏幕大小,屏幕分辨率等。因此,它們不利於像素的完美佈局。請使用佈局管理器或[它們的組合](http://stackoverflow.com/a/5630271/418556)以及[white space]的佈局填充和邊框(http://stackoverflow.com/a/17874718/ 418556)。 –