GridLayout + Mouse Listener

ε祈祈猫儿з 提交于 2019-11-30 15:41:32

问题


Okay guys I have a problem, I dont know how know which cell was clicked on a grid layout, is there any function?

I have grid layout on container, for 10 rows and 10 columns, and what I want is a mouse listener to all the cells, so when I click cell (2,1) it would say which cell I am clicking because of the mouse listener.

Any clues? thanks alot in advance


回答1:


Add a MouseListener to the Container that uses GridLayout and that holds the components in the grid. Then on mousePressed use the MouseEvent object, say called myMouseEvent, to get the point of the click and call getComponentAt(myMouseEvent.getPoint); to get the clicked component. No muss no fuss.

For example:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

public class TestComponentAt extends JPanel {
   private static final int ROW_COUNT = 10;
   private static final int W = 60;
   private static final int H = W;
   private static final Dimension PREF_SIZE = new Dimension(W, H);
   protected static final Color SELECTION_COLOR = Color.pink;
   private JPanel selectedPanel = null;
   private Color originalColor = null;

   public TestComponentAt() {
      setLayout(new GridLayout(ROW_COUNT, ROW_COUNT, 1, 1));
      setBackground(Color.black);
      for (int i = 0; i < ROW_COUNT * ROW_COUNT; i++) {
         JPanel panel = new JPanel();
         String name = String.format("[%d, %d]", 
               i / ROW_COUNT, i % ROW_COUNT);
         panel.setName(name);
         if (i == 0) {
            originalColor = panel.getBackground();
         }
         panel.setPreferredSize(PREF_SIZE);
         add(panel);
      }
      addMouseListener(new MouseAdapter() {
         @Override
         public void mousePressed(MouseEvent e) {
            JPanel panel = (JPanel) getComponentAt(e.getPoint());
            if (panel == null || panel == TestComponentAt.this) {
               return;
            }
            if (selectedPanel != null) {
               selectedPanel.setBackground(originalColor);
               selectedPanel.removeAll();
               selectedPanel.revalidate();
               selectedPanel.repaint();
            }
            selectedPanel = panel;
            selectedPanel.setBackground(SELECTION_COLOR);
            selectedPanel.add(new JLabel(selectedPanel.getName()));
            selectedPanel.revalidate();
            selectedPanel.repaint();
         }
      });
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("TestComponentAt");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new TestComponentAt());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}



回答2:


You can work with the width an height of your container, get the width an height of container and divided it to the number of cells and find the range of the cell(2,1),for example if width and height of container equal to 100 and number of rows,column=10*10 then the left coordinate of cell(2,1) is x=10,y=0 and by method getX() and getY() of the mouseListener class you find the location of mouse clicked, then if the x and y of mouse location in the range of cell(2,1) you can know that it clicked on the cell(2,1).




回答3:


recursively add a mouselistener to all Components in your UI and debug...



来源:https://stackoverflow.com/questions/8127418/gridlayout-mouse-listener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!