以下Java代码(存储在Jar文件中)可在我的计算机上使用,但是当将其部署到另一台计算机上时,我只会看到文本,而看不到音频或图像。
有任何想法吗?
import java.awt.Color;
import java.awt.Font;
import java.util.*;
import java.io.File;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.*;
import javax.swing.plaf.ColorUIResource;
public class SlotMachines
{
public static void main (String[] args)
{
UIManager.put("OptionPane.background",new ColorUIResource(250,175,0));
UIManager.put("Panel.background",new ColorUIResource(0,0,0));
String ask;
JLabel lbl = new JLabel("Input your cash?");
lbl.setFont(new Font("Georgia", Font.BOLD, 16));
lbl.setForeground(Color.WHITE);
ask = JOptionPane.showInputDialog(null, lbl);
double money = Double.parseDouble(ask);
int again;
int rerun = 1;
Random generate = new Random();
while (rerun == 1) //rerun while loop
{
JLabel pnl = new JLabel("Would you like to play Slots?");
pnl.setFont(new Font("Georgia", Font.BOLD, 16));
pnl.setForeground(Color.WHITE);
again = JOptionPane.showConfirmDialog (null, pnl);
int bet = 2;
if (again == JOptionPane.NO_OPTION)
{
JLabel lab = new JLabel("Your cash stays at: " + money);
lab.setFont(new Font("Georgia", Font.BOLD, 16));
lab.setForeground(Color.WHITE);
JOptionPane.showMessageDialog (null, lab);
}
while (again == JOptionPane.YES_OPTION)
{
File Winner = new File("jackpot.au");
File Mega = new File("mega.au");
File lose = new File("lose.au");
if (money >= 2)
{
money -= bet;
int[] arr = new int[3];
for (int i = 0; i < arr.length; i++)
{
ImageIcon icon = new ImageIcon("slotmachine.jpg");
arr[i] = generate.nextInt(10);
JLabel label = new JLabel("[" + arr[i] + "] ");
label.setFont(new Font("Impact", Font.BOLD, 125));
label.setForeground(Color.ORANGE);
JOptionPane.showMessageDialog (null, label, " ", JOptionPane.INFORMATION_MESSAGE, icon);
}
if (arr[0] == arr[1] && arr[1] == arr[2])
{
JLabel win = new JLabel("JACKPOT!!! BET X 5!");
win.setForeground(Color.CYAN);
PlaySound(Mega);
win.setFont(new Font("Arial", Font.BOLD, 140));
JOptionPane.showMessageDialog (null, win);
money += bet*5;
}
else
{
if (arr[0] == arr[1] || arr[0] == arr[2] || arr[1] == arr[2])
{
JLabel small = new JLabel("Rolling In cash! Doubles!");
small.setForeground(Color.magenta);
PlaySound(Winner);
small.setFont(new Font("Arial", Font.BOLD, 20));
JOptionPane.showMessageDialog (null, small);
money += bet*2;
}
}
JLabel cash = new JLabel("Your cash: $" + (int)money);
cash.setForeground(Color.GREEN);
cash.setFont(new Font("Georgia", Font.BOLD, 18));
JOptionPane.showMessageDialog (null, cash);
if (money >= 2)
{
JLabel big = new JLabel("Would you like to spin again?");
big.setForeground(Color.WHITE);
big.setFont(new Font("Georgia", Font.BOLD, 16));
again = JOptionPane.showConfirmDialog (null, big);
if (again != JOptionPane.YES_OPTION)
{
JLabel hell = new JLabel("You came out of hell with: $" + (int)money);
hell.setForeground(Color.RED);
hell.setFont(new Font("Georgia", Font.BOLD, 18));
JOptionPane.showMessageDialog (null, hell);
rerun = 0;
}
}
}
else
{
JLabel sorry = new JLabel("Sorry you can't afford to spin, your card is at: $" + money);
sorry.setForeground(Color.WHITE);
sorry.setFont(new Font("Georgia", Font.BOLD, 16));
PlaySound(lose);
JOptionPane.showMessageDialog (null, sorry);
again = JOptionPane.NO_OPTION;
}
}
rerun = 0;
}
}
static void PlaySound(File Sound)
{
try{
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(Sound));
clip.start();
}catch(Exception e)
{
}
}
}
最佳答案
可能的两个原因:
ClassLoader.getResource()
来检索它们,因为它们在类路径中而不在工作目录中。像这样:public static void playSound(String pathToMusicOnClasspath) {
try {
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(ClassLoader.getSystemResource(pathToMusicOnClasspath)));
clip.start();
} catch(Exception ignore) {}
}
这样称呼它:
playSound("jackpot.au");
这也应该对您的
ImageIcon
完成。ImageIcon icon = new ImageIcon(ClassLoader.getSystemResource("slotmachine.jpg"));
关于java - 在另一台主机上部署jar时没有音频/图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41529539/