Java swing JComponent“size” [英] Java swing JComponent "size"

查看:151
本文介绍了Java swing JComponent“size”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个需要一些自定义摇摆组件的项目。到目前为止,我已经制作了一个带有一系列图像的新按钮(Java Metal外观根本不适合我的UI)。我已经在这个新组件上实现了 MouseListener ,这就是我出现问题的地方。我的小部件在悬停时更改图像,单击等除了我的 MouseListener 选择鼠标进入整个 GridLayout 容器而不是进入图像。所以我有一个大约200 * 100的图像,周围的容器大约是400 * 200,当它输入 GridLayout <时,会触发 mouseEntered 方法/ code>部分(甚至空白部分)而不是图像。我怎样才能使它只在我将鼠标悬停在图像上时被触发?我试过设置大小和边界以及其他属性都无济于事。

I'm doing a project where i need some custom swing components. So far I have made a new button with a series of images (the Java Metal look doesn't fit with my UI at all). Ive implemented MouseListener on this new component and this is where my problem arises. My widget changes image on hover, click etc except my MouseListener picks up mouse entry into a the entire GridLayout container instead of into the image. So I have an image of about 200*100 and the surrounding container is about 400*200 and the mouseEntered method is fired when it enters that GridLayout section (even blank space parts of it) instead of over the image. How can I make it so that it is only fired when I hover over the image? Ive tried setting size and bounds and other attributes to no avail.

编辑:这是我的问题的演示。正如您所看到的那样(颜色非常相似)右下角按钮只需输入 GridlLayout 的部分即可突出显示。我只想在实际拍摄图像时突出显示,而不是 GridLayout 部分。

Here's a demonstration of my issue. As you can see (sort of, colors are very similar) the bottom right button is highlighted just by entering its section of the GridlLayout. I only want it highlighted when I'm over the image actual, not the GridLayout section.

我不会添加 MouseListener 方法,因为它们只涉及切换显示的图像。

I Won't add the MouseListener methods because they just involve switching the displayed image.

public customWidget()
{
    this.setLayout(new FlowLayout());
    try {
        imageDef=ImageIO.read(new File("/home/x101/Desktop/buttonDef.png"));
        imageClick=ImageIO.read(new File("/home/x101/Desktop/buttonClick.png"));
        imageHover=ImageIO.read(new File("/home/x101/Desktop/buttonHover.png"));
        current=imageDef;
    } catch (IOException e) 
    {

        e.printStackTrace();
    }
    this.addMouseListener(this);
}

protected void paintComponent(Graphics g)
{
    super.paintComponents(g);
    g.drawImage(current, 0, 0, current.getWidth(), current.getHeight(), null);

}

编辑:添加代码部分

推荐答案

我假设您的图片只包含4个'customWidget'对象(在2x2网格中)。

I assume your image contains ONLY 4 'customWidget' objects (in a 2x2 grid).

您的代码正如我所料。您的MouseListener方法正在响应MouseEvents的'customWidget'(不是'customWidget'中绘制的图像),其大小占据图像的1/4,因此当它们进入放大区域时它们会响应。错误实际上在您的测试程序中,因为您允许自定义按钮小部件大于图像。

Your code is working as I would expect. Your MouseListener methods are responding to MouseEvents for 'customWidget' (not the image drawn in 'customWidget'), which is sized to take up 1/4 of the image, so they will respond when it enters the enlarged area. The error is actually in your Test program, because you are allowing the custom button widget to be larger than the image.

如果您想要一个提供类似图像的测试程序对你来说,你应该创建一个更大的网格(比如4x4),然后只将按钮放在每个其他网格节点上。将空组件放入间隙中。

If you want a Test program that provides an image similar to yours, you should create a larger grid (say 4x4), and then only place your buttons in every other grid node. Place an empty component into the gaps.

这篇关于Java swing JComponent“size”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆