从GridBagLayout中的特定坐标获取组件
我正在使用带有GridBagLayout的JFrame,我希望能够从特定的x,y坐标获取组件(在我的情况下是JButton)。不应该这样做,因为我给了它x,y坐标GridBagConstraints开始?从GridBagLayout中的特定坐标获取组件
有没有简单的方法来做到这一点?
这里是实现你所寻找的一个小荒谬的例子。我希望这将是对你有帮助...
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Color;
import java.awt.Point;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Map;
import java.util.Set;
import java.util.LinkedHashMap;
class GridBagFrame extends JFrame implements ActionListener
{
GridBagLayout gb ;
GridBagConstraints gc;
Map <Point,JButton> map ;
final int SIZE = 20;
public void createAndShowGUI()
{
gb = new GridBagLayout();
gc = new GridBagConstraints();
gc.fill = GridBagConstraints.BOTH;
map = new LinkedHashMap<Point,JButton>();
Container container = getContentPane();
container.setLayout(gb);
int x =0 , y = -1 ;
JButton[] button = new JButton[SIZE];
for (int i = 0 ; i < SIZE ; i++)
{
button[i] = new JButton(String.valueOf(i + 1));
if (i % 4 == 0)
{
x = 0 ;
y = y +1;
}
gc.gridx = x++;
gc.gridy = y;
gb.setConstraints(button[i],gc);
container.add(button[i]);
map.put(new Point(x,y),button[i]);
button[i].setActionCommand(x+","+y);
button[i].addActionListener(this);
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setResizable(false);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent evt)
{
resetAll();
JButton source = (JButton)evt.getSource();
String command = source.getActionCommand();
String[] arr = command.split(",");
int x = Integer.parseInt(arr[0]);
int y = Integer.parseInt(arr[1]);
for (int iy = y - 1; iy <= y + 1; iy++)
{
for (int ix = x -1 ; ix <= x + 1 ; ix++)
{
JButton button = map.get(new Point(ix,iy));
if (button != null)
{
button.setForeground(Color.red);
}
}
}
source.setForeground(Color.blue);
}
private void resetAll()
{
Set<Point> pointSet = map.keySet();
for (Point point : pointSet)
{
map.get(point).setForeground(Color.black);
}
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
GridBagFrame frame = new GridBagFrame();
frame.createAndShowGUI();
}
});
}
}
如果您知道所需的JFrame上的x,y坐标,则可以使用GridBagLayout的location()方法找到什么是单元格(GridBagConstraints中的x,y)。知道GridBagLayout上的x,y,您需要遍历JFrame的组件,并找到GridBagConstraints中具有相同的x,y的元素,并将其与在getConstraints(Component comp)方法上给出的约束进行比较。
看看我希望能够得到的组件(对我来说一个JButton)从特定的x,y坐标。
- 将ActionListener添加到每个按钮。
- 在ActionListener代码中,您可以使用ActionEvent的
event.getSource()
方法获取您单击的按钮。 - 一旦知道源代码,您就可以使用组件的
getParent()
方法获得父容器。 - 然后,您可以使用容器的
getLayout()
方法获取布局管理器。 - 一旦你有了GridBagLayout,你可以使用
getContraints()
方法下注组件的约束条件。 - 然后你可以得到x/y坐标。
@Bamapag可能是另一个相当直接的工作方式,可以使用[putClientProperty](http://stackoverflow.com/a/13531549/714968),你可以将多个这种方法与任何额外的细节结合起来,在飞行中获得这个坐标, – mKorbel 2013-03-08 19:21:55
您应该注册'mouseClicked'事件给每个组件,而不是..这将帮助你找回所有的组件点击了的JFrame .. – 2013-03-08 18:44:17
我”什么m试图做的是采取一定的JButton并在它周围的方形模式上执行JButtons上的一组操作。 即,如果我的按钮在1,1我希望能够得到按钮0,0,1,0,2,0,0,1,2,1等等。 – Bamapag 2013-03-08 18:52:32