Java swing JComponent“大小”

发布于 2024-10-01 16:53:44 字数 1263 浏览 9 评论 0原文

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

编辑:这是我的问题的演示。正如您所看到的(某种程度上,颜色非常相似),只需输入 GridlLayout 的部分,右下按钮就会突出显示。我只想在实际图像上方突出显示它,而不是 GridLayout 部分。

alt text

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

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);

}

编辑:添加代码部分

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.

EDIT: 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.

alt text

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);

}

EDIT: added code section

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一张白纸 2024-10-08 16:53:44

作为替代方案,请考虑按钮 API ,其中包括方法 setRolloverIcon()“使按钮在光标经过时显示指定的图标。”

附录:对于示例

import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ButtonIconTest {

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }

    private static void createAndShowGui() {
        String base = "http://download.oracle.com/"
            + "javase/tutorial/uiswing/examples/components/"
            + "RadioButtonDemoProject/src/components/images/";
        ImageIcon dog = null;
        ImageIcon pig = null;
        try {
            dog = new ImageIcon(new URL(base + "Dog.gif"));
            pig = new ImageIcon(new URL(base + "Pig.gif"));
        } catch (MalformedURLException ex) {
            ex.printStackTrace(System.err);
            return;
        }
        JFrame frame = new JFrame("Rollover Test");
        JPanel panel = new JPanel();
        panel.add(new JLabel(dog));
        panel.add(new JLabel(pig));

        JButton button = new JButton(dog);
        button.setRolloverIcon(pig);
        panel.add(button);

        frame.add(panel);

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

As an alternative, consider the The Button API, which includes the method setRolloverIcon() "to make the button display the specified icon when the cursor passes over it."

Addendum: For example,

import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ButtonIconTest {

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }

    private static void createAndShowGui() {
        String base = "http://download.oracle.com/"
            + "javase/tutorial/uiswing/examples/components/"
            + "RadioButtonDemoProject/src/components/images/";
        ImageIcon dog = null;
        ImageIcon pig = null;
        try {
            dog = new ImageIcon(new URL(base + "Dog.gif"));
            pig = new ImageIcon(new URL(base + "Pig.gif"));
        } catch (MalformedURLException ex) {
            ex.printStackTrace(System.err);
            return;
        }
        JFrame frame = new JFrame("Rollover Test");
        JPanel panel = new JPanel();
        panel.add(new JLabel(dog));
        panel.add(new JLabel(pig));

        JButton button = new JButton(dog);
        button.setRolloverIcon(pig);
        panel.add(button);

        frame.add(panel);

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
溺深海 2024-10-08 16:53:44

我假设您的图像仅包含 4 个“customWidget”对象(在 2x2 网格中)。

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

如果您想要一个提供与您的图像相似的图像的测试程序,您应该创建一个更大的网格(例如 4x4),然后仅将按钮放置在每个其他网格节点中。将空组件放入间隙中。

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

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.

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.

送舟行 2024-10-08 16:53:44

虽然我不会回答您的特定问题,但我希望这会有所帮助:

如果组件看起来不正确,也许您应该重用 Swing 组件并编写自定义的外观或主题。

这肯定有助于确保应用程序的外观保持一致,并且至少您正在使用正确的工具来完成您想要完成的任务。

作为旁注,请注意 Java 附带多种外观和感觉,包括模仿本机操作系统主题的外观和感觉。

请参阅:http://download.oracle.com/javase/tutorial/uiswing /lookandfeel/plaf.html

Although I won't answer to your particular question, I hope this helps:

If the components just look wrong maybe you should reuse Swing components and just write a custom Look&Feel or theme.

It would certainly help ensuring the look of the application is consistent and at least you are using the right tool for the task you want to accomplish.

As a sidenote, be aware that Java comes with multiple Look&feels, including Look&Feels made to mimic the native OS theme.

See: http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文