简体   繁体   中英

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

I am writing a program, and the mouse listener, mousePressed(), does not seem to respond. I have written a few GUI programs now, and having compared this code, I do not see any significant difference that would explain the lack of mouse listening. The following code is incomplete and has portions that are meant for testing functionality only, and some of it may not make sense. I just need to know why mousePressed() is not working.

 /**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() { is not a valid constructor, it's just a normal method.

You should be using something more like public DrawPanel() { , this way the MouseListener will be registered when you create a new instance of the class

Also, make sure you're calling super.paintComponent , otherwise you will end up with a bunch of other issues...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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