Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在安装过程中更改图像图标_Java_Swing_Jbutton_Imageicon - Fatal编程技术网

Java 在安装过程中更改图像图标

Java 在安装过程中更改图像图标,java,swing,jbutton,imageicon,Java,Swing,Jbutton,Imageicon,我有两个问题: 我正在创建一个简单的记忆游戏,它保存一个随机序列,并期望玩家提供相同的输入,我正在尝试更改JButtonImageIcon当使用setIcon()单击到一个更亮的img1版本时的 private final JButton btnImg1 = new JButton(""); ImageIcon img1 = new ImageIcon("C:\\Users\\vide\\Desktop\\img1.png"); ImageIcon img1_b = new ImageIcon

我有两个问题:

  • 我正在创建一个简单的记忆游戏,它保存一个随机序列,并期望玩家提供相同的输入,我正在尝试更改
    JButton
    ImageIcon
    当使用
    setIcon()
    单击到一个更亮的
    img1
    版本时的

    private final JButton btnImg1 = new JButton("");
    ImageIcon img1 = new ImageIcon("C:\\Users\\vide\\Desktop\\img1.png");
    ImageIcon img1_b = new  ImageIcon("C:\\Users\\vide\\Desktop\\img1_b.png");
    
    try {
         btnImg1.setIcon(img1);
         Thread.sleep(2000);
         btnImg1.setIcon(img1_b);
     }
    
  • 我制作了2个
    int
    列表
    s来保存输入和随机序列,导致动态大小:

    List<Integer> seqAlea =  new ArrayList<Integer>();
    List<Integer> seqInsere =  new ArrayList<Integer>();
    int placar = 0;
    
    Random geraNumero = new Random();
    
    public void comecaJogo(){
    
        for(int i = 0; i < 3; i++){
            int numero = geraNumero.nextInt(4) + 1;             
            seqAlea.add(i, numero);
        }
    }
    
    List sequalea=new ArrayList();
    List-seqInsere=new-ArrayList();
    int placar=0;
    Random Geranumeo=新的Random();
    科梅卡约戈公共区(){
    对于(int i=0;i<3;i++){
    整数=geraNumero.nextInt(4)+1;
    加上(i,数字);
    }
    }
    

  • 有更好的方法吗?

    首先,不要在主线程中使用睡眠。如果你不想阻止它,这将使应用程序等待睡眠结束,从而“阻止”主线程

  • 对于第一个问题,此代码将解决:

    // Assuming that your image will be within your package
    final URL resource = getClass().getResource("/com/mypackage/myimage.png");
    
    final JButton btn = new JButton("My Button");
    final Image image = ImageIO.read(resource);
    final ImageIcon defaultIcon = new ImageIcon(image);
    
    btn.setIcon(defaultIcon);
    btn.addActionListener(new ActionListener() {
    
        public void actionPerformed(ActionEvent e) {
            try {
                //This will do the trick to brighten your image
                Graphics2D g2 = (Graphics2D) image.getGraphics();
    
                // Here we're creating a white color with 50% of alpha transparency
                g2.setColor(new Color(255, 255, 255, 50));
                // Fill the entire image with the new color
                g2.fillRect(0, 0, defaultIcon.getIconWidth(), defaultIcon.getIconHeight());
                g2.dispose();
                btnBtn.setIcon(new ImageIcon(image));
            } catch (Exception ex) {
                /*Although this is a bad practice, my point here is not 
                 * to explain exceptions. 
                 * But it's a good practice to always capture as many exceptions as you can
                */
            }
        }
    });
    
  • 实际上,您不需要明确地告知添加元素的位置,特别是如果它是一个序列。ArrayList不会对项目进行排序


  • 要在单击按钮时更改其图标,请添加
    btnImg1.setIcon(img1_b)发送到按钮操作侦听器。永远不要在事件调度线程中休眠。EDT被阻止时不会绘制任何内容。如果需要延迟,请使用a。因此,请澄清:我是否应该在“image image”和“ImageIcon defaultIcon”中导入相同的图像并在它们之间切换?这不像是在切换它们。我们有一个对象,让我们使用它。实际上,我们正在使用
    图像中的
    图形
    来创建和操作其颜色,使其变亮,然后用它创建一个
    新图像图标
    。假设你必须改变1000张图片的颜色,我不知道。你肯定不想添加1000多个明亮版本的图像,对吧?这是避免向程序中添加更多图像的方法。使用完Graphics2对象g2后,别忘了处理它。1+以上投票选出一个好答案。对于我自己,我会避免在程序运行时创建图标,并尝试在启动时创建所有图标(如果内存限制允许)。为了解决他的时间延迟问题,他需要使用Swing计时器。@HovercraftFullOfEels感谢您的反馈!我将修复并添加
    g2.dispose()
    。我同意你在创业时创建图标的观点。关于时间延迟,我真的不知道为什么会有,不知道是否有原因,但是,是的,摆动计时器是一个很好的解决方案!