How to get X and Y index of element inside GridLayout?

后端 未结 5 2091
不知归路
不知归路 2020-11-22 01:12

I am studying a java tutorial and saw that the way to find the x/y indexes of a JButton inside a GridLayout is to traverse a bidimensional array of buttons b which is associ

相关标签:
5条回答
  • 2020-11-22 01:33

    this solution selects everything object between like them
    first write method that get text or Everything needed for Jbuuton or jlable or.... second change under code

    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());
    
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 01:39

    This example shows how to create a grid button that knows its location on the grid. The method getGridButton() shows how to obtain a button reference efficiently based on its grid coordinates, and the action listener shows that the clicked and found buttons are identical.

    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();
                }
            });
        }
    }
    
    0 讨论(0)
  • 2020-11-22 01:44

    You have saved an array of all JButtons; you could search for ae.getSource() and you have the position.

    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 5; j++) {
        if( b[i][j] == ae.getSource() ) { 
          // position i,j
        }
      }
    }
    
    0 讨论(0)
  • 2020-11-22 01:44

    You can use setName() to store within a JButton its location(ex. button.setName(i+" "+j);) when you create it; you can then access it by splitting the string you get from button.getName() around the space. It is not an especially efficient method, but it sounds a little like what you are (or were, by now) looking for.

    0 讨论(0)
  • 2020-11-22 01:55

    From JButtons

    • JButton#setName(String);

    • JBUtton#setActionCommand(String);

    • JBUtton#setAction(Action);

    from/to Container

    SwingUtilities#convert...

    SwingUtilities#getDeepestComponentAt

    0 讨论(0)
提交回复
热议问题