我正在尝试在JtextArea
中添加多行文字,但是没有滚动,所以我无法移动以查看默认JtextArea
区域下方的任何数据,这里是代码和textArea_1
是JtextArea
:
String abc="";
for(int i=0; i<=100; i++){
abc = abc + data[i][0]+"\n";
}
textArea_1.setText(abc);
答案 0 :(得分:2)
您需要考虑添加JScrollPane
。
链接:http://docs.oracle.com/javase/7/docs/api/javax/swing/JScrollPane.html
import javax.swing.JScrollPane;
JScrollPane scrollPane = new JScrollPane(TEXTAREAHERE);
答案 1 :(得分:1)
滚动由JScrollPane
处理。
查看How to use scroll panes并自行参考,JScrollPane
以及How to use Text Areas
更新了示例
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestScrollPane04 {
public static void main(String[] args) {
new TestScrollPane04();
}
public TestScrollPane04() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JTextArea textArea = new JTextArea(10, 20);
String abc = "";
for (int i = 0; i <= 100; i++) {
abc = abc + "This is some additional text " + i + "\n";
}
textArea.setText(abc);
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(textArea));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
答案 2 :(得分:0)
您应该添加JScrollPane
。如
JscrollPane myScroll=new JScrollPane(textArea_1);
如果您的文字不止于此,则可以通过滚动查看其他文字。默认情况下,当您set
或append
发送到JTextArea
时,它会滚动到结尾(您可以看到,如果文字很大,则会显示滚动条)。所以要回滚到第一行,请尝试
textArea_1.setCaretPosition(0);
请参阅Java doc