2017-12-04 2 views
1

이 프로그램에 도움이 필요합니다. 내가하는 모든 일이 효과가없는 것처럼 보입니다. 오류가없는 경우에도 똑같은 것을 보여줍니다. Cheg에있는 사람들이 코드 수정 코드를 보내 왔지만 여전히 작동하지 않았습니다.누군가이 콤보 상자를 올바르게 할 수 있습니까? 나는 매우 분실했습니다

귀하는 전 세계적으로 여행을 떠났으 며 다른 나라의 비용이 얼마나 들지 알아내는 프로그램이 필요합니다. 유럽, 일본, 오스트레일리아, 인도 및 멕시코의 여러 국가에서 캐나다를 방문 할 계획이므로 해당 국가의 통화로 프로그램이 작동해야합니다. CurrencyConverter.java 및 RatePanel.java 파일에는이 변환을 수행하는 프로그램의 골격이 들어 있습니다. 다음과 같이 완료하십시오. CurrencyPanel에는 현재 두 개의 구성 요소 만 포함됩니다. 제목의 JLabel과 계산 결과를 표시하는 JLabel입니다. 또한 두 개의 병렬 배열을 포함합니다. 하나는 통화 이름 배열 (문자열 배열)이고 다른 하나는 해당 환율 배열입니다 (통화 단위를 미국 달러로 표시). 프로그램을 컴파일하고 실행하여 어떻게 보이는지 확인하십시오 (많지는 않습니다 !!). 사용자가 통화를 선택할 수 있도록 콤보 상자를 추가하십시오. 생성자에 대한 인수는 통화 이름 배열이어야합니다. 첫 번째 통화 이름은 실제로 사용자에 대한 지침이므로 추가 레이블이 필요하지 않습니다. 인덱스가 선택된 항목의 인덱스로 설정되도록 ComboListener에서 actionPerformed를 수정하십시오. 당신이 지금까지 가지고있는 것을 시험하십시오. 주어진 통화의 한 단위가 미국 달러로 표시되어야합니다. 사용자가 선택한 통화로 항목 비용을 입력 할 수 있도록 텍스트 필드 (및 레이블)를 추가하십시오. ComboListener를 업데이트하여 텍스트 필드에서이 값을 가져 와서 해당 금액을 계산하여 달러로 표시해야합니다. 프로그램을 테스트하십시오. 레이아웃을 수정하여보다 매력적인 GUI를 만들 수 있습니다 (제안 : 상자 레이아웃 사용).

// ****************************************************************** 
    // RatePanel.java 
    // 
    // Panel for a program that converts different currencies to 
    // U.S. Dollars 
    // ****************************************************************** 

    import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.*; 

    public class RatePanel extends JPanel 
    { 
     private JComboBox currencyCombo; 

     private double[] rate;   // exchange rates 
     private String[] currencyName; 
     private JLabel result; 

     // ------------------------------------------------------------ 
     // Sets up a panel to convert cost from one of 6 currencies 
     // into U.S. Dollars. The panel contains a heading, a text 
     // field for the cost of the item, a combo box for selecting 
     // the currency, and a label to display the result. 
     // ------------------------------------------------------------ 
     public RatePanel() 
     { 
      JLabel title = new JLabel ("How much is that in dollars?"); 
      title.setAlignmentX (Component.CENTER_ALIGNMENT); 
      title.setFont (new Font ("Helvetica", Font.BOLD, 20)); 

      // Set up the arrays for the currency conversions 
      currencyName = new String[] {"Select the currency..", 
             "European Euro", "Canadian Dollar", 
             "Japanese Yen", "Australian Dollar", 
             "Indian Rupee", "Mexican Peso"}; 
      rate = new double [] {0.0, 0.948968, 0.646920, 
            0.00803616, 0.562082, 
            0.0204441, 0.103735}; 


      result = new JLabel (" U.S. Dollars ????? "); 


      add (title); 

      add (result); 

     } 


     // ****************************************************** 
     // Represents an action listener for the combo box. 
     // ****************************************************** 
     private class ComboListener implements ActionListener 
     { 
      // -------------------------------------------------- 
      // Determines which currency has been selected and 
      // the value in that currency then computes and 
      // displays the value in U.S. Dollars. 
      // -------------------------------------------------- 
      public void actionPerformed (ActionEvent event) 
      { 
       int index = 0; 
       result.setText ("1 " + currencyName[index] + 
           " = " + rate[index] + " U.S. Dollars"); 
      } 
     } 
    } 

메인 프로그램

 // ***************************************************************** 
     // File name: CurrencyConverter.java 
     // Name: 
     // Purpose: Computes the dollar value of the cost of an item in 
     // another currency 
     // ***************************************************************** 

    import java.awt.*; 
    import javax.swing.*; 
    public class CurrencyConverter 
    { 
     public static void main(String[] args) 
    { 
     JFrame frame = new JFrame("Currency Converter"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     RatePanel ratePanel = new RatePanel(); 
     frame.getContentPane().add(ratePanel); 
     frame.pack(); 
     frame.setVisible(true); 

    } 
    } 
+1

힌트를 기반으로한다. – Berger

+0

그래서 나는 새로운 j 콤보 상자를 만들었고 currencyName을 메서드의 매개 변수로 설정 했습니까? –

답변

0

다만 jComboBox에서 getSelectedIndex에 기초 rate 배열에서 해당 값을 얻는다.

구내 :

  • 당신은 사용자가 양이
  • 당신은 통화의 드롭 다운 메뉴가 필요한 입력하는 jTextField
  • 당신은 사용자가
  • 을 클릭 할 수있는 버튼이 있습니다 결과가 표시되는 다른 jTextField이 있습니다.
당신의 버튼의 ActionPerformed 방법 일단

rate 배열을 볼 수 있습니다

double[] rate = new double[]{0.948968, 0.646920, 
      0.562082, 0.0204441}; 

그런 다음 계산과 비슷하게 수행

double result = Double.parseDouble(jTextField2.getText()) * rate[jComboBox1.getSelectedIndex()]; 

그리고 (나는 편의를 위해 2 소수 자릿수로 반올림)에의하여 인쇄 jTextField :

jTextField1.setText("Result: "+(String.format("%.2f", result))); 

입력 :

55.23

출력 : *

Dropdown1 - 52.41

Dropdown2 - 35.73

Dropdown3 - 31.04

`JComboBox`가 getSelectedIndex '()'방법이있다 : -3210

Dropdown4 1.13

*이 단지 예에 rate 배열의 값

+0

답장을 보내 주셔서 감사합니다. 그러나이를 내 코드에 통합하면 호환되지 않는 형식 오류가 발생합니다. –

+0

* 정확한 * 오류와 그 원인을 알 수 있습니까? 스레드 "주요"java.lang.NumberFormatException의에서 – notyou

+0

예외 : 빈 문자열 \t sun.misc.FloatingDecimal.readJavaFormatString (FloatingDecimal.java:992)에서 java.lang.Double.parseDouble (Double.java:510)에서 \t RatePanel에서 \t (RatePanel.java:52) \t at CurrencyConverter.main (CurrencyConverter.java:16) –

관련 문제