2017-10-27 1 views
0

두 개의 클래스가 있습니다. 하나는 마법 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 클래스를 활성화하기 위해 "예"를 눌렀 으면합니다.

답변

0

어떤 오류가 발생 했습니까? "내부 클래스는 정적 선언을 가질 수 없습니다."

다른 접근법을 시도해 볼 수 있습니다. wav 플레이어 클래스를 중첩하는 대신 Magic8Ball 클래스 뒤에 if를 삽입하십시오. 두 클래스 모두 같은 파일에 존재할 수 있지만, 아는 바와 같이 그 중 하나만 공개 될 수 있습니다.

+0

좋아, 그래서, 난 그냥 공용 클래스 WavPlayer에서 단어 "대중"을 제거하는 것이 할 JFrame의 확장을? –

+0

사실 저는 마우스 오른쪽 버튼으로 클릭하여 결합하는 법을 배웠습니다. 하지만 지금이 프로그램을 실행하면 "어떤 클래스를 실행 하시겠습니까? 8ball 또는 Wavplayer"라고 표시됩니다. 내가 8ball이하기를 원하는 것은 사용자가 게임이 끝나면 "예"를 선택할 때 Wavplayer를 호출하는 것입니다. –

+0

JOptionPane.showConfirmDialog를 호출하면 (자), 밀린 버튼에 일치하는 정수가 돌려 주어집니다. 당신은 이런 식으로 시도 할 수도 있습니다 : – HankNessip

0

이제 두 클래스는이 같은 시도 할 수 있는지 :

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); 
} 
+0

그래서 내가 쓴대로 정확히 복사 및 붙여 넣기를하지 않습니다. 그래서 내가 단어 "가치"를 대체합니까 return 0 == 당신의 코드에 쓴 다음과 같은 내 코드에 있나요? –

관련 문제