gpt4 book ai didi

java - 为什么不显示这些图像?

转载 作者:行者123 更新时间:2023-11-30 08:52:13 27 4
gpt4 key购买 nike

我正在用 Java 开发老虎机,到目前为止我创建了一个按钮,它会随机生成两张图片。由于某种原因,其中一张图片出现而另一张图片没有。我不明白这个问题,因为这两张图片的代码完全相同。这是我的代码。请帮忙!

import java.applet.*;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JApplet;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.imageio.*;
import java.net.URL;
import java.io.*;
import java.awt.image.*;





public class slotmachine extends JApplet implements Runnable {
JButton b1 = new JButton("START");
JPanel p;
int int1, int2;
BufferedImage img= null;
BufferedImage img2 = null;
BufferedImage img3 = null;
BufferedImage img4 = null;
BufferedImage img5 = null;
BufferedImage img6 = null;
BufferedImage img7 = null;
BufferedImage img8 = null;
BufferedImage img9 = null;
BufferedImage img10 = null;
public slotmachine(){
init();
}


public void init() {

this.setLayout(null);
this.setSize(10000,10000);

b1.setBounds(100,100,100,100);
b1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e){
Random random1 = new Random();
int1 = random1.nextInt(10);
Random random2 = new Random();
int2 = random2.nextInt(10);
repaint();

}


});


getContentPane().add(b1);

try {

img = ImageIO.read(new File("question.png"));
img2 = ImageIO.read(new File("banana.png"));
img3 = ImageIO.read(new File("chocolate.png"));
img4 = ImageIO.read(new File("icecream.png"));
img5 = ImageIO.read(new File("bell.png"));
img6 = ImageIO.read(new File("apple.png"));
img7 = ImageIO.read(new File("money.png"));
img8 = ImageIO.read(new File("number-7.png"));
img9 = ImageIO.read(new File("necklace.png"));
img10 = ImageIO.read(new File("gloves.png"));
} catch (IOException e) {
}

repaint();

this.setVisible(true);
}


public void paint(Graphics g) {
super.paintComponents(g);
g.drawString("Int 1 is" + int1,30,30);
g.drawString("Int 2 is" + int2,30,80);
switch (int1) {
case 0:

g.setColor(Color.white);
g.fillRect(300,300,300,500);
g.drawImage(img, 300, 500, this);
break;
case 1:
g.setColor(Color.white);
g.fillRect(300,300,300,500);
g.drawImage(img2,300,500,this);
break;
case 2:
g.setColor(Color.white);
g.fillRect(300,300,300,500);
g.drawImage(img3,300,500,this);
break;
case 3:
g.setColor(Color.white);
g.fillRect(300,300,300,500);
g.drawImage(img4,300,500,this);
break;
case 4:
g.setColor(Color.white);
g.fillRect(300,300,300,500);
g.drawImage(img5,300,500,this);
break;
case 5:
g.setColor(Color.white);
g.fillRect(300,300,300,500);
g.drawImage(img6,300,500,this);
break;
case 6:
g.setColor(Color.white);
g.fillRect(300,300,300,500);
g.drawImage(img7,300,500,this);
break;
case 7:
g.setColor(Color.white);
g.fillRect(300,300,300,500);
g.drawImage(img8,300,500,this);
break;
case 8:
g.setColor(Color.white);
g.fillRect(300,300,300,500);
g.drawImage(img9,300,500,this);
break;
case 9:
g.setColor(Color.white);
g.fillRect(300,300,300,500);
g.drawImage(img10,300,500,this);
break;

}


switch (int2) {
case 0:

g.setColor(Color.white);
g.fillRect(300,300,800,500);

g.drawImage(img, 800, 500, this);
break;
case 1:
g.setColor(Color.white);
g.fillRect(300,300,800,500);
g.drawImage(img2,800,500,this);
break;
case 2:
g.setColor(Color.white);
g.fillRect(300,300,800,500);
g.drawImage(img3,800,500,this);
break;
case 3:
g.setColor(Color.white);
g.fillRect(300,300,800,500);
g.drawImage(img4,800,500,this);
break;
case 4:
g.setColor(Color.white);
g.fillRect(300,300,800,500);
g.drawImage(img5,800,500,this);
break;
case 5:
g.setColor(Color.white);
g.fillRect(300,300,800,500);
g.drawImage(img6,800,500,this);
break;
case 6:
g.setColor(Color.white);
g.fillRect(300,300,800,500);
g.drawImage(img7,800,500,this);
break;
case 7:
g.setColor(Color.white);
g.fillRect(300,300,800,500);
g.drawImage(img8,800,500,this);
break;
case 8:
g.setColor(Color.white);
g.fillRect(300,300,800,500);
g.drawImage(img9,800,500,this);
break;
case 9:
g.setColor(Color.white);
g.fillRect(300,300,800,500);
g.drawImage(img10,800,500,this);
break;

}

this.setVisible(true);


}

}

最佳答案

    img10 = ImageIO.read(new File("gloves.png"));
} catch (IOException e) {
}

该代码中有两个问题。

  1. 沙盒小程序不能使用文件,具有所有权限的小程序只能使用客户端机器上存在的文件。由于这些图像显然是应用程序资源(即由您提供),因此它们需要在服务器上(或至少在小程序的运行时类路径上)并由 URL 加载。
  2. 问题在于忽略异常!将其更改为:

    } catch (IOException e) {
    e.printStackTrace();
    }

    当然,请确保 Java Console配置为显示。如果默认电平没有输出,请提高电平再试。

关于java - 为什么不显示这些图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30251156/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com