繁体   English   中英

Java中的方法mousePressed()似乎不起作用

[英]Method mousePressed() in Java doesn't seem to be working

我正在编写程序,鼠标侦听器mousePressed()似乎没有响应。 我现在已经编写了一些GUI程序,并且比较了这段代码,我看不出有任何明显的差异可以解释缺少鼠标侦听的原因。 以下代码不完整,并且包含仅用于测试功能的部分,并且其中一些可能没有意义。 我只需要知道为什么mousePressed()无法正常工作。

 /**This class creates a panel that draws a polygon with as many vertices as the user desires. It will have a button that tells the component to close the 
polygon. Vertices are chosen with clicks of the mouse on the drawing surface.*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PolygonDraw extends JPanel implements ActionListener{

    DrawPanel drawingSurface;
    int[][] coordinates, oldCoordinates;
    int indices = 1;
    static int x = 50;

    public PolygonDraw(){

        //create painting panel.
        setLayout(new BorderLayout());
        drawingSurface = new DrawPanel();
        add(drawingSurface, BorderLayout.CENTER);

        JButton closePoly = new JButton("Close the Polygon");
        closePoly.addActionListener(this);

        JButton clear = new JButton("Clear");
        clear.addActionListener(this);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(0,1));
        buttonPanel.setBorder(BorderFactory.createEtchedBorder());
        buttonPanel.add(clear);
        buttonPanel.add(closePoly);
        add(buttonPanel, BorderLayout.SOUTH);

    }

    private class DrawPanel extends JPanel{

        public void DrawPanel(){

            addMouseListener(new MouseAdapter(){

                /**
                    This creates an array containing coordinates for every mouse press. Logic in this allows for the creation of array that get larger and larger as more
                    vertices are created by the user.
                */
                public void mousePressed(MouseEvent evt){//compile list of vertices

                    //create the last element in the array.
                    //create an array with i elements
                    /*coordinates = new int[indices][2];
                    coordinates[indices - 1][0] = evt.getX();
                    coordinates[indices - 1][1] = evt.getY();
                    if (oldCoordinates != null){

                        for (int i = 0; i < indices - 1; i++){

                            coordinates[i][0] = oldCoordinates[i][0];
                            coordinates[i][1] = oldCoordinates[i][1];

                        }

                    }

                    oldCoordinates = coordinates;
                    indices++;*/
                    x += 5;
                    repaint();

                }

            });

        }

        public void paintComponent(Graphics g){//draw lines between vertices, finish polygon, and fill polygon in with a color.

            //int x, y;

            //super.paintComponent(g);
            /*g.setColor(Color.BLACK);
            for(int i = 0; i <= indices; i++){
                x = coordinates[i][0];
                y = coordinates[i][1];
                g.fillOval(x + 2, y + 2, 4, 4);
            }
            */

            g.fillRect(x,50,50,50);

        }

    }

    public void actionPerformed(ActionEvent evt){//Clear drawing area, or close vertices to make polygon.



    }


}

public void DrawPanel() {不是有效的构造函数,它只是一个普通方法。

您应该使用更类似于public DrawPanel() { ,这样在创建类的新实例时将注册MouseListener

另外,请确保您正在调用super.paintComponent ,否则最终会遇到很多其他问题……

@Override
protected void paintComponent(Graphics g) {//draw lines between vertices, finish polygon, and fill polygon in with a color.
    super.paintComponent(g);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM