GUI 文本框滚动条帮助
1. JTextArea 消息 = new JTextArea(5, 30); 2. JScrollPane 滚动文本框 = new JScrollPane(消息, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED ); 3. 集装箱窗户; 4. ... 5. messages.setPreferredSize(new Dimension(500,100)); 6. messages.setLineWrap(true); 7. messages.add(scrollTextBox, BorderLayout.CENTER); 8. 窗口.add(消息); 9.
10. 我正在尝试在 GUI 框中编写一个文本框,该文本框从服务器返回消息,它需要一个滚动条,因为消息不断发送,但目前只有前几行可见,其余的不可见,因为我无法让滚动条发挥作用。 11. 我之前的尝试要么导致了这个错误: 12. java.lang.IllegalArgumentException:将容器的父级添加到自身 13. 在 java.awt.Container.checkAddToSelf(来源未知) 14. 在 java.awt.Container.addImpl(来源未知) 15. 在 java.awt.Container.add(来源未知) 16. 在 LODGUI.drawAndShow(LODGUI.java:91) 17. 在 LODClient.run(LODClient.java:94) 18. 在 LODClient.main(LODClient.java:157) 19.
20. 或者没有变化并且没有滚动条。
1.
JTextArea messages = new JTextArea(5, 30);
2.
JScrollPane scrollTextBox = new JScrollPane(messages, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED );
3.
Container window;
4.
...
5.
messages.setPreferredSize(new Dimension(500,100));
6.
messages.setLineWrap(true);
7.
messages.add(scrollTextBox, BorderLayout.CENTER);
8.
window.add(messages);
9.
10.
I'm trying to write a text box within a GUI box that returns messages from a server, it needs a scroll bar because messages are continually being sent but at the moment only the first few lines are visible and the rest aren't because i can't get the scroll to work.
11.
my attempts before have either resulted in this error:
12.
java.lang.IllegalArgumentException: adding container's parent to itself
13.
at java.awt.Container.checkAddToSelf(Unknown Source)
14.
at java.awt.Container.addImpl(Unknown Source)
15.
at java.awt.Container.add(Unknown Source)
16.
at LODGUI.drawAndShow(LODGUI.java:91)
17.
at LODClient.run(LODClient.java:94)
18.
at LODClient.main(LODClient.java:157)
19.
20.
or no change and no scroll bar.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误消息中对此问题进行了说明。您将消息添加到scrollTextBox,然后将scrollTextBox添加到消息。
请尝试格式化您的问题......
The problem is explained in the error message. You are adding messages to scrollTextBox, then adding scrollTextBox to messages.
And please please try to format you question...
删除第 7 行。
将第 8 行更改为:(
执行此操作的方法是将组件“消息”插入到 JScrollPane 中,如第 2 行中所做的那样。然后,不要将“消息”插入到容器中,而是插入 scoll窗格。)
请下次格式化您的问题。
Delete line 7.
Change line 8 to:
(The way to do it is to insert your component 'messages' into a JScrollPane which you do in line 2. Then, don't insert 'messages' into the container, but insert the scoll pane.)
And please, next time format your question.