我的代码应该显示为老虎机类型背景,每个通道中的图像未激活,当您按下旋转时,随机化哪些可见,如果它们不可见,则将它们设置为无形。我的问题是当你按下旋转图像不显示。当我将它们正常显示时,它们会显示出来。所以我相信当我尝试在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();
}
}
答案 0 :(得分:0)
你忘了初始化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)
...
答案 1 :(得分:0)
第一个问题是你如何使用Random类。如果你反复实例化它然后再调用random.nextInt(7)
,你将总是得到相同的值(可能是0)。
相反,您需要在构造函数中将其实例化并将其分配给实例变量。
第二个问题是nextInt(7)
没有像你想象的那样返回介于1和7之间的int值,但它返回0到6之间的int值。