-3

我正在寻找从包含案例的棋盘中获取坐标 x/y !我实际上使用 System.out.println 来获取它,但这不是好方法......我需要从方法 getX() 和 getY() 获取这些数据!

列:AH 行:1-8

我搜索了 3 个小时......

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class CoordBoutons extends JFrame {

        CoordBoutons() {
            super("GridLayout");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            Container contenant = getContentPane();
            contenant.setLayout(new GridLayout(8, 8));

            for (int i = 0; i < 8; i++) {
                for (int j = 0; j < 8; j++) {
                    contenant.add(new CaseEchiquier(i, j));
                }
            }

            pack();
            setVisible(true);
        }

        class CaseEchiquier extends JPanel {

            private int lin, col;

            CaseEchiquier(int i, int j) {
                lin = i;
                col = j;
                setPreferredSize(new Dimension(80, 75));
                setBackground((i + j) % 2 == 0 ? Color.WHITE : Color.GRAY);
                addMouseListener(new MouseAdapter() {
                  /*  private Color background;

                    @Override
                    public void mousePressed(MouseEvent e) {
                        background = getBackground();
                        setBackground(Color.RED);
                        repaint();

                    }
                  */

                    @Override
                    public void mousePressed(MouseEvent e) {
                         System.out.println((char)('a' + col) + "" + (8 - lin));
                    }

                    @Override
                    public void mouseEntered(MouseEvent e) {

                    }

                 /*   @Override
                    public void mouseReleased(MouseEvent e) {
                        setBackground(background);
                    }
                 */   

                });
    //            addActionListener(new ActionListener() {
    //                public void actionPerformed(ActionEvent evt) {
    //                    System.out.println((char) ('a' + col) + "" + (8 - lin));
    //
    //                }
    //            });
            }


        }

        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @Override
            public void run() {
                JFrame.setDefaultLookAndFeelDecorated(true);
                CoordBoutons coordBoutons = new CoordBoutons();
            }
        });
    }
}
4

1 回答 1

2

MouseEvent具有可用于获取坐标的getX和方法。getY如下使用它:

public void mouseClicked(MouseEvent e) {
   int x = e.getX(); 
}

x在这种情况下,将包含用户按下鼠标按钮的坐标。

更新:关于您的评论,我已经对代码进行了一些更新。在这里,我们使用getSource()我们强制转换为 a CaseEchiquier,因为这是 getSource 将返回的对象类型。之后,您拥有用户按下的对象,您可以执行任何您想要使用它执行的逻辑

public void mouseClicked(MouseEvent e){
    CaseEchiquier current =(CaseEchiquier)e.getSource(); // get the object that the user pressed
    int lin = current.getLin();
    int col= current.getCol();
    //do something else with the object current
}

您还需要在CaseEchiquier 类中添加一个getLinand方法getCol

于 2013-05-08T13:36:17.990 回答