我有两个单独的类(class)。一个是神奇的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");
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);
}
}
因此,正如您在类(class)结束时所看到的,当听到 Hans Zimmer 没有时间谨慎时,选项是中止任务或停靠。因此,如果用户点击"is",我怎样才能激活它或调用我为星际歌曲提供的这个类:
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 类,当提示用户再次玩时,他们点击"is"来激活 wavplayer 类.... .
最佳答案
你遇到了什么错误?也许“内部类不能有静态声明”?
您可以尝试另一种方法。不要嵌套 wav 播放器类,而是在 Magic8Ball 类后面插入 if。这两个类可以存在于同一个文件中,但如您所知,只有其中一个类可能是公共(public)的。
关于java - 如何根据"is"或“否”按钮/选项 Pane 组合或调用另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46982877/