使用Swing动态重新排列布局 [英] Dynamically rearrange layout with Swing

查看:153
本文介绍了使用Swing动态重新排列布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用具有与Twitter类似功能的Swing构建桌面应用程序。我有一个提要页面,其中显示推文。

I am building a desktop app with Swing with similar functionality to Twitter. I have a "feed" page where "tweets" are displayed.

我在JPanel中有推文,并希望通过放置新动态来动态显示新推文JPanel顶部的推文,并将旧的推文降低。我试图通过使用 jpanel.add(tweet,cell 0 0,wrap)来使用MigLayout执行此操作但是这不能按预期工作并且为了获得它显示我想要的布局我必须调用 jpanel.revalidate()

I have the "tweets" in a JPanel and want to dynamically display new ones coming in by placing new "tweets" at the top of the JPanel and moving older ones down. I was trying to do this with MigLayout by using jpanel.add(tweet, "cell 0 0, wrap") however this didn't work as intended and in order to get it to display with the layout I wanted I had to call jpanel.revalidate().

这不是理想的因为每秒都会有很多推文进入,重新绘制面板可能会很慢。无论如何我可以在面板顶部添加新的推文而无需重新绘制?

This is not ideal as there could be many tweets coming in every second and redrawing the panel can be quite slow. Is there anyway I can add new "tweets" to the top of the panel without redrawing?

推荐答案

虽然可以进行动态布局,如此处此处,我使用单列 JTable ,其中 TableModel 反映了您想要的下推式堆栈效果,可能是内部队列< Tweet> JTable 渲染效率很高,组件在 JScrollPane 中运行良好。

Although dynamic layouts are possible, as shown here and here, I'd use a one-column JTable with a TableModel that reflects the pushdown stack effect you want, perhaps one having an internal Queue<Tweet>. JTable rendering is efficient, and the component works well in a JScrollPane.

这篇关于使用Swing动态重新排列布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆