GridLayout +鼠标监听器

时间:2011-11-14 20:08:45

标签: java swing mouse listener grid-layout

好的家伙我有问题,我不知道怎么知道在网格布局上点击了哪个单元格,是否有任何功能?

我在容器上有网格布局,包含10行和10列,我想要的是所有单元格的鼠标监听器,所以当我单击单元格(2,1)时,它会说我点击了哪个单元格,因为老鼠听众。

任何线索?非常感谢

3 个答案:

答案 0 :(得分:8)

将MouseListener添加到使用GridLayout并保存网格中组件的Container。然后在mousePressed上使用MouseEvent对象,比如名为myMouseEvent来获取点击的点,并调用getComponentAt(myMouseEvent.getPoint);来获取被点击的组件。没有麻烦没事。

例如:

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();
         }
      });
   }
}

答案 1 :(得分:0)

您可以使用宽度和容器的高度,获取容器高度的宽度并将其除以单元格数并找到单元格的范围(2,1),例如,如果宽度和高度为容器等于100和行数,列= 10 * 10然后单元格(2,1)的左坐标是x = 10,y = 0并且通过方法getX()和mouseListener类的getY(),你找到单击鼠标的位置,然后如果鼠标位于单元格(2,1)范围内的x和y,则可以知道它单击了单元格(2,1)。 enter image description here

答案 2 :(得分:-1)

以递归方式将mouselistener添加到UI中的所有组件并进行调试......