所以我正在使用Java Eclipse开发游戏,但是在尝试添加声音时遇到了一个问题。在这里,有关添加声音的大多数问题都使用sun.audio。*;。但是当我导入它时,它说它不可用。我为什么不能使用sun.audio有理由吗?有没有其他方法可以将背景音乐添加到我的游戏中?
问题只与声音有关,但我为整个课程添加了代码,以便您可以看到我正在使用的所有内容。
import java.awt.Color;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.io.*;
import sun.audio.*; // error
@SuppressWarnings("serial")
public class MainScreen extends JFrame implements ActionListener{
JFrame startFrame = new JFrame();
JLabel startLabel = new JLabel();
static JLabel infoLabel = new JLabel();
JPanel startPanel = new JPanel();
TextArea textArea = new TextArea();
TextField textField = new TextField();
static JButton playGameButton = new JButton("PLAY");
PacManGUIMenubar menubar = new PacManGUIMenubar();
public MainScreen() {
startPanelSetup();
music();
}
private static void music() {
//this is the method with the music
}
private void startPanelSetup() {
startPanel.setBounds(1, 1, 598, 798);
startPanel.setBackground(Color.yellow);
startPanel.setBorder(BorderFactory.createLineBorder(Color.orange));
playGameButton.setBounds(225,450,100,50);
playGameButton.setLayout(null);
playGameButton.setVisible(false);
playGameButton.addActionListener(this);
startFrame.add(playGameButton);
infoLabel.setLayout(null);
infoLabel.setBounds(50,50,300,50);
infoLabel.setBackground(Color.YELLOW);
infoLabel.setText("Select a level from file to play");
infoLabel.setBorder(javax.swing.BorderFactory.createEmptyBorder());
startFrame.add(infoLabel);
startLabel.setIcon(new ImageIcon(new ImageIcon("images/PacMan.jpg").getImage().getScaledInstance(596, 250, 0)));
startLabel.setBounds(2,100,600,300);
startLabel.setVisible(true);
startFrame.add(startLabel);
startFrame.setSize(600, 800);
startFrame.setLayout(null);
startFrame.add(startPanel);
startFrame.setVisible(true);
startFrame.setResizable(false);
startFrame.setJMenuBar(menubar);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == playGameButton){
startFrame.dispose();
new PacManGUI();
}
}
}
谢谢