두 개의 클래스가 있습니다. 하나는 마법 8 공이고, 다른 하나는 성간 노래를 연주하는 Wav 플레이어입니다. 당신은 클래스의 끝에서 볼 수Yes 또는 No 버튼/Joptionpane에 따라 다른 클래스를 결합하거나 호출하는 방법
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)에게 더 시간을 듣고 없을 때 임무 또는 독을 중단하는 것입니다. 사용자가 예 안타 그래서, 내가 그것을 활성화하는 방법을 가질 수 있습니다 또는이 내가 성간 노래가 클래스 호출
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 클래스가 WavPlayer 클래스를 호출하고 8ball 클래스가 끝날 때 사용자가 다시 재생할 것인지 묻는 메시지를 표시하고 wavplayer 클래스를 활성화하기 위해 "예"를 눌렀 으면합니다.
좋아, 그래서, 난 그냥 공용 클래스 WavPlayer에서 단어 "대중"을 제거하는 것이 할 JFrame의 확장을? –
사실 저는 마우스 오른쪽 버튼으로 클릭하여 결합하는 법을 배웠습니다. 하지만 지금이 프로그램을 실행하면 "어떤 클래스를 실행 하시겠습니까? 8ball 또는 Wavplayer"라고 표시됩니다. 내가 8ball이하기를 원하는 것은 사용자가 게임이 끝나면 "예"를 선택할 때 Wavplayer를 호출하는 것입니다. –
JOptionPane.showConfirmDialog를 호출하면 (자), 밀린 버튼에 일치하는 정수가 돌려 주어집니다. 당신은 이런 식으로 시도 할 수도 있습니다 : – HankNessip