Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取gridlayout上按钮的位置_Java_Arrays_Swing_Jbutton_Mouselistener - Fatal编程技术网

Java 获取gridlayout上按钮的位置

Java 获取gridlayout上按钮的位置,java,arrays,swing,jbutton,mouselistener,Java,Arrays,Swing,Jbutton,Mouselistener,如何使用gridlayout获取单击按钮的位置(我指的是行和列) public void init(final Container pane) { JPanel controls = new JPanel(); int size = (int) Math.sqrt(puzzle.getSize() + 1); controls.setLayout(new GridLayout(size, size)); for (int i = 0; i < puzzle.

如何使用gridlayout获取单击按钮的位置(我指的是行和列)

public void init(final Container pane) {
    JPanel controls = new JPanel();
    int size = (int) Math.sqrt(puzzle.getSize() + 1);
    controls.setLayout(new GridLayout(size, size));
    for (int i = 0; i < puzzle.getSize(); i++) {
        int k = puzzle.getListItem(i);
        if (k == puzzle.getEmptyFlag())
            controls.add(new JLabel(""));
        else {
            JButton jb = new JButton(String.valueOf(k));
            jb.addMouseListener(new MouseAdapter() {

                @Override
                public void mouseClicked(MouseEvent e) {
                    //get row and column
                }

            });
            controls.add(jb);
        }
    }
    pane.add(controls);
}
public void init(最终容器窗格){
JPanel controls=新的JPanel();
int size=(int)Math.sqrt(puzzle.getSize()+1);
setLayout(新的GridLayout(大小、大小));
对于(int i=0;i
  • 千万不要为了这个目的在按钮上添加鼠标听器。改用ActionListener
  • 为什么不创建JButton的数组或ArrayList,然后简单地遍历列表以找到合适的一个呢
  • i、 e

    在其他地方,您需要用可行的JButton对象填充网格,并将这些JButton放入GUI中

    然后在稍后的程序中,使用嵌套for循环在网格中迭代,将网格按钮与
    getSource()
    JButton进行比较

    i、 e.在JButton的ActionListener中

    public void actionPerformed(ActionEvent e) {
      for (int row = 0; row < ROWS; row++) {
        for (int col = 0; col < COLS; col++) {
           if buttonGrid[row][col] == e.getSource();
           // here you have your row and column
        }
      }
    }
    
  • 千万不要为了这个目的在按钮上添加鼠标听器。改用ActionListener
  • 为什么不创建JButton的数组或ArrayList,然后简单地遍历列表以找到合适的一个呢
  • i、 e

    在其他地方,您需要用可行的JButton对象填充网格,并将这些JButton放入GUI中

    然后在稍后的程序中,使用嵌套for循环在网格中迭代,将网格按钮与
    getSource()
    JButton进行比较

    i、 e.在JButton的ActionListener中

    public void actionPerformed(ActionEvent e) {
      for (int row = 0; row < ROWS; row++) {
        for (int col = 0; col < COLS; col++) {
           if buttonGrid[row][col] == e.getSource();
           // here you have your row and column
        }
      }
    }
    

    在这种情况下,您甚至不必搜索索引,因为您在创建按钮时就知道它们:

    for (int i = 0; i < puzzle.getSize(); i++) {
        int k = puzzle.getListItem(i);
        if (k == puzzle.getEmptyFlag())
            controls.add(new JLabel(""));
        else {
            JButton jb = new JButton(String.valueOf(k));
    
            final int rowIndex = i / size;
            final int columnIndex = i % size;
    
            // Using an ActionListener, as Hovercraft Full Of Eels already told you:
            jb.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("rowIndex "+rowIndex+" columnIndex "+columnIndex);
                }
    
            });
            controls.add(jb);
        }
    }
    
    for(int i=0;i
    在这种情况下,您甚至不必搜索索引,因为您在创建按钮时就知道它们:

    for (int i = 0; i < puzzle.getSize(); i++) {
        int k = puzzle.getListItem(i);
        if (k == puzzle.getEmptyFlag())
            controls.add(new JLabel(""));
        else {
            JButton jb = new JButton(String.valueOf(k));
    
            final int rowIndex = i / size;
            final int columnIndex = i % size;
    
            // Using an ActionListener, as Hovercraft Full Of Eels already told you:
            jb.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("rowIndex "+rowIndex+" columnIndex "+columnIndex);
                }
    
            });
            controls.add(jb);
        }
    }
    
    for(int i=0;i
    噢。我懂了。我不知道。我懂了。我不知道。