Java 未在内部实现 Int if 已更改

Java not realizing Int inside if has changed

我的代码应该显示为老虎机类型的背景,每个通道中都有未激活的图像,当您按下旋转时,它会随机分配哪些图像可见,如果它们不应该可见,则它们是设置为不可见。我的问题是当您按下旋转时图像不显示。当我将它们设置为正常可见时,它们确实会出现。所以我相信当我尝试将它们设置为在 if/else 语句中可见时。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.Dimension;
import javax.swing.ImageIcon;

public class Slots1 extends JFrame implements ActionListener
{
  JPanel panel;
  JButton spin;
  int wheelOne = 0;
  JLabel ImgSlot1;
  JLabel ImgSlot12;
  JLabel ImgSlot13;
  JLabel ImgSlot14;
  JLabel ImgSlot15;
  JLabel ImgSlot16;
  JLabel ImgSlot17;

  public Slots1()
  {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    double width = screenSize.getWidth();
    double height = screenSize.getHeight();
    int wid = (int) width;
    int hgt = (int) height;

    JFrame frame = new JFrame("sesame chicken");
    frame.setSize(wid,hgt);
    frame.setUndecorated(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel = new JPanel(null);
    panel.setBackground(Color.GRAY);

    ImageIcon pic = new ImageIcon("Backr.png");
    JLabel layout = new JLabel(pic);
    layout.setLocation(0,0);
    layout.setSize(1366,768);

    ImageIcon Slot1 = new ImageIcon("SLu1.png"); 
    ImageIcon Slot2 = new ImageIcon("SLu2.png"); 
    ImageIcon Slot3 = new ImageIcon("SLu3.png"); 
    ImageIcon Slot4 = new ImageIcon("SLu4.png"); 
    ImageIcon Slot5 = new ImageIcon("SLu5.png"); 
    ImageIcon Slot6 = new ImageIcon("SLu6.png"); 
    ImageIcon Slot7 = new ImageIcon("SLu7.png"); 

    ImgSlot1 = new JLabel(Slot1);
    ImgSlot1.setLocation(75,25);
    ImgSlot1.setSize(150,500);
    JLabel ImgSlot12 = new JLabel(Slot2);
    ImgSlot12.setLocation(75,25);
    ImgSlot12.setSize(150,500);
    JLabel ImgSlot13 = new JLabel(Slot3);
    ImgSlot13.setLocation(75,25);
    ImgSlot13.setSize(150,500);
    JLabel ImgSlot14 = new JLabel(Slot4);
    ImgSlot14.setLocation(75,25);
    ImgSlot14.setSize(150,500);
    JLabel ImgSlot15 = new JLabel(Slot5);
    ImgSlot15.setLocation(75,25);
    ImgSlot15.setSize(150,500);
    JLabel ImgSlot16 = new JLabel(Slot6);
    ImgSlot16.setLocation(75,25);
    ImgSlot16.setSize(150,500);


    JLabel ImgSlot17 = new JLabel(Slot7);
    ImgSlot17.setLocation(75,25);
    ImgSlot17.setSize(150,500);

    ImgSlot1.setVisible(false);
    ImgSlot12.setVisible(false);
    ImgSlot13.setVisible(false);
    ImgSlot14.setVisible(false);
    ImgSlot15.setVisible(false);
    ImgSlot16.setVisible(false);
    ImgSlot17.setVisible(false);

    JButton spin = new JButton("SPIN");
    spin.setSize(100,50);
    spin.setLocation(100, 650);
    spin.setBackground(Color.green);
    spin.setForeground(Color.red);

    panel.add(ImgSlot1);
    panel.add(ImgSlot12);
    panel.add(ImgSlot13);
    panel.add(ImgSlot14);
    panel.add(ImgSlot15);
    panel.add(ImgSlot16);
    panel.add(ImgSlot17);
    panel.add(spin);
    panel.add(layout);
    frame.add(panel);

    frame.setVisible(true);
    panel.setVisible(true);

    spin.addActionListener(this);
  }
  public void actionPerformed(ActionEvent e) 
  {
    Object source = e.getSource();
    if (source == spin){
      Random rand = new Random();
      wheelOne = rand.nextInt(7);
    }

    if (wheelOne == 1){
      ImgSlot1.setVisible(true);}
    else if (wheelOne == 2){
      ImgSlot12.setVisible(true);}
    else if (wheelOne == 3){
      ImgSlot13.setVisible(true);}
    else if ((wheelOne == 4)){
      ImgSlot14.setVisible(true);}
    else if (wheelOne == 5){
      ImgSlot15.setVisible(true);}
    else if (wheelOne == 6){
      ImgSlot16.setVisible(true);}
    else if (wheelOne == 7){
      ImgSlot17.setVisible(true);}

    if (wheelOne != 1){
      ImgSlot1.setVisible(false);}
    if (wheelOne != 2){
      ImgSlot12.setVisible(false);}
    if (wheelOne != 3){
      ImgSlot13.setVisible(false);}
    if (wheelOne != 4){
      ImgSlot14.setVisible(false);}
    if (wheelOne != 5){
      ImgSlot15.setVisible(false);}
    if (wheelOne != 6){
      ImgSlot16.setVisible(false);}
    if (wheelOne != 7){
      ImgSlot17.setVisible(false);}

  }
  public static void main(String[] args)
  {
    Slots1 run = new Slots1();
  }
}

这是我最终得到的代码

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.Dimension;
import javax.swing.ImageIcon;

public class Slots1 extends JFrame implements ActionListener
{
  JPanel panel;
  JButton spin;
  JLabel ImgSlot1;
  JLabel ImgSlot2;
  JLabel ImgSlot3;
  JLabel ImgSlot4;
  JLabel ImgSlot5;
  JLabel ImgSlot6;
  JLabel ImgSlot7;
  int wheelOne = 1;
  public Slots1()
  {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    double width = screenSize.getWidth();
    double height = screenSize.getHeight();
    int wid = (int) width;
    int hgt = (int) height;

    JFrame frame = new JFrame("sesame chicken");
    frame.setSize(wid,hgt);
    frame.setUndecorated(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel = new JPanel(null);
    panel.setBackground(Color.GRAY);

    ImageIcon pic = new ImageIcon("Backr.png");
    JLabel layout = new JLabel(pic);
    layout.setLocation(0,0);
    layout.setSize(1366,768);

    ImageIcon Slot1 = new ImageIcon("SLu1.png"); 
    ImageIcon Slot2 = new ImageIcon("SLu2.png"); 
    ImageIcon Slot3 = new ImageIcon("SLu3.png"); 
    ImageIcon Slot4 = new ImageIcon("SLu4.png"); 
    ImageIcon Slot5 = new ImageIcon("SLu5.png"); 
    ImageIcon Slot6 = new ImageIcon("SLu6.png"); 
    ImageIcon Slot7 = new ImageIcon("SLu7.png"); 

    ImgSlot1 = new JLabel(Slot1);
    ImgSlot1.setLocation(75,25);
    ImgSlot1.setSize(150,500);
    ImgSlot2 = new JLabel(Slot2);
    ImgSlot2.setLocation(75,25);
    ImgSlot2.setSize(150,500);
    ImgSlot3 = new JLabel(Slot3);
    ImgSlot3.setLocation(75,25);
    ImgSlot3.setSize(150,500);
    ImgSlot4 = new JLabel(Slot4);
    ImgSlot4.setLocation(75,25);
    ImgSlot4.setSize(150,500);
    ImgSlot5 = new JLabel(Slot5);
    ImgSlot5.setLocation(75,25);
    ImgSlot5.setSize(150,500);
    ImgSlot6 = new JLabel(Slot6);
    ImgSlot6.setLocation(75,25);
    ImgSlot6.setSize(150,500);
    ImgSlot7 = new JLabel(Slot7);
    ImgSlot7.setLocation(75,25);
    ImgSlot7.setSize(150,500);

    ImgSlot1.setVisible(false);
    ImgSlot2.setVisible(false);
    ImgSlot3.setVisible(false);
    ImgSlot4.setVisible(false);
    ImgSlot5.setVisible(false);
    ImgSlot6.setVisible(false);
    ImgSlot7.setVisible(false);

    spin = new JButton("SPIN");
    spin.setSize(100,50);
    spin.setLocation(100, 650);
    spin.setBackground(Color.green);
    spin.setForeground(Color.red);

    panel.add(ImgSlot1);
    panel.add(ImgSlot2);
    panel.add(ImgSlot3);
    panel.add(ImgSlot4);
    panel.add(ImgSlot5);
    panel.add(ImgSlot6);
    panel.add(ImgSlot7);
    panel.add(spin);
    panel.add(layout);
    frame.add(panel);

    frame.setVisible(true);
    panel.setVisible(true);

    spin.addActionListener(this);
    System.out.println(spin);
  }
  public void actionPerformed(ActionEvent e) 
  {
    Object source = e.getSource();
System.out.println(source);
System.out.println(spin);
System.out.println();
    if (source.equals(spin)){
    Random rand = new Random();
    wheelOne = rand.nextInt(7);}

    if (wheelOne == 0){
      ImgSlot1.setVisible(true);}
    else if (wheelOne != 0){
    ImgSlot1.setVisible(false);}
    if (wheelOne == 1){
    ImgSlot2.setVisible(true);}
    else if (wheelOne != 1){
    ImgSlot2.setVisible(false);}
    if (wheelOne == 2){
    ImgSlot3.setVisible(true);}
    else if (wheelOne != 2){
    ImgSlot3.setVisible(false);}
    if ((wheelOne == 3)){
    ImgSlot4.setVisible(true);}
    else if (wheelOne != 3){
    ImgSlot4.setVisible(false);}
    if (wheelOne == 4){
    ImgSlot5.setVisible(true);}
    else if (wheelOne != 4){
    ImgSlot5.setVisible(false);}
    if (wheelOne == 5){
    ImgSlot6.setVisible(true);}
    else if (wheelOne != 5){
    ImgSlot6.setVisible(false);}
    if (wheelOne == 6){
    ImgSlot7.setVisible(true);}
    else if (wheelOne != 6){
      ImgSlot7.setVisible(false);}

    }

  public static void main(String[] args)
  {
    Slots1 run = new Slots1();
  }
}

您忘记初始化 ImgSlot12(和其他的),这就是它抛出异常的原因:

else if (wheelOne == 2){
        ImgSlot12.setVisible(true);}

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
   at com.company.Slots1.actionPerformed(Slots1.java:132)
   ...

第一个问题是您如何使用随机class。如果您一次又一次地实例化它然后调用 random.nextInt(7),您将始终得到相同的值(可能是 0)。

相反,您需要在构造函数中实例化它并将其分配给实例变量。

第二个问题是 nextInt(7) 不是您假设的 return 1 到 7 之间的整数值,而是 return 0 到 6 之间的整数值。