- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个单独的类(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/
编辑 感谢您的所有评论和回答。我很困惑,在尝试不同的答案后我能够解决问题,但现在无法重现问题???我不知道发生了什么事。 我想做的是通过 id 获取一个元素,但是这个 id 中包含多个点,因为 id
我正在尝试对 bool 字段进行是/否选择。默认小部件是复选框输入。但是,如果我使用 Select 覆盖默认小部件,则会得到:NameError: Select is not defined 我认为这
我有一个背景图像,所有 View 中的图像都相同,但导航栏 traslucent 默认设置为 YES 并且背景不均匀。我尝试了各种解决方案,但没有任何改变:( 我的 AppDelegate.h #im
所以我有一个要分析的数据框。问题是数据框中有一堆 1 和 0(1 是,0 是否),而不是是/否。如何修改数据框以使其不是 1 和 0,而是 Yes 和 No,以便我可以使用逻辑回归?我确信有一个我没有
我是 XML/XSL 的新手(比如 2 天新)。我有一行我正在做一个 xsl:value-of 选择,它返回一个 True/False 属性。我想让它显示是/否,但我试图这样做没有成功。下面是我目前的
我正在尝试写一个简单的 do..until循环,它不起作用: $yesNo = Read-Host -Prompt 'Do you want to add alternative DNS names
这个问题已经有答案了: How to use this boolean in an if statement? (8 个回答) 已关闭 7 年前。 import java.util.Scanner;
提示问题生成器 class SynthesisPromptGenerator: def wait_key(self): ''' Wait for a key press on
我想自动给出输入是/否,而不是通过键盘/用户输入。 user_choice = raw_input("Are you sure want to remove all item (y/n):\n") i
我需要在文件存在时向用户显示一些消息,显示消息“文件存在...你想覆盖它吗??” if (File.Exists(binaryFilePath)) { Program.DisplayMessa
我在一个表格中有 5 个问题,其中一些的回答是"is"/“否”。一旦用户选择其中一个按钮,我希望能够切换是和否按钮。我遇到的问题是,如果其中一个问题得到了回答,并且我回答了下一个问题,它会从所有其他问
如何使用 bash 脚本在文本文件的某行/之后搜索字符串? 例如我想搜索第一次出现的 "version:" 字符串,但不是在文件开头而是在第 1 行。说 35 其中包含文本 *-disk:0 这样我就
目前我正在创建一个 Alexa 技能来处理 FAQ 问题和导致直接回复的问题,但我对 alexa 何时需要另一个回复有疑问。 例如,如果用户询问, “我应该吃什么” Alexa 会回复“你想吃意大利面
如果用户反复按下后退按钮,我需要一种方法来检测他们何时处于我的任务/应用程序的最后一个 Activity 并显示“你想退出吗?”在他们返回主屏幕或他们之前运行的任何应用程序之前的对话框。 Hook o
我有一个充满数据的表格,在行的末尾我有一个删除选项。 与其链接到一个全新的页面来确认删除,不如将“删除”链接拆分为一个是/否链接,根据单击的内容,删除行或变回“删除”,这样会很整洁。我知道 reddi
使用一些批处理文件,我想在 Java Keystore 中添加不受信任的自签名证书。 命令是 %JAVA_HOME%/bin/keytool -import -v -trustcacerts -ali
我有一个充满 IP 地址的电子表格(“IP LIST”)。在该工作簿中,我有第二个电子表格代表 IP 地址部分,该部分属于 IP 地址的较大部分中的打印机(“打印机”)。我已经在“IP LIST”上确
一旦用户单击"is"并在输入框中输入一个值,但改变主意并单击“否”选项,但输入值仍然存在,如何删除该值 Do you offer rented call center seating on
因此,我尝试验证用户是否输入了"is"或“否”,并继续询问,直到他们输入其中之一。这是我到目前为止的代码。 System.out.println("Would you like a Diamond i
我目前正在关注 Erik Roberts 所著的《Java 的艺术与科学》一书。当在代码中测试是/否/替代答案时,我会因为没有答案而被问两次。 /* * File: YesNoQuestion.ja
我是一名优秀的程序员,十分优秀!