public class Main {
private static void createAndShowGUI() {
JFrame frame1 = new JFrame("FINAL YEAR PROJECT VER 1.0");
frame1.setSize(500,500);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout experimentLayout = new FlowLayout();
experimentLayout.setAlignment(FlowLayout.CENTER);
frame1.setLayout(experimentLayout);
JTextArea jtextarea = new JTextArea(200,200);
JScrollPane scrollingArea = new JScrollPane(jtextarea);
frame1.getContentPane().add(jtextarea);
frame1.getContentPane().add(scrollingArea, FlowLayout.CENTER);
jtextarea.setText("Welcome to Document-Query Similarity System Based On Weblogs");
frame1.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
我尝试使用 JTextarea
上的 setText()
方法显示文本。但是,该字符串不显示。我缺少什么?
最佳答案
它就在那里,只需将文本区域缩小得多即可。像这样的东西:
JTextArea jtextarea = new JTextArea(20,20);
在当前大小下,您看不到文本。无法滚动到文本的部分原因是添加文本区域和滚动条的方式不正确。更好的方法是:
frame1.setLayout(new BorderLayout());
...
//delete the addition of the textarea to the frame, you already put it in the scroll pane.
frame1.getContentPane().add(scrollingArea, BorderLayout.CENTER);
关于java - 无法将字符串附加到 jtextarea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5556304/