2017-04-20 1 views
0

이 곳을 처음 방문했지만 저는 StackOverflow에서 찾은 모든 것을 좋아했습니다. 지금 나는 자바 클래스에서 최종 프로젝트를 위해 만든 오래된 프로그램을 빌드하려고 노력하고 있지만, 슬프게도 나는로드 덤프를 쳤다.입력이있는 JFrame 삽입 및 출력 표시

내 프로그램이 사용자의 온도를 요청한 다음 화씨 또는 섭씨인지 묻습니다. 그런 다음 화씨와 섭씨 모두를 계산하고 뜨겁거나 추운 날씨, 가벼운 날씨 등의 여부를 사용자에게 알려줍니다.

내가 지금하고 싶은 것은 달리고 실행할 수있게하는 것입니다. 이것은 Visual Basic과 같은 팝업 창에 표시됩니다. 가능한가?

package temperaturereader; 
import javax.swing.JFrame; 
import javax.swing.WindowConstants; 
import java.util.Scanner; 
/** 
* 
* @author pcstudent 
*/ 

public class TemperatureReader { 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 
    //Declare your Variables 
    double input = 0.0; 
    double Fahrenheit= 0.0; 
    double Celsius= 0.0; 
    String ForC; 
    String Hot = "It is hot today."; 
    String Cold = "It is cold today."; 
    String Cool = "It is cool today."; 
    String Warm = "It is warm today."; 
    String Freezing = "It is freezing today. Take a jacket."; 
    String TooHot = "It is dangerously hot. Stay hydrated."; 
    String SunHot = "You are currently standing on the sun."; 
    String Oymyakon = "You are in Oymyakon, Russia."; 


    //Asking for Temperature 
    Scanner one = new Scanner(System.in); 
    System.out.println("Enter the temperature:"); 
    input = one.nextDouble(); 

    //Asking for Fahrenheit or Celsius 
    Scanner two = new Scanner(System.in); 
    System.out.println("Is the temperature in Fahrenheit or Celsius?"); 
    ForC = two.next(); 


    //Check for Fahrenheit and convert accordingly 
    if (ForC.equalsIgnoreCase("Fahrenheit")) { 
     Fahrenheit = input; 
     Celsius = (Fahrenheit -32)*.5556; 
    } 
    else if (ForC.equalsIgnoreCase("F")) { 
     Fahrenheit = input; 
     Celsius = (Fahrenheit -32)*.5556; 
    } 
    //Check for Celsius and convert accordingly 
    else if (ForC.equalsIgnoreCase("Celsius")) { 
     Celsius = input; 
     Fahrenheit = (Celsius*1.8) + 32; 
    } 
    else if (ForC.equalsIgnoreCase("C")){ 
     Celsius = input; 
     Fahrenheit = (Celsius*1.8) + 32; 
    } 

    //If there is an error, stop the program. 
    else { 
     System.out.println("Error."); 
      System.exit(0); 
    } 

    System.out.println(""); 


    //Compare temperatures and display. 

    if (Fahrenheit >= 9941) { 
     System.out.println("The temperature is currently " + Fahrenheit + " degrees Fahrenheit and " + Celsius + " degrees Celsius."); 
     System.out.println(""); 
     System.out.println(SunHot); 
    } 

    else if (Fahrenheit >= 105) { 
     System.out.println("The temperature is currently " + Fahrenheit + " degrees Fahrenheit and " + Celsius + " degrees Celsius."); 
     System.out.println(""); 
     System.out.println(TooHot); 
    } 

    else if (Fahrenheit >= 80) { 
     System.out.println("The temperature is currently " + Fahrenheit + " degrees Fahrenheit and " + Celsius + " degrees Celcius."); 
     System.out.println(""); 
     System.out.println(Hot); 
    } 
    else if (Fahrenheit >= 60) { 
     System.out.println("The temperature is currently " + Fahrenheit + " degrees Fahrenheit and " + Celsius + " degrees Celsius."); 
     System.out.println(""); 
     System.out.println(Warm);    
    } 
    else if (Fahrenheit >= 59) { 
     System.out.println("The temperature is currently " + Fahrenheit + " degrees Fahrenheit and " + Celsius + " degrees Celsius."); 
     System.out.println(""); 
     System.out.println(Cool);  
    } 
    else if (Fahrenheit >= 40) { 
     System.out.println("The temperature is currently " + Fahrenheit + " degrees Fahrenheit and " + Celsius + " degrees Celsius."); 
     System.out.println(""); 
     System.out.println(Cold);  
    } 

    else if (Fahrenheit >= 32) { 
     System.out.println("The temperature is currently " + Fahrenheit + " degrees Fahrenheit and " + Celsius + " degrees Celsius."); 
     System.out.println(""); 
     System.out.println(Freezing); 
    } 

    else if (Fahrenheit >= -58) { 
     System.out.println("The temperature is currently " + Fahrenheit + " degrees Fahrenheit and " + Celsius + " degrees Celsius."); 
     System.out.println(""); 
     System.out.println(Oymyakon); 
    } 
    else { 
     System.out.println(""); 
    } 

} 
+0

는 당신이 얻을 문제입니다 ? – Yahya

+0

가능합니까? 예. 웹에서 사용할 수있는 모든 종류의 자습서가 있습니다. StackOverflow는 "이봐, 나에게 이걸하는 법을 말해줘"가 아니라, 네가 뭘 시도했는지 보여주고 네가 갇혀있는 곳을 설명하기위한거야. 자세한 지침은 http://stackoverflow.com/help/how-to-ask를 참조하십시오. – betseyb

+0

시작하려면 SO는 튜토리얼 사이트가 아니므로 시작하기 위해 스스로 노력해야합니다. [JFC/Swing으로 GUI 생성하기] (http://docs.oracle.com/javase/tutorial/uiswing/) 및 [프레임 작성 방법 (메인 윈도우)] (https://docs.oracle) .com/javase/tutorial/uiswing/components/frame.html). [JavaFX 시작하기] (http://docs.oracle.com/javafx/2/get_started/jfxpub-get_started.htm)도보실 수 있습니다. – MadProgrammer

답변

-1

그래서 사용자에게 결과를 표시하기위한 새 창을 만들어야합니까?

(난 당신의 코드를 최적화하려고) 새로운 JFrame의 클래스를 만들고 그 클래스의 새로운 객체를 생성하고 objName.show(true)

+0

좋아요,하지만 OP가 어떻게 프레임을 만드는지, 어떻게합니까? OP는 프레임에 컨트롤을 추가합니다. OP가 컨트롤과 상호 작용하고 이벤트를 관리하는 방법은 무엇입니까? 질문은 너무 넓어서 답은 얼마나 나쁜지를 반영합니다. – MadProgrammer

1
당신은 여기에서 시작할 수 있습니다

하여 볼 수 있도록 :

import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JTextField; 

public class TemperatureReader { 
    private double Fahrenheit= 0.0; 
    private double Celsius= 0.0; 
    private String ForC; 
    private final String Hot = "It is hot today.", Cold = "It is cold today.", Cool = "It is cool today.",Warm = "It is warm today.", 
         Freezing = "It is freezing today. Take a jacket.",TooHot = "It is dangerously hot. Stay hydrated.", 
         SunHot = "You are currently standing on the sun.",Oymyakon = "You are in Oymyakon, Russia."; 

public static void main(String[] args) { 
    TemperatureReader tr = new TemperatureReader(); 
    // a very simple JFrame , it can be improved but this is just an example 
    JFrame jf = new JFrame("Temperature Reader"); 
    jf.setSize(700, 400); // you can change this 
    jf.getContentPane().setLayout(new FlowLayout()); 
    JTextField t = new JTextField("", 12); 
    String []types = {"Fahrenheit", "Celsius"}; 
    t.setHorizontalAlignment(JTextField.LEFT); 
    JComboBox<String> type = new JComboBox<String>(types); 
    type.setSelectedIndex(0); 
    jf.getContentPane().add(t); 
    JTextField result = new JTextField("", 60); 
    result.setHorizontalAlignment(JTextField.RIGHT); 
    JButton btn = new JButton("Calculate"); 
    jf.getContentPane().add(t); 
    jf.getContentPane().add(type); 
    jf.getContentPane().add(result); 
    jf.getContentPane().add(btn); 
    jf.setVisible(true); 

    btn.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent arg0) { 
      double input = 0.0; 
      // you need to validate it first and check if it's not empty and contains only digits ..etc 
      input = Double.valueOf(t.getText()); 
      tr.setForC(type.getSelectedItem().toString()); 

      switch(tr.getForC()){ 
      case "Fahrenheit": 
       tr.setCelsius((input -32)*.5556); 
       tr.setFahrenheit(input); 
       break; 

      case "Celsius": 
       tr.setFahrenheit((input*1.8) + 32); 
       tr.setCelsius(input); 
       break; 
     } 

      double f = tr.getFahrenheit(); 
      double c = tr.getCelsius(); 
      String display = (f>=9941)? tr.result(f, c, tr.getSunHot()): 
          (f>= 105)? tr.result(f, c, tr.getTooHot()): 
          (f>= 80)? tr.result(f, c, tr.getHot()): 
          (f>= 60)? tr.result(f, c, tr.getWarm()): 
          (f>= 59)? tr.result(f, c, tr.getCool()): 
          (f>= 40)? tr.result(f, c, tr.getCold()): 
          (f>= 32)? tr.result(f, c, tr.getFreezing()): 
          (f>= -58)? tr.result(f, c, tr.getOymyakon()): "Error"; 


       result.setText(display); 
     } 
    }); 
    } 


    public String result(double f, double c, String s){ 
     return "The temperature is currently " + f + " degrees Fahrenheit and " + c + " degrees Celsius.\n" + s + "\n"; 
    } 

    public double getFahrenheit() { 
     return Fahrenheit; 
    } 

    public void setFahrenheit(double fahrenheit) { 
     Fahrenheit = fahrenheit; 
    } 

    public double getCelsius() { 
     return Celsius; 
    } 

    public void setCelsius(double celsius) { 
     Celsius = celsius; 
    } 

    public String getForC() { 
     return ForC; 
    } 

    public void setForC(String forC) { 
     ForC = forC; 
    } 

    public String getHot() { 
     return Hot; 
    } 

    public String getCold() { 
     return Cold; 
    } 

    public String getCool() { 
     return Cool; 
    } 

    public String getWarm() { 
     return Warm; 
    } 

    public String getFreezing() { 
     return Freezing; 
    } 

    public String getTooHot() { 
     return TooHot; 
    } 

    public String getSunHot() { 
     return SunHot; 
    } 

    public String getOymyakon() { 
     return Oymyakon; 
    } 

}