无法将字符串附加到 jtextarea

发布于 2024-10-30 04:36:48 字数 1092 浏览 1 评论 0原文

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() 方法显示文本。但是,该字符串不显示。我缺少什么?

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();
            }
        });
    }
}

I am trying to display text using the setText() method on JTextarea. However, the string does not display. What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑寡妇 2024-11-06 04:36:48

它就在那里,只需使文本区域小得多即可。类似于:

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);

It is there, just make the textarea much smaller. Something like:

JTextArea jtextarea = new JTextArea(20,20);

With it's current size you don't see the text. Part of the reason you can't scroll to the text is the incorrect way you are adding the textarea and the scroll bar. A better way to do that would be:

    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);
苏璃陌 2024-11-06 04:36:48

替换:

JTextArea jtextarea = new JTextArea();

而不是:

JTextArea jtextarea = new JTextArea(200, 200);

构造函数的文档说参数不是以像素为单位:

构造一个新的空 TextArea
指定的行数和
列。创建默认模型,
并且初始字符串为空。

Replace:

JTextArea jtextarea = new JTextArea();

instead of:

JTextArea jtextarea = new JTextArea(200, 200);

Documentation for your constructor said that arguments arent in pixels:

Constructs a new empty TextArea with
the specified number of rows and
columns. A default model is created,
and the initial string is null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文