我正在为大学的作业制作一个简单的程序。我有5张图片,5个按钮和5个JLabel。当我从JComboBox中选择一种语言时,Jframe中的文本将更改为所选语言。我想把声音放在我的播放按钮后面,这样当他们点击" Play"在红色下,它会说红色。当我把它改成法语时,我想要它说Rouge(我可以用我的ActionListener自己做这部分)但是你能帮我把声音放进去吗?我有5个.wav文件我想用。这是我的代码。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
import java.net.*;
import sun.audio.*;
import java.util.*;
public class MainGUI extends JFrame implements ActionListener {
JPanel ImagePanel, buttonPanel, buttonPanel2, buttonPanel3, buttonPanel4, buttonPanel5, textPanel, textPanel2, textPanel3, textPanel4, textPanel5, panel, panel2;
JLabel black, blue, green, yellow, red;
JButton play, play2, play3, play4, play5;
JLabel blackText, blueText, greenText, yellowText, redText;
JComboBox language;
ResourceBundle res;
Locale en = new Locale("en", "EN");
Locale fr = new Locale("fr", "FR");
Locale sv = new Locale("se", "SV");
String languages [] = {"English", "French", "Swedish"};
MainGUI() {
super("Assignment Two - Language Learning");
res = ResourceBundle.getBundle("ProgramProperties_en");
Container c = getContentPane();
language = new JComboBox(languages);
language.addActionListener(this);
play = new JButton("Play");
play2 = new JButton("Play");
play3 = new JButton("Play");
play4 = new JButton("Play");
play5 = new JButton("Play");
play.addActionListener(this);
ImagePanel = new JPanel();
panel = new JPanel();
panel2 = new JPanel();
textPanel = new JPanel();
textPanel2 = new JPanel();
textPanel3 = new JPanel();
textPanel4 = new JPanel();
textPanel5 = new JPanel();
buttonPanel = new JPanel();
buttonPanel2 = new JPanel();
buttonPanel3 = new JPanel();
buttonPanel4 = new JPanel();
buttonPanel5 = new JPanel();
black = new JLabel();
blue = new JLabel();
green = new JLabel();
yellow = new JLabel();
red = new JLabel();
blackText = new JLabel("Black");
blueText = new JLabel("Blue");
greenText = new JLabel("Green");
yellowText = new JLabel("Yellow");
redText = new JLabel("Red");
ImageIcon cBlack = new ImageIcon("src/black.png");
black.setIcon(cBlack);
ImageIcon cBlue = new ImageIcon("src/blue.png");
blue.setIcon(cBlue);
ImageIcon cGreen = new ImageIcon("src/green.png");
green.setIcon(cGreen);
ImageIcon cYellow = new ImageIcon("src/yellow.png");
yellow.setIcon(cYellow);
ImageIcon cRed = new ImageIcon("src/red.png");
red.setIcon(cRed);
ImagePanel.add(black);
ImagePanel.add(blue);
ImagePanel.add(green);
ImagePanel.add(yellow);
ImagePanel.add(red);
buttonPanel.add(play);
buttonPanel2.add(play2);
buttonPanel3.add(play3);
buttonPanel4.add(play4);
buttonPanel5.add(play5);
buttonPanel.add(Box.createRigidArea(new Dimension(20, 0)));
buttonPanel2.add(Box.createRigidArea(new Dimension(20, 0)));
buttonPanel3.add(Box.createRigidArea(new Dimension(20, 0)));
buttonPanel4.add(Box.createRigidArea(new Dimension(10, 0)));
buttonPanel5.add(Box.createRigidArea(new Dimension(-10, 0)));
textPanel.add(blackText);
textPanel.add(Box.createRigidArea(new Dimension(60, 0)));
textPanel2.add(blueText);
textPanel2.add(Box.createRigidArea(new Dimension(50, 0)));
textPanel3.add(greenText);
textPanel3.add(Box.createRigidArea(new Dimension(50, 0)));
textPanel4.add(yellowText);
textPanel4.add(Box.createRigidArea(new Dimension(50, 0)));
textPanel5.add(redText);
textPanel5.add(Box.createRigidArea(new Dimension(50, 0)));
panel.add(textPanel);
panel.add(textPanel2);
panel.add(textPanel3);
panel.add(textPanel4);
panel.add(textPanel5);
panel.add(ImagePanel);
panel.add(buttonPanel);
panel.add(buttonPanel2);
panel.add(buttonPanel3);
panel.add(buttonPanel4);
panel.add(buttonPanel5);
panel2.add(language);
c.add(panel);
c.add(panel2);
c.setLayout(new GridLayout(2,3));
setSize(600,500);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(language.getSelectedItem()=="French") {
res = ResourceBundle.getBundle("ProgramProperties", fr);
blackText.setText(res.getString("Black"));
blueText.setText(res.getString("Blue"));
greenText.setText(res.getString("Green"));
yellowText.setText(res.getString("Yellow"));
redText.setText(res.getString("Red"));
play.setText(res.getString("Play"));
play2.setText(res.getString("Play"));
play3.setText(res.getString("Play"));
play4.setText(res.getString("Play"));
play5.setText(res.getString("Play"));
}
else if(language.getSelectedItem()=="English") {
res = ResourceBundle.getBundle("ProgramProperties", en);
blackText.setText(res.getString("Black"));
blueText.setText(res.getString("Blue"));
greenText.setText(res.getString("Green"));
yellowText.setText(res.getString("Yellow"));
redText.setText(res.getString("Red"));
play.setText(res.getString("Play"));
play2.setText(res.getString("Play"));
play3.setText(res.getString("Play"));
play4.setText(res.getString("Play"));
play5.setText(res.getString("Play"));
}
else if(language.getSelectedItem()=="Swedish") {
res = ResourceBundle.getBundle("ProgramProperties", sv);
blackText.setText(res.getString("Black"));
blueText.setText(res.getString("Blue"));
greenText.setText(res.getString("Green"));
yellowText.setText(res.getString("Yellow"));
redText.setText(res.getString("Red"));
play.setText(res.getString("Play"));
play2.setText(res.getString("Play"));
play3.setText(res.getString("Play"));
play4.setText(res.getString("Play"));
play5.setText(res.getString("Play"));
}
else{}
}
public static void main(String[] args) {
new MainGUI();
}
}
我知道我的代码现在不是最有效的,我会尽快整理它。我感谢任何帮助,并将回答任何可以帮助您帮助我的问题!