- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不断收到此错误:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at SlotMachinePanel.<init>(SlotMachinePanel.java:55)
at SlotMachineDriver.main(SlotMachineDriver.java:16)
我不确定我做错了什么,我是否将图像引用到了错误的位置?我尝试调试,但不确定问题是什么。请告诉我该怎么做。
下面是我的代码
//-----------------------------------
// Slot Machine Panel
//-----------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class SlotMachinePanel extends JPanel
{
private JPanel buttonPanel, newSlotsPanel, primary;
private JButton spin, cashout;
private JLabel spinlabel, cashoutlabel, slotsPanelLabel1, slotsPanelLabel2;
private JLabel imageLabel;
private ImageIcon icon;
private int spinResult = 0;
private int currentTokens = 5;
//---------------------------------------------------------------------
//Constructor: Sets up the SlotMachine GUI.
//---------------------------------------------------------------------
public SlotMachinePanel ()
{
//Creation of primary (Background Panel)
primary = new JPanel (); primary.setPreferredSize (new Dimension(325,275));
primary.setBackground (Color.red);
//Creation of newSlotsPanel (Slot Screen Panel)
newSlotsPanel = new JPanel ();
newSlotsPanel.setPreferredSize (new Dimension (300,175));
newSlotsPanel.setBackground (Color.white);
slotsPanelLabel1 = new JLabel ("Current Tokens:" + currentTokens);
slotsPanelLabel2 = new JLabel ("Result of Spin:" + spinResult);
//Creation of buttonPanel (Button Panel)
buttonPanel = new JPanel();
buttonPanel.setPreferredSize (new Dimension(300,80));
buttonPanel.setBackground (Color.blue);
//Creation of spin button
spin = new JButton("Spin");
spin.addActionListener (new SpinListener());
spinlabel = new JLabel ("Only costs 1 token to spin! Good luck!");
spinlabel.setForeground (Color.white);
//Creation of cashout button
cashout = new JButton("Cash Out");
cashout.addActionListener (new CashoutListener());
cashoutlabel = new JLabel ("No Button Pushed");
//Creation of image loader
icon = (new javax.swing.ImageIcon(getClass().getResource("casino chips.jpg")));
imageLabel = new JLabel (icon);
//Layering of Panels
add (primary);
primary.add(newSlotsPanel);
primary.add(buttonPanel);
//Adding Labels on newSlotsPanel
newSlotsPanel.add(slotsPanelLabel1);
newSlotsPanel.add(slotsPanelLabel2);
newSlotsPanel.add(imageLabel);
//Adding Buttons and Labels on Button Panel
buttonPanel.add(spin);
buttonPanel.add(cashout);
buttonPanel.add(spinlabel);
}
//*****************************************************************
// Represents a listener for Spin Button (action) events.
//*****************************************************************
private class SpinListener implements ActionListener
{
//--------------------------------------------------------------
// Updates the Spin and label when the button is pushed.
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
//Informing of pushed buttom
spinlabel.setText("Spin again or cash out");
//Initiate Random Number choice for images
Random generator = new Random();
int imageNum;
imageNum = generator.nextInt(27);
switch (imageNum)
{
case 0: //aaa
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/aaa.png")));
spinResult = 5;
currentTokens = currentTokens + spinResult - 1;
spinlabel.setText("Won 5 tokens! Nice win! Spin again!");
break;
case 1: //aac
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/aac.png")));
spinResult = 0;
currentTokens = currentTokens + spinResult - 1;
spinlabel.setText("Won 0 tokens. Spin again!");
break;
case 2: //aao
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/aao.png")));
spinResult = 1;
currentTokens = currentTokens + spinResult - 1;
spinlabel.setText("Won 1 token. Spin again!");
break;
case 3: //aca
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/aca.png")));
spinResult = 2;
currentTokens = currentTokens + spinResult - 1;
spinlabel.setText("Won 2 tokens. Spin again!");
break;
case 4: //acc
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/acc.png")));
spinResult = 4;
currentTokens = currentTokens + spinResult - 1;
spinlabel.setText("Won 4 tokens. Nice win! Spin again!");
break;
case 5: //aco
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/aco.png")));
spinResult = 0;
currentTokens = currentTokens + spinResult - 1;
spinlabel.setText("Won 0 tokens. Spin again!");
break;
case 6: //aoa
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/aoa.png")));
spinResult = 1;
spinlabel.setText("Won 1 token. Nice! Spin again!");
currentTokens = currentTokens + spinResult - 1;
break;
case 7: //aoc
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/aoc.png")));
spinResult = 0;
currentTokens = currentTokens + spinResult - 1;
spinlabel.setText("Won 0 tokens. Spin again!");
break;
case 8: //aoo
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/aoo.png")));
spinResult = 0;
currentTokens = currentTokens + spinResult - 1;
spinlabel.setText("Won 0 tokens. Spin again!");
break;
case 9: //caa
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/caa.png")));
spinResult = 3;
currentTokens = currentTokens + spinResult - 1;
spinlabel.setText("Won 3 tokens. Nice win! Spin again!");
break;
case 10: //cac
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/cac.png")));
spinResult = 2;
currentTokens = currentTokens + spinResult - 1;
spinlabel.setText("Won 2 tokens. Nice win! Spin again!");
break;
case 11: //cao
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/cao.png")));
spinResult = 0;
currentTokens = currentTokens + spinResult - 1;
spinlabel.setText("Won 0 tokens. Spin again!");
break;
case 12: //cca
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/cca.png")));
spinResult = 4;
currentTokens = currentTokens + spinResult - 1;
spinlabel.setText("Won 4 tokens. Nice win! Spin again!");
break;
case 13: //ccc
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/ccc.png")));
spinResult = 10;
currentTokens = currentTokens + spinResult - 1;
spinlabel.setText("Jackpot! Won 10 tokens. Spin again!");
break;
case 14: //cco
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/cco.png")));
spinResult = 2;
currentTokens = currentTokens + spinResult - 1;
spinlabel.setText("Won 2 tokens. Nice win! Spin again!");
break;
case 15: //coa
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/coa.png")));
spinResult = 0;
currentTokens = currentTokens + spinResult - 1;
spinlabel.setText("Won 0 tokens. Spin again!");
break;
case 16: //coc
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/coc.png")));
spinResult = 2;
currentTokens = currentTokens + spinResult - 1;
spinlabel.setText("Won 2 tokens. Nice win! Spin again!");
break;
case 17: //coo
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/coo.png")));
spinResult = 0;
currentTokens = currentTokens + spinResult - 1;
spinlabel.setText("Won 0 tokens. Spin again!");
break;
case 18: //oaa
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/oaa.png")));
spinResult = 1;
currentTokens = currentTokens + spinResult - 1;
spinlabel.setText("Won 1 token. Nice win! Spin again!");
break;
case 19: //oac
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/oac.png")));
spinResult = 0;
currentTokens = currentTokens + spinResult - 1;
spinlabel.setText("Won 0 tokens. Spin again!");
break;
case 20: //oao
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/oao.png")));
spinResult = -1;
currentTokens = currentTokens + spinResult - 1;
spinlabel.setText("Ouch... Lost 1 token. Spin again!");
break;
case 21: //oca
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/oca.png")));
spinResult = 0;
currentTokens = currentTokens + spinResult - 1;
spinlabel.setText("Won 0 tokens. Spin again!");
break;
case 22: //occ
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/occ.png")));
spinResult = 1;
currentTokens = currentTokens + spinResult - 1;
spinlabel.setText("Won 1 token. Nice! Spin again!");
break;
case 23: //oco
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/oco.png")));
spinResult = -1;
currentTokens = currentTokens + spinResult - 1;
spinlabel.setText("Ouch... Lost 1 token! Spin again!");
break;
case 24: //ooa
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/ooa.png")));
spinResult = 0;
currentTokens = currentTokens + spinResult - 1;
spinlabel.setText("Won 0 tokens. Spin again!");
break;
case 25: //ooc
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/ooc.png")));
spinResult = 0;
currentTokens = currentTokens + spinResult - 1;
spinlabel.setText("Won 0 tokens. Spin again!");
break;
case 26: //ooo
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/ooo.png")));
spinResult = -5;
currentTokens = currentTokens + spinResult - 1;
spinlabel.setText("Cmon!! Lost 5 tokens. Spin again!");
break;
default:
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/ooo.png")));
}
slotsPanelLabel1.setText("Current Tokens:" + currentTokens);//returns new value of current tokens
slotsPanelLabel2.setText("Result of Spin:" + spinResult);//returns the new result of spinning
}
}
//*****************************************************************
// Represents a listener for Spin Button (action) events.
//*****************************************************************
private class CashoutListener implements ActionListener
{
//--------------------------------------------------------------
// Updates the Spin and label when the button is pushed.
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
//Informing of pushed buttom
spinlabel.setText("Spin to play again. Your cash out value is: "+
currentTokens);
imageLabel.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/programming/assignment/pkg2/programming assignment2/money.jpg"))); //
currentTokens = 5; // resets the current token amount to 5.
}
}
}
我还设置了驱动程序
//Driver for SlotsMachinePanel
import javax.swing.JFrame;
public class SlotMachineDriver
{
//--------------------------------
// Creates the main program frame.
//--------------------------------
public static void main (String[] args)
{
JFrame frame = new JFrame ("Slot Machine");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new SlotMachinePanel());
frame.pack();
frame.setVisible(true);
}
}
最佳答案
似乎返回null
。
getClass().getResource("赌场筹码.jpg")
我会检查是否是这样以及为什么。
然后相应地修复它。
关于java - 继续出现此错误;线程中出现异常 "main"java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22849049/
我正在我的应用程序后台下载视频。如果用户在下载过程中重启了应用/设备,有什么方法可以在他们下次启动应用时从他们中断的地方继续下载? 最佳答案 这主要取决于文件服务器的配置(HTTP、FTP 等)。 现
我正在试验 WPF 动画,但有点卡住了。这是我需要做的: 鼠标悬停: 淡入(2 秒内从 0% 到 100% 不透明度) MouseOut: 暂停 2 秒 淡出(2 秒内从 100% 到 0% 不透明度
我的问题是这个线程的延续: Ant: copy the same fileset to multiple places 我是映射器的新手。有人(carej?)可以分享一个使用映射器来做到这一点的例子吗
继续previous question我希望能够显示一些事件指示器即使主线程被阻塞。(基于this article)。 基于所附代码的问题: 使用 Synchronize(PaintTargetWin
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期结果
我有一个场景,其中有一个线程在等待和执行任务之间循环。但是,我想中断线程的等待(如果愿意,可以跳过其余的等待)并继续执行任务。 有人知道如何做到这一点吗? 最佳答案 我认为你需要的是实现 wait()
这是我的代码架构: while (..) { for (...; ...;...) for(...;...;...) if ( )
import java.util.Scanner; public class InteractiveRectangle { public static void main(String[] args)
如何将 continue 放入具有函数的列表理解中? 下面的示例代码... import pandas as pd l = list(pd.Series([1,3,5,0,6,8])) def inv
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 6 年前。 Improve this qu
我正在用 python 开发一个程序,遇到了一个我不知道如何解决的问题。我的意图是使用 with 语句,避免使用 try/except。 到目前为止,我的想法是能够使用 continue 语句,就像在
我对下一段代码的执行感到困惑: label: for (int i = 0; i < 100; i++) { if (i % 2 == 0) c
这很好用: #include int main(){ volatile int abort_counter = 0; volatile int i = 0; while (i
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic
如果不满足某些条件,我会尝试跳到循环的下一次迭代。问题是循环仍在继续。 我哪里出错了? 根据第一条评论更新了代码示例。 foreach ($this->routes as $route =>
如果不满足某些条件,我会尝试跳到循环的下一次迭代。问题是循环仍在继续。 我哪里出错了? 根据第一条评论更新了代码示例。 foreach ($this->routes as $route =>
Android项目中的一个需求:通过线程读取文件内容,并且可以控制线程的开始、暂停、继续,来控制读文件。在此记录下。 直接在主线程中,通过wait、notify、notifyAll去控制读文件的线
link text 我得到了引用计数的概念 所以当我执行“del astrd”时,引用计数降为零并且 astrd 被 gc 收集? 这是示例代码。这些代码是我在昨天的问题之后开发的:link text
我想首先检查我的 Range 是否有 #NA 错误,然后在退出宏之前显示包含错误的单元格地址。这是我到目前为止所做的。 现在,如果出现错误,我想显示 MsgBox警告用户错误并停止程序的其余部分执行,
while( (c = fgetc(stdin)) != EOF ){ count++; if (count == lineLen - 1){ moreChars =
我是一名优秀的程序员,十分优秀!