Java秋千:将滚动条添加到Textarea

发布于 2025-02-04 12:41:01 字数 396 浏览 4 评论 0原文

我有一个文本区域,可以更新按钮按下(此更新正常)。当它更新时,在需要滚动条的文本区域中会出现一个长列表。但是,当我在下面添加最后两行时,不仅不会出现滚动条,而且文本也不会出现。我该如何解决? (我是Java Swing的新手,所以答案越简单越好)

textArea1 = new JTextArea("");
textArea1.setBounds(450, 40, 400, 700);
frame.getContentPane().add(textArea1);
JScrollPane scroll = new JScrollPane(textArea1);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

I have a text area that updates a button press (this updating works fine). When it updates, a long list appears in the text area that requires a scroll bar. However, when I add the last two lines below, not only does the scroll bar not appear, text doesn't appear as well. How do I fix this? (I'm new to java swing so the simpler the answer the better)

textArea1 = new JTextArea("");
textArea1.setBounds(450, 40, 400, 700);
frame.getContentPane().add(textArea1);
JScrollPane scroll = new JScrollPane(textArea1);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

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

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

发布评论

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

评论(1

随梦而飞# 2025-02-11 12:41:01

您将jscrollpane添加到jframe

textArea1 = new JTextArea("");
textArea1.setBounds(450, 40, 400, 700);
JScrollPane scroll = new JScrollPane(textArea1);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(scroll);

Oracle有一个有用的教程,创建带有swing 的GUI。使用Netbeans IDE部分跳过学习秋千。

您应该使用摇摆布局管理器,而不是setBounds

You add the JScrollPane to the JFrame.

textArea1 = new JTextArea("");
textArea1.setBounds(450, 40, 400, 700);
JScrollPane scroll = new JScrollPane(textArea1);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(scroll);

Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section.

You should be using Swing layout managers, rather than setBounds.

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