简体   繁体   English

我无法利用Mouselistener组件

[英]I can't draw on a mouselistener component

I can't post any code right now, since the computer I'm programming on has no internet connection, and I absolutely refuse to write it out on this phone. 我现在无法发布任何代码,因为我正在编程的计算机没有互联网连接,因此我绝对拒绝在此手机上写出来。

Basically, I have a JPanel (which implements mouseListener), which contains a Component in its contentPane. 基本上,我有一个JPanel (实现mouseListener),它的contentPane中包含一个Component The JPanel is listening for mouse events on the Component . JPanel正在侦听Component上的鼠标事件。

When I draw to the panel, it works fine except that the area under the Component (which is visible but not painting anything) just shows the Panel's background (a standard colour fill) and not the image I drew on top of it. 当我在面板上绘制时,它工作正常,除了“ Component下方的区域(可见但未绘制任何东西)仅显示面板的背景(标准颜色填充),而不是我在其顶部绘制的图像。

I get the feeling that I'm missing something fundamental to do with mouseListeners... 我感到我缺少与mouseListeners有关的基本信息...

OK, here's the whole class, now that my computer's working as intended again: 好的,这是整个课程,现在我的计算机又可以正常工作了:

(Also, it seems I was using Labels, not Components. Sorry about that.) (此外,似乎我使用的是Labels,而不是Components。对此很抱歉。)

import java.awt.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;
import javax.imageio.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ComponentEvent;
import java.util.*;

public class PictureViewer extends Container implements MouseListener, ComponentListener
{
    java.util.List<Image> images;
    public Component leftSide, rightSide;
    int currentImage;
    boolean leftMoused, rightMoused;
    boolean mouseDown;
    Image leftTab, rightTab, noImage;

    public PictureViewer()
    {
        setVisible(true);
        setBackground(Color.BLUE);
        addComponentListener(this);

        images = new ArrayList<Image>();

        leftSide = new Label();
        leftSide.setLocation(0, 0);
        leftSide.setSize(getWidth() / 2, getHeight());
        leftSide.addMouseListener(this);
        add(leftSide);

        rightSide = new Label();
        rightSide.setLocation(getWidth() / 2, 0);
        rightSide.setSize(getWidth() / 2, getHeight());
        rightSide.addMouseListener(this);
        rightSide.setVisible(false);
        add(rightSide);

        noImage = Toolkit.getDefaultToolkit().getImage(getClass().getResource("Images/No Picture.png"));
        leftTab = Toolkit.getDefaultToolkit().getImage(getClass().getResource("Images/Left Tab.png"));
        rightTab = Toolkit.getDefaultToolkit().getImage(getClass().getResource("Images/Right Tab.png"));
    }


    public void addImage(Image image)
    {
        images.add(image);
    }
    public void clear()
    {
        images.clear();
    }

    public void paint(Graphics g)
    {       
        super.paint(g);

        Graphics2D g2d = (Graphics2D)g;

        Image imageToDraw;

        if (images.size() > 0)
        {
            imageToDraw = images.get(currentImage);
        }
        else
        {
            imageToDraw = noImage;
        }

        g2d.drawImage(imageToDraw, getX(), getY(), getWidth(), getHeight(), 0, 0, imageToDraw.getWidth(this), imageToDraw.getHeight(this), this);
        g2d.draw(new Rectangle(0, 0, 20, 20));

        if (leftMoused)
        {
            g2d.drawImage(leftTab, getX() + 8, getY() + (int)(getSize().getHeight() - leftTab.getHeight(this) / 2), this);
        }
    }

    public void componentHidden(ComponentEvent e){}
    public void componentShown(ComponentEvent e){}
    public void componentMoved(ComponentEvent e)
    {
        componentResized(e);
    }
    public void componentResized(ComponentEvent e)
    {
        leftSide.setLocation(getLocation());
        leftSide.setSize(getWidth() / 2, getHeight());

        rightSide.setLocation((int)(getLocation().getX() + (getWidth() / 2)), (int)getLocation().getY());
        rightSide.setSize(leftSide.getSize());

        System.out.println(getSize());

        repaint();
    }   

    public void mouseReleased(MouseEvent e){}
    public void mouseClicked(MouseEvent e){}
    public void mousePressed(MouseEvent e){}
    public void mouseEntered(MouseEvent e)
    {   
    if (e.getComponent() == leftSide){
        leftMoused = true;
        System.out.println("Left");}
    else {
        rightMoused = true;
        System.out.println("Right");}

    repaint();
    }
    public void mouseExited(MouseEvent e)
    {   
    if (e.getComponent() == leftSide)
        leftMoused = false;     
    else
        rightMoused = false;

    repaint();
    }   


}

The component will also have a paintComponent method which by default will paint the container's background. 该组件还将具有paintComponent方法,默​​认情况下将绘制容器的背景。 You will need to override the method, setOpaque to false (depending on the component), or something else to keep painting from happening. 您将需要重写方法,将setOpaque为false(取决于组件)或其他方法以防止绘画发生。

However, it sounds like what you really want is to add mouse listener to the panel and have it listen to a defined boundary instead the panel rather than adding a component to the panel. 但是,听起来您真正想要的是向面板添加鼠标侦听器,并让它侦听已定义的边界而不是面板,而不是向面板添加组件。

I can't post any code right now 我现在无法发布任何代码

You wrote custom code that doesn't work and you expect us to guess what that code looks like? 您编写的自定义代码无效,您希望我们猜测该代码的外观吗? We don't have time to spend guessing what mistake you might have made. 我们没有时间去猜测您可能犯了什么错误。 The point of the forums is to make it easy for us to answer the question which means you need to provide all the information we need to help solve the problem. 论坛的目的是使我们易于回答问题,这意味着您需要提供我们需要的所有信息来帮助解决问题。

That is why you need to provide a SSCCE that demonstrates the problem. 因此,您需要提供一个SSCCE来说明问题。

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

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