- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这篇文章的目的是弄清楚为什么它会在两个按钮上复制两个图像。这很奇怪,不应该发生。这是主要目标。然后它将找到解决方案。谢谢!
它的外观图片
我做了一个MRE
它在两个按钮上输出两个图像,我不知道为什么。
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GameManager extends JFrame{
private final Map <String, String> images = new HashMap<>(2);
GameManager()
{
images.put("Articuno", "https://i.ya-webdesign.com/images/articuno-transparent-pokemon-xy-17.gif");
images.put("Rayquaza", "https://play.pokemonshowdown.com/sprites/ani-back-shiny/rayquaza.gif");
JPanel pnlPokemonInParty = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//Why does it put both images on both buttons? It actually sets the images on top of one another.
//You can tell which image is in the front and which is behind the other.
//I'm setting the buttons to be transparent. Setting the buttons to not be transparent will cover the the image below it,
//that's how I know they're being stacked on top of one another.
JButton btn1 = gifBtn("Articuno");
JButton btn2 = gifBtn("Rayquaza");
c.gridx = 0;
pnlPokemonInParty.add(btn1, c);
c.gridx = 1;
pnlPokemonInParty.add(btn2, c);
this.add(pnlPokemonInParty);
this.pack();
this.setVisible(true);
}
public JButton gifBtn(String name)
{
final JButton btn = new JButton();
URL url = null;
try {
url = new URL(images.get(name));
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
Icon icon = new ImageIcon(url);
btn.setIcon(icon);
btn.setBackground(new Color(50,50,50,0));
return btn;
}
public static void main(String[] args)
{
GameManager gameManager = new GameManager();
}
}
我可以通过不将 Jbutton 的背景颜色设置为透明来隐藏问题,但这没有解决问题。
为什么会这样?我更担心两个图像在同一个 JButton 上,但还有另一个问题在查看图像时很容易注意到,我真的不知道如何解释。
最佳答案
在发布问题时,建议发布 MRE像下面这样:
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GameManager extends JFrame{
private final Map <String, String> images = new HashMap<>(2);
GameManager()
{
images.put("Articuno", "https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/256x256/Box_Green.png");
images.put("Rayquaza", "https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/256x256/Box_Red.png");
JPanel pnlPokemonInParty = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton btn1 = gifBtn("Articuno");
JButton btn2 = gifBtn("Rayquaza");
c.gridx = 0;
pnlPokemonInParty.add(btn1, c);
c.gridx = 1;
pnlPokemonInParty.add(btn2, c);
this.add(pnlPokemonInParty);
this.pack();
this.setVisible(true);
}
public JButton gifBtn(String name)
{
final JButton btn = new JButton();
URL url = null;
try {
url = new URL(images.get(name));
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
Icon icon = new ImageIcon(url);
btn.setIcon(icon);
btn.setBackground(new Color(50,50,50,0));
return btn;
}
public static void main(String[] args)
{
GameManager gameManager = new GameManager();
}
}
该代码使用公开可用的图像运行良好,因此它表明本地资源存在问题。
MRE 让帮助变得更容易,而且它是一个强大的调试工具。很多情况下,在准备的时候,你很可能会发现问题。
btn.setBackground(new Color(50,50,50,0));
It is up to the look and feel to honor this property, some may choose to ignore it.
(引自 JComponent#setBackground(Color)
documentation 。)
编辑 2:
覆盖 paintComponent
的自定义 JButton
正常工作(具有透明颜色,其中 alfa 为 0,如 new Color(50,50,50,0)
或任何其他颜色):
class Main extends JFrame{
private final Map <String, String> images = new HashMap<>();
Main()
{
images.put("Articuno", "https://66.media.tumblr.com/d9105814c15295196a3dbe75c32ba1a0/tumblr_oagpklvBGf1scncwdo1_400.gif");
images.put("Rayquaza", "https://play.pokemonshowdown.com/sprites/ani-back-shiny/rayquaza.gif");
images.put("GreenCircle", "https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/128x128/Circle_Green.png");
images.put("RedBox", "https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/256x256/Box_Red.png");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(Color.WHITE);
this.setLayout(new FlowLayout());
this.add(gifBtn("GreenCircle"));
this.add(gifBtn("RedBox"));
this.add(gifBtn("Articuno"));
this.add(gifBtn("Rayquaza"));
this.pack();
this.setVisible(true);
}
public JButton gifBtn(String name)
{
JButton btn = new CustomButton();
try {
URL url = new URL(images.get(name));
btn.setIcon(new ImageIcon(url));
} catch (MalformedURLException ex) { ex.printStackTrace(); }
return btn;
}
public static void main(String[] args) throws Exception
{
new Main();
}
}
class CustomButton extends JButton{
private final Color bgColor = new Color(255,192,203,0);
public CustomButton() {
//setBorderPainted(false); //optioal
setContentAreaFilled(false);
setOpaque(false);
}
@Override
public void paintComponent(Graphics g){
g.setColor(bgColor);
Rectangle r = g.getClipBounds();
g.fillRect(r.x, r.y, r.width, r.height);
super.paintComponent(g);
}
}
JComponent#setBackground(Color)
documentation状态:
Direct subclasses of JComponent must override paintComponent to honor this property. It is up to the look and feel to honor this property, some may choose to ignore it.
出于某种原因 JButton
没有。
关于JButton 上的 Java ImageIcon 重复图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60924036/
我想知道是否有办法创建一个 ImageIcon,它是另一个 ImageIcon 的镜像。 在 Google 上搜索,我找到了如何使用许多 AWT 库来实现这一点。 有没有办法用 Swing 做到这一点
我想为 Tanks Game 编写代码,但在现有 ImageIcon 上设置 tank.jpg 时遇到问题,因为我希望这两个图像都可见并向用户显示,就像: JButton block = new JB
我有一个JTable,我可以在其中从数据库获取数据并显示。其中一列是 blob 对象类型,它包含一个图像(如果存在,否则为 null )。我还有一个用于 JTable 的双击事件处理程序,这样当用户仅
这是我的代码: /** * Opens file and gives a warning if non image. */ private void openFile() { final
我有一个 JLabel与 ImageIcon ,我使用 setIcon() JLabel中的函数. ImageIcon然后上来,坐在我的JLabel 的文字左侧.是否有可能拥有 ImageIcon在文
我有一个对象“Team”,其中包含一些分数变量、名称(字符串)和 Logo 。 Logo 的类型为 ImageIcon: public class Team { public String n
当我尝试在 JTable 中显示 ImageIcon 时,仅呈现描述。代码是: private void createTable() { dataModel = new AbstractTab
我编写了非常简单的代码来显示葡萄的图标,但代码仍然没有向我显示任何内容 这是我的代码 import javax.swing.*; import java.awt.*; public class Cod
我有两个问题: 我正在创建一个简单的内存游戏,它保存随机序列并期望玩家输入相同的输入,并且我正在尝试更改 JButton ImageIcon 当使用 setIcon() 单击到更亮版本的 img1 时
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 6 年前。 我试图向这些标签添加一些图像,但
是否有办法将字符串转换为 ImageIcon? 有点像这里的代码:http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/Createaniconinmem
这是我的代码 import javax.swing.*; public class Option extends JFrame{ ImageIcon img = new ImageIcon("imag
我正在开发一个 Java swing 应用程序,我正在尝试加载国际象棋棋子的图像,一切都工作得很好,直到突然出现错误: Exception in thread "main" java.lang.Sta
这个问题已经有答案了: Displaying an ImageIcon (2 个回答) 已关闭10 年前。 我有四个面板,我想向这些面板添加图像。 当我点击开始按钮时,每个面板需要更改4张图像。 我正
我在java中制作了这样的图像列表: static String[] imageList1 = { "images/bgs/bg.png", "images/bgs/image1.png",
这个问题已经有答案了: Update JLabel repeatedly with results of long running task (2 个回答) 已关闭 8 年前。 您好,我在将图像名称读
我不知道这里发生了什么,因为我在这里画了很多画,但这让我发疯。 m_background 图像只显示了一半。当我调整大小时,它看起来像是那里有一些组件,所以当我将窗口底部向上拉时,显示的图像较少,而当
我想添加一个图标而不是复选框的默认方框。每当我添加图标时,唯一的复选框标签 View 就不会显示图标或默认方框。为什么没有看到图标..我搜索了很多..但没有任何效果对我有用这是代码.. import
我想在按下按钮一次时在标签上设置图标,如果按下两次,它将删除我已经使用了 label.setIcon(null); 但它对我来说效果不佳。 public void actionPerformed(Ac
我已经研究了很多其他问题,其中人们遇到的问题与我在这里遇到的问题类似(大多数解决方案都是通过简单的错误找到的),但我终究无法弄清楚为什么我的图形不会t 显示在我的 jframe 中。我是 Java G
我是一名优秀的程序员,十分优秀!