我正在尝试制作一个程序来调用我的第二个Java类,它由背景播放的音乐和一个JFrame组成。 第一个Jframe有一个PLAY按钮。 我如何完全调用另一个Java类(使用try and catch,JFrame等)并关闭我的第一个Java类?
这是我的第一个Java类的输出
这是我第一节Java课的代码。
public class Instruction extends JFrame implements ActionListener {
void introSound(String musicLocation)
{
try {
File musicPath = new File (musicLocation);
if (musicPath.exists()){
AudioInputStream audioInput = AudioSystem.getAudioInputStream(musicPath);
Clip clip = AudioSystem.getClip();
clip.open(audioInput);
clip.start();
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
else {
System.out.println("Can't find file");
}
}
catch (Exception ex){
ex.printStackTrace();
}
}
//JButton
JButton goNextFrameBtn = new JButton("PLAY");
JFrame insFrame = new JFrame();
Instruction() {prepGUI();}
public void prepGUI(){
insFrame.setTitle("The Phineas and Ferb MATH QUIZ");
insFrame.setSize(1050, 670);
insFrame.setBackground(Color.RED);
insFrame.getContentPane().setLayout(null);
insFrame.setContentPane(new JLabel(new ImageIcon("BG1.JPG")));
insFrame.setResizable(false);
insFrame.setLocationRelativeTo(null);
insFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
insFrame.setVisible(true);
//adding JButton
goNextFrameBtn.setFont(new Font("Ferbtastic", Font.BOLD, 50));
goNextFrameBtn.setBounds(850,520,150,80);
insFrame.add(goNextFrameBtn);
goNextFrameBtn.addActionListener(this);
}
public void actionPerformed (ActionEvent e){
if (e.getSource() == goNextFrameBtn) {
insFrame.dispose();
new PnFMathQuiz();
}
}
public static void main(String[] args) {
String filepath = "PnF Theme Song.wav";
Instruction musicObject = new Instruction();
musicObject.introSound(filepath);
} }
这是我的第二个Java类的代码,我想通过点击PLAY按钮打开它
public class PnFMathQuiz extends JFrame{
void playSound(String musicLocation)
{
try {
File musicPath = new File (musicLocation);
if (musicPath.exists()){
AudioInputStream audioInput = AudioSystem.getAudioInputStream(musicPath);
Clip clip = AudioSystem.getClip();
clip.open(audioInput);
clip.start();
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
else {
System.out.println("Can't find file");
}
}
catch (Exception ex){
ex.printStackTrace();
}
}
JFrame frame = new JFrame();
PnFMathQuiz() {
prepareGUI();
}
public void prepareGUI() {
frame.setTitle("The Phineas and Ferb MATH QUIZ");
frame.setSize(1500, 1000);
frame.getContentPane().setLayout(null);
frame.setContentPane(new JLabel(new ImageIcon("BG2.JPG")));
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
public static void main(String[] args) {
String filepath = "Quirky Worky Song.wav";
PnFMathQuiz musicObject = new PnFMathQuiz();
musicObject.playSound(filepath);
} }
我希望你能帮我做这件事,谢谢你。
您可以先显示第二个JFrame,然后释放当前的JFrame。 我从第二个JFrame的prepareGUI中删除了setVisible(true)。 设置下一个JFrame的可见性最好是第一个JFrame的责任。
public void actionPerformed (ActionEvent e){
if (e.getSource() == goNextFrameBtn) {
JFrame pnMathQuize = new PnFMathQuiz();
pnMathQuize.setVisible(true);
insFrame.dispose();
}
}
并将您在第二个JFrame的main方法中所做的事情移动到构造函数,如下所示
PnFMathQuiz() {
prepareGUI();
String filepath = "Quirky Worky Song.wav";
this.playSound(filepath);
}
public void prepareGUI() {
frame.setTitle("The Phineas and Ferb MATH QUIZ");
frame.setSize(1500, 1000);
frame.getContentPane().setLayout(null);
frame.setContentPane(new JLabel(new ImageIcon("BG2.JPG")));
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}