Java:使用图像作为按钮

发布于 2024-10-15 18:57:11 字数 217 浏览 3 评论 0原文

我想在Java中使用图像作为按钮,我尝试这样做:

BufferedImage buttonIcon = ImageIO.read(new File("buttonIconPath"));
button = new JButton(new ImageIcon(buttonIcon));

但这仍然显示图像后面的实际按钮,我只希望图像充当按钮,我该怎么做?

I would like to use an image as a button in Java, and I tried to do this:

BufferedImage buttonIcon = ImageIO.read(new File("buttonIconPath"));
button = new JButton(new ImageIcon(buttonIcon));

But this still shows the actual button behind the image, I would only like the image to function as the button, how can I do this?

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

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

发布评论

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

评论(9

本王不退位尔等都是臣 2024-10-22 18:57:11

像这样删除边框:

button.setBorder(BorderFactory.createEmptyBorder());

然后删除内容1

button.setContentAreaFilled(false);

1:取自@3sdmx添加到问题的解决方案

Remove the border like so:

button.setBorder(BorderFactory.createEmptyBorder());

and then also the contents1:

button.setContentAreaFilled(false);

1: Taken from the solution added to the question by @3sdmx

怕倦 2024-10-22 18:57:11

建议将图像设置为标签,并向标签添加鼠标侦听器以检测点击。

例子:

ImageIcon icon = ...;

JLabel button = new JLabel(icon);

button.addMouseListener(new MouseAdapter() {
  @Override
  public void mouseClicked(MouseEvent e) {
     ... handle the click ...
  }
});

A suggestion would be to set the Image as a label and add a mouse listener to the label to detect clicks.

Example:

ImageIcon icon = ...;

JLabel button = new JLabel(icon);

button.addMouseListener(new MouseAdapter() {
  @Override
  public void mouseClicked(MouseEvent e) {
     ... handle the click ...
  }
});
困倦 2024-10-22 18:57:11

buttonIcon.setBorder(new EmptyBorder(0,0,0,0));

buttonIcon.setBorder(new EmptyBorder(0,0,0,0));

哀由 2024-10-22 18:57:11
button.setBorderPainted( false );
button.setBorderPainted( false );
赠佳期 2024-10-22 18:57:11

通过将 contentAreaFilled 属性设置为 False,可以在 netbeans 中轻松完成此操作

This can be done easily in netbeans by setting the contentAreaFilled Property to False

廻憶裏菂餘溫 2024-10-22 18:57:11
    BufferedImage buttonIcon = ImageIO.read(new File("myImage.png"));
    button = new JButton(new ImageIcon(buttonIcon));
    button.setBorderPainted(false);
    button.setFocusPainted(false);
    button.setContentAreaFilled(false);
    BufferedImage buttonIcon = ImageIO.read(new File("myImage.png"));
    button = new JButton(new ImageIcon(buttonIcon));
    button.setBorderPainted(false);
    button.setFocusPainted(false);
    button.setContentAreaFilled(false);
烟凡古楼 2024-10-22 18:57:11

就写这个

button.setContentAreaFilled(false);

just write this

button.setContentAreaFilled(false);
不乱于心 2024-10-22 18:57:11

据我所知,没有简单的方法可以做到这一点,您需要重写 JButton 类的“paintComponent”方法来绘制图像,如果您只想显示图像并表现得像按钮,您可以添加一个 JPanel 绘制图像(clicky)并添加 MouseListener /MouseAdapter 处理“mousePressed”事件

As far i know, there is no easy way of doing it, you will need to override the "paintComponent" method of the JButton class to aint your image, if you only want to display an image and behave like a button, you can add a JPanel wich draws the image (clicky) and add a MouseListener/MouseAdapter to handle the "mousePressed" event

悍妇囚夫 2024-10-22 18:57:11

我按照以下步骤操作,可以成功创建“ImageButton”。

  1. 创建一个 JButton
  2. 添加一个动作侦听器
  3. 设置一个图像图标(注意我已将 info.png 图标放在 src\main\resources 文件夹中并使用类加载器加载)。项目结构如下。 项目文件夹结构
  4. 设置一个空的 Border
  5. 禁用内容区域填充
  6. 禁用可聚焦性
  7. 添加到 contentPane

PFB 对我有用的代码

JButton btnNewButton = new JButton("");
btnNewButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        System.out.println("Info clicked");
    }
});

String iconfilePath = this.getClass().getClassLoader().getResource("info.png").getFile();
btnNewButton.setIcon(new ImageIcon(iconfilePath));
btnNewButton.setBounds(10, 438, 39, 31);
btnNewButton.setBorder(BorderFactory.createEmptyBorder());
btnNewButton.setContentAreaFilled(false);
btnNewButton.setFocusable(false);
contentPane.add(btnNewButton);

上面代码产生的输出按钮如下

在此处输入图像描述

I followed below steps and i could create an 'ImageButton' successfully.

  1. Create a JButton
  2. Added an action listener
  3. Set an image icon (note i have placed the info.png icon in the src\main\resources folder and loaded using class loader). The project structure is as here. Project folder structure
  4. Set an empty Border
  5. Disabled the content area filling
  6. Disabled the focusability
  7. Added to the contentPane

PFB the code that worked for me

JButton btnNewButton = new JButton("");
btnNewButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        System.out.println("Info clicked");
    }
});

String iconfilePath = this.getClass().getClassLoader().getResource("info.png").getFile();
btnNewButton.setIcon(new ImageIcon(iconfilePath));
btnNewButton.setBounds(10, 438, 39, 31);
btnNewButton.setBorder(BorderFactory.createEmptyBorder());
btnNewButton.setContentAreaFilled(false);
btnNewButton.setFocusable(false);
contentPane.add(btnNewButton);

The output button resulted from above code is as below

enter image description here

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