Java 如何根据“是”或“否”按钮/作业窗格组合或调用另一个类
我有两节课。一个是魔法8球,另一个是播放星际歌曲的Wav播放器Java 如何根据“是”或“否”按钮/作业窗格组合或调用另一个类,java,button,call,inner-classes,Java,Button,Call,Inner Classes,我有两节课。一个是魔法8球,另一个是播放星际歌曲的Wav播放器 import java.security.SecureRandom; import javax.swing.JOptionPane; import javax.swing.ImageIcon; public class Magic8Ball { private final static ImageIcon image = new ImageIcon("images/BuckminsterFuller.jpg"); priva
import java.security.SecureRandom;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
public class Magic8Ball {
private final static ImageIcon image = new ImageIcon("images/BuckminsterFuller.jpg");
private final static SecureRandom rand = new SecureRandom();
private final static String wisdom[] = {
"Not Synergistically feasible",
"It is geometrically analytical",
"Your question does not follow General Semantics, hazy, try again",
"Yes - Sustainable",
"No, energetically inefficient",
"Maybe a Dymaxion process. Technology not up to date.",
"Your question is negatively Entropic.",
"Everyone is born a genius, but the process of living de-geniuses them.",
"Humanity is acquiring all the right technology for all the wrong reasons.",
"We are called to be architects of the future, not its victims",};
public static void main(String[] args) {
boolean askQ = true;
while (askQ) {
String question = getUserQ();
String randomWisdom = getRandomWisdom();
showWisdom(question, randomWisdom);
askQ = userWantsToAskAnotherQ();
}
}
private static String getUserQ() {
return JOptionPane.showInputDialog(null,
"Ask a question that has to do with the structural integrity of earth:",
"Only Engineers, Scientists and Architects allowed",
JOptionPane.INFORMATION_MESSAGE);
}
private static String getRandomWisdom() {
return wisdom[rand.nextInt(wisdom.length)];
}
private static void showWisdom(String question, String randomWisdom) {
JOptionPane.showMessageDialog(null, question + "\n" + randomWisdom,
"Buckminster's Magic-8 Ball has responded.",
JOptionPane.PLAIN_MESSAGE, image);
}
private static boolean userWantsToAskAnotherQ() {
return 0 == JOptionPane.showConfirmDialog(null, "Abort Mission or Dock "
+ "while Hearing" + "\n" + "No Time for Caution? by Hans Zimmer",
"Would you like to stay on spaceship earth or abandon ship?",
JOptionPane.YES_NO_OPTION, 0, image);
}
}
因此,正如你在课程结束时看到的,你可以选择中止任务或停靠,同时听到Hans Zimmer没有时间提醒你。因此,如果用户点击Yes,我如何激活它或调用我为《星际之歌》设置的这个类:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.*;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
public class WavPlayer extends JFrame {
JButton btn = new JButton("Play No Time For Caution");
File wavFile;
URL defaultSound;
public static Clip clip;
public static AudioInputStream audioInputStream;
public WavPlayer(String url) {
try {
setSize(300, 100);
setLocation(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel jp = new JPanel();
defaultSound = new URL (url);
jp.add(btn);
getContentPane().add(jp);
pack();
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
play();
}
});
} catch (MalformedURLException ex) {
Logger.getLogger(WavPlayer.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void play() {
try {
audioInputStream = AudioSystem.getAudioInputStream(defaultSound);
try {
clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.loop(20000);
clip.start();
} catch (LineUnavailableException e) {
}
} catch (UnsupportedAudioFileException | IOException e) {
}
}
public void stop() {
clip.stop();
}
public static void main(String args[]) {
WavPlayer t = new WavPlayer("file:C:/Users/borjax01/Desktop/Netbeans/JavaApplication/music/Interstellar.wav");
t.setVisible(true);
}
}
编辑:我已经通过重构并将一个类移动到另一个类中来组合这两个类。我想做的是,我想让8ball类在8ball类结束时调用WavPlayer类,当提示用户再次播放时,他们点击“是”以激活WavPlayer类…..您遇到了什么错误?“内部类不能有静态声明”也许
你可以尝试另一种方法。在Magic8Ball类之后插入if,而不是嵌套wav player类。这两个类可以存在于同一个文件中,但正如您所知,其中只有一个可能是公共的。您遇到了什么错误?“内部类不能有静态声明”也许
你可以尝试另一种方法。在Magic8Ball类之后插入if,而不是嵌套wav player类。这两个类可以存在于同一个文件中,但正如您所知,其中只有一个可能是公共的。既然两个类都存在,您可以尝试以下方法:
value = JOptionPane.showConfirmDialog(...)
if (value == JOptionPane.YES_OPTION) {
WavPlayer t = new WavPlayer("file:C:/Users/borjax01/Desktop/Netbeans/JavaApplication/music/Interstellar.wav");
t.setVisible(true);
}
现在这两个类都出现了,您可以尝试以下方法:
value = JOptionPane.showConfirmDialog(...)
if (value == JOptionPane.YES_OPTION) {
WavPlayer t = new WavPlayer("file:C:/Users/borjax01/Desktop/Netbeans/JavaApplication/music/Interstellar.wav");
t.setVisible(true);
}
好的,为了做到这一点,我只是从公共类中删除了单词“public”?实际上我刚刚学会了如何通过右键单击,将其移动到类中来组合。但当我现在运行它时,它只会说“你想运行哪个类?8ball还是Wavplayer”。我想8ball做的是当用户在游戏结束时选择“是”时调用Wavplayer。调用JOptionPane.showConfirmDialog返回一个与按下的按钮匹配的整数。你可以这样做:好的,要做到这一点,我只需从公共类WavPlayer extends JFrame中删除单词“public”?实际上,我刚刚学会了如何通过右键单击,将其移动到类中来组合。但当我现在运行它时,它只会说“你想运行哪个类?8ball还是Wavplayer”。我想8ball做的是当用户在游戏结束时选择“是”时调用Wavplayer。调用JOptionPane.showConfirmDialog返回一个与按下的按钮匹配的整数。你可以尝试这样的方法:所以我知道我不会像你写的那样复制和粘贴它。那么,我是否要用代码中的内容替换“value”一词,比如return 0==后跟您在代码中编写的内容?因此,我知道我不会像您编写的那样复制和粘贴它。那么,我是否要用代码中的内容替换“value”一词,比如return 0==后跟您在代码中编写的内容?