来自图像的JButton [英] JButton from image

查看:128
本文介绍了来自图像的JButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从图像中制作JButton。但当我这样做时:

  private void AddMainActionsButtons(Container powZawartosci){
JPanel mainActionButtons = new JPanel( );
JButton applyButton = createImageButton(obrazki / apply.png);
mainActionButtons.add(applyButton);
powZawartosci.add(mainActionButtons);
mainActionButtons.setBounds(150,530,400,90);
}

private JButton createImageButton(String imagePath){
ImageIcon icon = uploadImage(imagePath,);
JButton button = new JButton(icon);

返回按钮;
}

JButton看起来不好,因为图像在按钮组件内部,实际上我得到了图像上的图像。如何创建与我的图像一样大的JButton,以及如何通过图像100%覆盖它。换句话说:如何使我的图像成为按钮?

解决方案

你需要通过删除按钮来占用空间边界。有关详细信息,请参见,了解使用更简单图像的版本 JToggleButton 实例。




I want to make JButton from image. But when i do it like this:

private void AddMainActionsButtons(Container powZawartosci){
  JPanel mainActionButtons = new JPanel();
  JButton applyButton = createImageButton("obrazki/apply.png");
  mainActionButtons.add(applyButton);
  powZawartosci.add(mainActionButtons);
  mainActionButtons.setBounds(150,530,400,90);
}

private JButton createImageButton(String imagePath){
  ImageIcon icon = uploadImage(imagePath,"");
  JButton button = new JButton(icon);

  return button;
}

JButton looks bad, because image is inside button component, and in fact I got image on image. How can I create as big JButton as my image, and how to cover it by image in 100%. In other words: how to make my image be a Button?

解决方案

You need to take the space out of the buttons by removing the border. See this answer for details.

Seen above is the single image split into 9 parts. The N/S/E/W are buttons (East is activated - shown with a red border) and the rest are labels - each containing the relevant part of the original image.

See also this answer for a version that uses simpler images with JToggleButton instances.

这篇关于来自图像的JButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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