Java Swing Gridlayout:访问特定坐标 [英] Java Swing Gridlayout: Accessing Specific Coordinate

查看:215
本文介绍了Java Swing Gridlayout:访问特定坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建Java swing GUI,并且已格式化JPanel以使用GridLayout.我需要访问网格的特定框"(即特定坐标),但是我看不到这样做的方法.

I'm creating a Java swing GUI and I have formatted a JPanel to use a GridLayout. I need to access a specific "box" (i.e. specific coordinate) of the grid, but I cannot see a way to do so.

我该怎么做?

推荐答案

您不应该依赖GUI代码(视图)为您提供有关程序数据(模型)的信息.最好的解决方案是从一开始就知道"哪个组件-也许您应该有一个数据结构(2D数组?)来保存这些组件,并在将任何内容添加到网格时进行更新.

You shouldn't depend on GUI code (the View) to give you information about program data (the model). The best solution would be to "know" which component is where from the start--maybe you should have a data structure (2D array?) that holds the components and is updated whenever something's added to the grid.

但是,如果您想要快速且非常肮脏的修复程序,则可以使用通过您的GridLayout对象.不过,这不推荐.我只是出于完整性考虑而将其包括在内(并且因为这是对您的问题的更直截了当的回答).

If you want a quick and very-dirty fix, though, you could start playing games with JPanel.getComponentAt(). This requires pixel coordinates, though, so you'd need to do some reverse-engineering to figure out how much space a given grid square takes up. The space between grid squares is given by your GridLayout object. This is not recommended whatsoever though. I'm just including it in the interest of completeness (and since it's a more literal response to your question).

这篇关于Java Swing Gridlayout:访问特定坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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