Pho*_*nix 5 java swing jscrollpane jtextarea
我正在创建一个显示消息聊天日志的 Java Swing 应用程序。该帧仅包含 a ,JScrollPane其中有一个VerticalLayout,并且其中有无限数量的JTextAreas,每条消息一个。sJTextArea是不可编辑的,我使用它们而不是 a 的唯一原因JLabel是我需要换行文本。
作为测试,我创建了框架并添加了两条消息。
\n
这按预期工作,组件形成连续的文本页面并环绕框架。
\n然后我增加框架的大小。
\n
这仍然按预期工作,JScrollPane和JTextAreas 已经扩展并且文本已经重新包装。
然后我将窗口缩小到较小的尺寸。
\n
这没有按预期工作。保持JScrollPane其最大尺寸并且单词不会换行。我怎样才能做到当框架宽度方向缩小时,JScrollPane和JTextAreas 也会缩小?
我已经尝试过的事情:
\nJScrollPanewithJScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVERJTextArea一个BoxLayout或BorderLayoutJTextArea在 a 中JPanel并给面板一个BoxLayoutorBorderLayoutJScrollPane调整大小事件setSize(Dimension d)of JScrollPanewith getSize()ofJFrame编辑:一个最小的可验证示例,产生与上述相同的效果
\nimport javax.swing.JFrame;\nimport javax.swing.JPanel;\nimport javax.swing.JScrollPane;\nimport javax.swing.JTextArea;\n\nimport org.jfree.ui.tabbedui.VerticalLayout;\n\npublic class MessagingFrame extends JFrame {\n\n private JPanel textPanel;\n\n public MessagingFrame() {\n // Create display area\n textPanel = new JPanel();\n textPanel.setLayout(new VerticalLayout());\n // Wrap text panel in scroll pane\n JScrollPane scrollPane = new JScrollPane(textPanel);\n // Add scroll pane\n add(scrollPane);\n // Size center and display\n setSize(500, 500);\n setLocationRelativeTo(null);\n setVisible(true);\n }\n\n public void addMessage(String message) {\n JTextArea textArea = new JTextArea(message);\n textArea.setEditable(false);\n textArea.setLineWrap(true);\n textArea.setWrapStyleWord(true);\n textPanel.add(textArea);\n textPanel.updateUI();\n }\n\n public static void main(String[] args) {\n MessagingFrame frame = new MessagingFrame();\n frame.addMessage("According to all known laws of aviation, there is no way a bee should be able to fly. Its wings are too small to get its fat little body off the ground. The bee, of course, flies anyway because bees don\xe2\x80\x99t care what humans think is impossible.");\n frame.addMessage("We begin on Christmas Eve with me, Mark, and my roommate, Roger. We live in an industrial loft on the corner of 11th street and Avenue B, the top floor of what was once a music publishing factory. Old rock \'n\' roll posters hang on the walls. They have Roger\'s picture advertising gigs at CBGB\'s and the Pyramid Club. We have an illegal wood burning stove; its exhaust pipe crawls up to a skylight. All of our electrical appliances are plugged into one thick extension cord which snakes its way out a window. Outside, a small tent city has sprung up in the lot next to our building. Inside, we are freezing because we have no heat.");\n }\n\n}\nRun Code Online (Sandbox Code Playgroud)\n谢谢你!
\n您需要Scrollable在添加到滚动面板的面板上实现接口,以便该getScrollableTracksViewportWidth()方法返回true。
这将强制视口中的组件始终与视口大小相同,而不是添加到视口的组件的宽度。
当您实现接口时,您需要实现该接口的所有方法。
或者一个简单的解决方案是使用可滚动面板,它为您实现界面,您只需设置所需的属性即可。
所以基本代码是:
ScrollablePanel panel = new ScrollablePanel( new VerticalLayout());
panel.setScrollableWidth( ScrollablePanel.ScrollableSizeHint.FIT );
panel.add(...);
JScrollPane scrollPane = new JScrollPane( panel );
Run Code Online (Sandbox Code Playgroud)
将每个 JTextArea 包装在 JPanel 中,并为面板提供 BoxLayout 或 BorderLayout
您应该能够将文本区域直接添加到面板中。看起来 VerticalLayout 使每个组件尽可能宽。
textPanel.updateUI();
不要使用用于 LAF 更改的 updateUI()。当您从可见 GUI 添加/删除组件时,基本代码是:
panel.add();
panel.revalidate();
panel.repaint();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
986 次 |
| 最近记录: |