java重绘组件_java – 在重绘方法时在GUI中添加组件

博客强调在Java GUI中,paint或paintComponent方法仅用于绘图,程序逻辑不应放入,应使用事件侦听器响应用户事件。还提到使用JList保存可选信息列表,用ListSelectionListener响应,避免用KeyListeners而倾向Key Bindings,同时遇到组件重绘异常问题。

切勿在GUI中添加组件或删除paint或paintComponent方法中的组件.只是不要这样做.永远.期.

这些方法仅用于绘图,并且需要尽可能快,否则您的程序将显示无响应.不仅如此,您无法完全控制何时或甚至是否会调用这些方法,因此程序逻辑和结构不应该进入这些方法.

而是使用事件侦听器(如ActionListeners,ListSelectionListeners或使用键绑定)对用户事件做出反应.

编辑

关于

Got it, let me give you a more detailed breakdown. A client is navigating my program by looking at a list of characters in a game on a west panel. They can select a row from the list which will show char details on the east panel. The details are an image and description. Recently, I added relevant documents for that particular char, which will show on the bottom of the east panel. I created key listener’s, so the client can quickly view the document by pressing a num key, but I also want to give them the ability to click on the button to launch a pdf view and see the contents of the document.

我将使用JList来保存左侧的可选信息列表,并使用ListSelectionListener对其作出反应.在监听器中,我将更改相关的显示信息.我也避免使用带有Swing的KeyListeners,而是倾向于Key Bindings,因为它们更灵活,对焦点不那么刻板.

关于

Since every char has different related docs and different number of docs, I repainted the buttons every time, to reflect the amount of related docs and the appropriate titles for the docs. This is where the repaint is acting strange. You gave me a good explanation of what’s going wrong, but I don’t know how to give the client access to the docs now, aside from painting a description of the doc along with the hot key needed to launch it. Does that make sense?

我不确定你在这里做什么或者你想做什么.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值