简体   繁体   English

mousePressed方法将不响应

[英]mousePressed method won't respond

I'm trying to do something very basic: write program that will draw a line on a frame between two points: the point that the mouse was pressed on and the point where the mouse was released on. 我正在尝试做一些非常基本的事情:编写程序以在两点之间的框架上画一条线:鼠标被按下的点和鼠标被释放的点。

I have these classes: 我有这些课:

import java.awt.Graphics;

public class Line implements Drawable{
    private int x1,x2,y1,y2;
    public Line( int x1,int x2,int y1,int y2){
        this.x1=x1;
        this.x2=x2;
        this.y1=y1;
        this.y2=y2;
    }
    public void draw(Graphics g){
        g.drawLine(x1, y1, x2, y2);
    }
}

import java.awt.Graphics;

public interface Drawable {
    public void draw(Graphics g);
}

import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JPanel;

@SuppressWarnings("serial")
public class LinePanel extends JPanel {
    ArrayList<Line> lines = new ArrayList<Line>();

    public LinePanel() {
        addMouseListener(new MouseAdapter() {
            Point p1, p2;

            @Override
            public void mousePressed(MouseEvent e) {
                p1 = e.getPoint();
                System.out.println("pressed");
            }

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                System.out.println("clicked");

            }
            @Override
            public void mouseReleased(MouseEvent e) {
                p2 = e.getPoint();
                lines.add(new Line(p1.x, p1.y, p2.x, p2.y));
            }

        });
        System.out.println("after add mouselistener");
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (Line l : lines) {
            l.draw(g);
        }
    }
}

import javax.swing.JFrame;

public class LaunchLinesGui {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame frame=new JFrame();
        frame.setSize(500, 500);
        frame.setVisible(true);

        LinePanel panel=new LinePanel();
        frame.add(panel);
    }

}

The problem is that when I press themouse on any place on the frame I get no response and even the System.out.println 's of mousePressed methods are not printed on the console. 问题是,当我在框架上的任何位置按鼠标时,都没有响应,甚至没有在控制台上打印出System.out.printlnmousePressed方法。 What am I doing wrong? 我究竟做错了什么?

The problem was that I didn't set the size of the panel and the clicks inside the frame were not caught inside the panel . 问题在于我没有设置面板的大小, 框架内的咔嗒声也没有被面板捕获。

The fix was to add setSize(500, 500); 解决方法是添加setSize(500, 500); in LinePanel constructor. LinePanel构造函数中。

It is the job of the layout manager to determine the size/location of the components added to a frame. 布局管理器的工作是确定添加到框架中的组件的大小/位置。

The reason your panel has a size of (0, 0) is because you add the panel to the frame AFTER you make the frame visible so the layout manager is never invoked. 面板大小为(0,0)的原因是,在将面板显示为可见之后将面板添加到框架中,因此永远不会调用布局管理器。

Also, you should not set the size of the frame you should let the layout manager do its job by invoking the pack() method on the frame. 同样,您不应设置框架的大小,而应通过在框架上调用pack()方法让布局管理器完成其工作。

The proper order of your code should be: 您的代码的正确顺序应为:

    LinePanel panel=new LinePanel();

    JFrame frame=new JFrame();
    frame.add(panel);
    //frame.setSize(500, 500);
    frame.pack();
    frame.setVisible(true);

Also when you do custom painting the panel is responsible for determining its own size, so you also need to override the getPreferredSize() method of the panel: 同样,当您自定义绘画时,面板负责确定其自身的大小,因此,您还需要重写面板的getPreferredSize()方法:

@Override
public Dimension getPreferredSize()
{
    return new Dimension(500, 500);
}

Now the layout manager can do its job properly. 现在,布局管理器可以正确执行其工作。

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

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