如何获得GridLayout中元素的X和Y索引?

我正在学习一个java教程,看到在GridLayout中查找JButton的x / y索引的方法是遍历与布局相关联的buttonb的二维数组,并检查是否

b[i][j] == buttonReference

  @Override public void actionPerformed(ActionEvent ae) { JButton bx = (JButton) ae.getSource(); for (int i = 0; i < 5; i++) for (int j = 0; j < 5; j++) if (b[i][j] == bx) { bx.setBackground(Color.RED); } } 

有一个更简单的方法来获得button的X / Y索引?

就像是:

 JButton button = (JButton) ev.getSource(); int x = this.getContentPane().getComponentXIndex(button); int y = this.getContentPane().getComponentYIndex(button); 

this是一个GameWindow实例,当用户按下button时触发ActionEvent。

在这种情况下,它应该得到:x == 2,y == 1

@ GameWindow.java:

 package javaswingapplication; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.*; import javax.swing.*; public class GameWindow extends JFrame implements ActionListener { JButton b[][] = new JButton[5][5]; int v1[] = { 2, 5, 3, 7, 10 }; int v2[] = { 3, 5, 6, 9, 12 }; public GameWindow(String title) { super(title); setLayout(new GridLayout(5, 5)); setDefaultCloseOperation(EXIT_ON_CLOSE ); for (int i = 0; i < 5; i++) for (int j = 0; j < 5; j++) { b[i][j] = new JButton(); b[i][j].addActionListener(this); add(b[i][j]); } } @Override public void actionPerformed(ActionEvent ae) { ((JButton)ae.getSource()).setBackground(Color.red); } } 

@ JavaSwingApplication.java:

 package javaswingapplication; public class JavaSwingApplication { public static void main(String[] args) { GameWindow g = new GameWindow("Game"); g.setVisible(true); g.setSize(500, 500); } } 

这个例子展示了如何创build一个知道它在网格上的位置的网格button。 方法getGridButton()显示如何基于网格坐标有效地获取button引用,动作侦听器显示点击和find的button是相同的。

GridButtonPanel

 package gui; import java.awt.EventQueue; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; /** * @see http://stackoverflow.com/questions/7702697 */ public class GridButtonPanel { private static final int N = 5; private final List<JButton> list = new ArrayList<JButton>(); private JButton getGridButton(int r, int c) { int index = r * N + c; return list.get(index); } private JButton createGridButton(final int row, final int col) { final JButton b = new JButton("r" + row + ",c" + col); b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JButton gb = GridButtonPanel.this.getGridButton(row, col); System.out.println("r" + row + ",c" + col + " " + (b == gb) + " " + (b.equals(gb))); } }); return b; } private JPanel createGridPanel() { JPanel p = new JPanel(new GridLayout(N, N)); for (int i = 0; i < N * N; i++) { int row = i / N; int col = i % N; JButton gb = createGridButton(row, col); list.add(gb); p.add(gb); } return p; } private void display() { JFrame f = new JFrame("GridButton"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(createGridPanel()); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new GridButtonPanel().display(); } }); } } 

你已经保存了所有JButton的数组; 你可以searchae.getSource() ,你有位置。

 for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if( b[i][j] == ae.getSource() ) { // position i,j } } } 

从JButtons

  • 的JButton#的setName(string);

  • 的JButton#setActionCommand(string);

  • JButton的#的setAction(行动);

从/到容器

SwingUtilities类#转换…

SwingUtilities类#getDeepestComponentAt

您可以使用setName()在JButton中存储它的位置(例如button.setName(i +“”+ j);); 您可以通过分割从空间中的button.getName()获得的string来访问它。 这不是一个特别有效的方法,但它听起来有点像你现在(或现在)正在寻找的东西。

这个解决schemeselect像他们之间的所有对象
第一个写入方法,获取文本或Jbuuton所需的一切或jable或….第二个更改下的代码

 public class Event_mouse implements MouseListener { @Override public void mouseReleased(MouseEvent e) { try { Everything source = (Everything) e.getSource(); if(Everything.gettext==gol){ } } catch (Exception ee) { JOptionPane.showMessageDialog(null, ee.getMessage()); } }