Java Swing 选择面板 [英] Java Swing selecting a panel
问题描述
如果我在另一个面板中有一个由 9 个面板组成的网格,并且我希望能够单击一个子面板并在单击时将该面板的内容发送到选定面板"区域,我将如何处理?(想象一下电子游戏的角色选择页面.当玩家选择他们的角色时,它会在已选择"窗格中显示角色的展开视图)
If I have a grid of 9 panels within another panel and I want to be able to click a subpanel and on click, send the content of that panel to a 'selected panel' area, how would I go about this? (Imagine a character select page for a videogame. When a player selects their character, it shows an expanded view of the character in a 'selected' pane)
我正在考虑每个子面板的 mouseListeners 并检索单击的组件,但我不明白如何将该单击的组件复制到选定"区域.
I'm thinking of mouseListeners for each subpanel and retrieving the clicked component but I don't understand how I can copy that clicked component to a 'selected' area.
推荐答案
将您的项目添加到 JList
或单列 JTable
.将 ListSelectionListener
添加到您的选择的组件.指定 ListSelectionModel.SINGLE_SELECTION
.在您的 valueChanged()
处理程序中,ListSelectionEvent
将告诉您选择了什么值.使用该信息填写相邻面板的字段.一个完整的例子,如下图所示,见此处.
Add your items to a JList
or single-colum JTable
. Add a ListSelectionListener
to your chosen component. Specify ListSelectionModel.SINGLE_SELECTION
. In your valueChanged()
handler, the ListSelectionEvent
will tell you what value was selected. Use that information to fill in the fields of an adjacent panel. A complete example, illustrated below, is seen here.
这篇关于Java Swing 选择面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!