我试图找出将一些音乐添加到我用Java创建的基本测验游戏中的最佳方法。想知道是否有人对什么是最好的有任何意见......?我已经掌握了一些添加声音的基础知识,但是我无法理解这一点。我只是不知道在哪里放置音频文件的代码。
这是我的游戏:
Quiz.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
//Create the main quiz and its elements
class Quiz extends JFrame implements ActionListener {
JPanel panel;
JPanel panelresult;
JRadioButton choice1;
JRadioButton choice2;
JRadioButton choice3;
JRadioButton choice4;
ButtonGroup bg;
JLabel lblmess;
JButton btnext;
// These arrays will hold the strings of the questions and the answers.
String[][] questionsAnswers;
String[][] correctAnswer;
// This will be the key in the HashMap.
int qaid;
// Using a HashMap to output my data.
HashMap<Integer, String> map;
// Setting up the game board.
Quiz() {
initializedata();
setTitle("Music Quiz");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(1100, 600);
setLocation(300, 100);
setResizable(false);
Container cont = getContentPane();
cont.setLayout(null);
cont.setBackground(Color.GRAY);
bg = new ButtonGroup();
choice1 = new JRadioButton("Choice1", true);
choice2 = new JRadioButton("Choice2", false);
choice3 = new JRadioButton("Choice3", false);
choice4 = new JRadioButton("Choice4", false);
bg.add(choice1);
bg.add(choice2);
bg.add(choice3);
bg.add(choice4);
lblmess = new JLabel("Choose a correct anwswer");
lblmess.setForeground(Color.BLUE);
lblmess.setFont(new Font("Berlin Sans FB", Font.ITALIC, 23));
btnext = new JButton("Next");
btnext.setForeground(Color.GREEN);
btnext.setFont(new Font("Arial", Font.BOLD, 35));
btnext.addActionListener(this);
panel = new JPanel();
panel.setBackground(Color.LIGHT_GRAY);
panel.setLocation(10, 10);
panel.setSize(1100, 550);
panel.setLayout(new GridLayout(6, 2));
panel.add(lblmess);
panel.add(choice1);
panel.add(choice2);
panel.add(choice3);
panel.add(choice4);
panel.add(btnext);
cont.add(panel);
setVisible(true);
// Set the inital HashMap key to 0
qaid = 0;
// Call the readqa method with a starting point of 0
readqa(qaid);
}
// Method for determining what buttons are being selected.
public void actionPerformed(ActionEvent e) {
if (btnext.getText().equals("Next")) {
if (qaid < 9) {
map.put(qaid, getSelection());
qaid++;
readqa(qaid);
} else {
map.put(qaid, getSelection());
btnext.setText("Show answers");
}
} else if (btnext.getText().equals("Show answers"))
new Report();
}
public void initializedata() {
// questionsAnswers stores pairs of questions and its possible answers
questionsAnswers = new String[10][5];
questionsAnswers[0][0] = "1) Which singer was in MadMax:BeyondTheThunderdome?";
questionsAnswers[0][1] = "David Bowie";
questionsAnswers[0][2] = "Tina Turner";
questionsAnswers[0][3] = "Mick Fleetwood";
questionsAnswers[0][4] = "James Brown";
questionsAnswers[1][0] = "2) Which reggae singing star died 11th May 1981?";
questionsAnswers[1][1] = "Peter Tosh";
questionsAnswers[1][2] = "Jimmy Cliff";
questionsAnswers[1][3] = "Bob Marley";
questionsAnswers[1][4] = "Gregory Isaacs";
questionsAnswers[2][0] = "3) Who was The Beatles first drummer?";
questionsAnswers[2][1] = "Richard Starks";
questionsAnswers[2][2] = "Ringo Starr";
questionsAnswers[2][3] = "Peter Gunn";
questionsAnswers[2][4] = "Peter Best";
questionsAnswers[3][0] = "4) What is Bruce Springsteen's nickname?";
questionsAnswers[3][1] = "The Boss";
questionsAnswers[3][2] = "The Big Man";
questionsAnswers[3][3] = "The Killer";
questionsAnswers[3][4] = "The Ice Man";
questionsAnswers[4][0] = "5) Led Zeppelin rose from the ashes of what group?";
questionsAnswers[4][1] = "The Byrds";
questionsAnswers[4][2] = "The Yardbirds";
questionsAnswers[4][3] = "The Animals";
questionsAnswers[4][4] = "The Pretty Things";
questionsAnswers[5][0] = "6) Which Beatle is pictured first walking across the road on the Abbey Road album cover?";
questionsAnswers[5][1] = "John";
questionsAnswers[5][2] = "Paul";
questionsAnswers[5][3] = "George";
questionsAnswers[5][4] = "Ringo";
questionsAnswers[6][0] = "7) In 1987, I Still Haven’t Found What I’m Looking For was U2's first number one hit in which country?";
questionsAnswers[6][1] = "Ireland";
questionsAnswers[6][2] = "Canada";
questionsAnswers[6][3] = "U.S.A.";
questionsAnswers[6][4] = "Great Britain";
questionsAnswers[7][0] = "8) Which Jimi Hendrix classic contained the line ‘Scuse me while I kiss the sky?";
questionsAnswers[7][1] = "Hey Joe";
questionsAnswers[7][2] = "Wind Cries Mary";
questionsAnswers[7][3] = "Fire";
questionsAnswers[7][4] = "Purple Haze";
questionsAnswers[8][0] = "9) What show did MTV introduce in 1987 to feature heavy metal music and news?";
questionsAnswers[8][1] = "Headbangers Ball";
questionsAnswers[8][2] = "Rock N Heavy";
questionsAnswers[8][3] = "Night Tracks";
questionsAnswers[8][4] = "Metal Mayhem";
questionsAnswers[9][0] = "10) What Miami rap group created controversy with their album “As Nasty As They Wanna Be”?";
questionsAnswers[9][1] = "N.W.A.";
questionsAnswers[9][2] = "Run DMC";
questionsAnswers[9][3] = "2 Live Crew";
questionsAnswers[9][4] = "Digital Underground";
// correctAnswer stores pairs of question and its correct answer
correctAnswer = new String[10][2];
correctAnswer[0][0] = "Which singer was in Mad Max: Beyond The Thunderdome?";
correctAnswer[0][1] = "Tina Turner";
correctAnswer[1][0] = "Which reggae singing star died 11th May 1981?";
correctAnswer[1][1] = "Bob Marley";
correctAnswer[2][0] = "Who was The Beatles first drummer?";
correctAnswer[2][1] = "Peter Best";
correctAnswer[3][0] = "What is Bruce Springsteen's nickname?";
correctAnswer[3][1] = "The Boss";
correctAnswer[4][0] = "Led Zeppelin rose from the ashes of what group?";
correctAnswer[4][1] = "The Yardbirds";
correctAnswer[5][0] = "Which Beatle is pictured first walking across the road on the Abbey Road album cover?";
correctAnswer[5][1] = "John";
correctAnswer[6][0] = "In 1987, I Still Haven’t Found What I’m Looking For was U2's first number one hit in which country?";
correctAnswer[6][1] = "U.S.A.";
correctAnswer[7][0] = "Which Jimi Hendrix classic contained the line 'Scuse me while I kiss the sky?";
correctAnswer[7][1] = "Purple Haze";
correctAnswer[8][0] = "What show did MTV introduce in 1987 to feature heavy metal music and news?";
correctAnswer[8][1] = "Headbangers Ball";
correctAnswer[9][0] = "What Miami rap group created controversy with their album “As Nasty As They Wanna Be”?";
correctAnswer[9][1] = "2 Live Crew";
// Create a map object to store pairs of questions and the selected
// answer
map = new HashMap<Integer, String>();
}
// This will get the user selection and store it for later use
public String getSelection() {
String selectedChoice = null;
Enumeration<AbstractButton> buttons = bg.getElements();
while (buttons.hasMoreElements()) {
JRadioButton temp = (JRadioButton) buttons.nextElement();
if (temp.isSelected()) {
selectedChoice = temp.getText();
}
}
return (selectedChoice);
}
public void readqa(int qid) {
lblmess.setText(" " + questionsAnswers[qid][0]);
choice1.setText(questionsAnswers[qid][1]);
choice2.setText(questionsAnswers[qid][2]);
choice3.setText(questionsAnswers[qid][3]);
choice4.setText(questionsAnswers[qid][4]);
choice1.setSelected(true);
}
public void reset() {
qaid = 0;
map.clear();
readqa(qaid);
btnext.setText("Next");
}
public int calCorrectAnswer() {
int qnum = 10;
int count = 0;
for (int qid = 0; qid < qnum; qid++)
if (correctAnswer[qid][1].equals(map.get(qid)))
count++;
return count;
}
public class Report extends JFrame {
Report() {
setTitle("Answers");
setSize(1450, 550);
setBackground(Color.WHITE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
reset();
}
});
Draw d = new Draw();
add(d);
setVisible(true);
}
class Draw extends Canvas {
public void paint(Graphics g) {
int qnum = 10;
int x = 10;
int y = 20;
for (int i = 0; i < qnum; i++) {
// Print the 1st column
g.setFont(new Font("Arial", Font.PLAIN, 18));
g.drawString(i + 1 + ". " + correctAnswer[i][0], x, y);
y += 30;
g.setFont(new Font("Candara", Font.PLAIN, 22));
g.drawString(" Correct answer:" + correctAnswer[i][1],
x, y);
y += 30;
g.drawString(" Your answer:" + map.get(i), x, y);
y += 30;
// Print the 2nd column
if (y > 400) {
y = 20;
x = 550;
}
}
// Show number of correct answers
int numc = calCorrectAnswer();
g.setColor(Color.BLUE);
g.setFont(new Font("Arial", Font.BOLD, 14));
g.drawString("Number of correct answers:" + numc, 300, 500);
}
}
}
}
QuizProgram.java
public class QuizProgram {
public static void main(String args[]) {
new Quiz();
}
}