为什么不显示这些图像?

Why don't these images show up?

我正在 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);


  }

}

不是扩展 JApplet 并覆盖它的绘制和初始化方法,而是使用 JLabel 并将其设置为具有图像图标。

例如

import java.io.File;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.ImageIcon;

public class Frame extends javax.swing.JFrame{

//Random varible
private Random rand = new Random();
//Array List to hold the file names and locations
private ArrayList<File> pictures;

//Class call
public Frame(){
    initComponents();
}

//Generates the gui. I Used netbeans to make this for me.                        
private void initComponents() {

    pictureLable1 = new javax.swing.JLabel();
    pictureLable2 = new javax.swing.JLabel();
    generateButton = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    generateButton.setText("Generate");
    generateButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            generateButtonActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(pictureLable1, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(31, 31, 31)
            .addComponent(pictureLable2, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
            .addContainerGap())
        .addGroup(layout.createSequentialGroup()
            .addGap(151, 151, 151)
            .addComponent(generateButton)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(pictureLable2, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE)
                .addComponent(pictureLable1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 139, Short.MAX_VALUE)
            .addComponent(generateButton)
            .addContainerGap())
    );

    pack();
}                   

private void generateButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
    this.generate();
}                                              

/**
 * Generates the picture and then sets the labels accordingly.
 */
public void generate(){
    //Init the array list
    pictures = new ArrayList<>();

    //Add the pictures to the array list
    pictures.add(new File("C:/Users/UserName/Desktop/TestPictures/Picture1.png"));
    pictures.add(new File("banana.png"));
    pictures.add(new File("chocolate.png"));
    pictures.add(new File("icecream.png"));
    pictures.add(new File("bell.png"));
    pictures.add(new File("apple.png"));
    pictures.add(new File("money.png"));
    pictures.add(new File("number-7.png"));
    pictures.add(new File("necklace.png"));
    pictures.add(new File("gloves.png"));

    //Gets the random number for the picture
    int number = rand.nextInt((pictures.size() -1/*Max*/ - 0/*min*/) + 1) + 0/*Min*/;

    //Set the image icon to a randomly chosen image
    this.pictureLable1.setIcon(new ImageIcon(pictures.get(number).getAbsolutePath()));

    //Gets the random number again for the second picture
    number = rand.nextInt((pictures.size() -1/*Max*/ - 0/*min*/) + 1) + 0/*Min*/;

    //Set the second image icon to a randomly chosen image
    this.pictureLable1.setIcon(new ImageIcon(pictures.get(number).getAbsolutePath()));
}

/**
 * Your run of mill main method.
 *
 * @param args
 */
public static void main(String args[]){
    new Frame().setVisible(true);
}

// Variables declaration                
public javax.swing.JButton generateButton;
public static javax.swing.JLabel pictureLable1;
public static javax.swing.JLabel pictureLable2;

}

希望这对您有所帮助。

编辑:

确保将路径名设置为与第一张图片相同,以便 java 知道图片的位置。

也尽量不要在开头用非大写字母命名你的 类,使用 Frame 而不是 frame,只是好的做法是:).

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

该代码有两个问题。

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

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

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