2015-08-15 43 views
0

把這個JFrame窗體如何把這個JFrame窗體

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.URL; 
/** 
* 
* @author wooma 
*/ 
public class NewClass { 

    public static void main(String args[]){ 
    // converting 1000 Euro to US Dollar 
     System.out.println("Euro/US Dollar: " + findExchangeRateAndConvert("NGN", "USD", 150000)); 

    } 

    private static Double findExchangeRateAndConvert(String from, String to, int amount) { 
     try { 
      //Yahoo Finance API 
      URL url = new URL("http://finance.yahoo.com/d/quotes.csv?f=l1&s="+ from + to + "=X"); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); 
      String line = reader.readLine(); 
      if (line.length() > 0) { 
       return Double.parseDouble(line) * amount; 
      } 
      reader.close(); 
     } catch (Exception e) { 
      System.out.println(e.getMessage()); 
     } 
     return null; 
    } 
} 
+1

一方面,JFrame中不會在Android上工作... –

+0

@George西姆斯公司的NetBeans,u能幫助,它只是一個貨幣轉換器 – chopsticks

+0

@chopsticks爲什麼你把它標記爲Android? –

回答

0

我覺得我不應該這樣做,因爲SO不是一個代碼服務站點,但嘿!這是一個例子! (不太好找,改變了NGN到EUR)

import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.URL; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JTextField; 

public class Example { 

    public static void main(String args[]) { 

     JFrame frame = new JFrame(); 

     JTextField tf1 = new JTextField(10); 

     JTextField tf2 = new JTextField(10); 

     JButton b1 = new JButton("Convert to USD"); 
     b1.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       try { 
        tf2.setText("" + findExchangeRateAndConvert("EUR", "USD",Integer.parseInt(tf1.getText()))); 
       } catch(NumberFormatException nfe) { 

       } 
      } 
     }); 

     JButton b2 = new JButton("Convert to EUR"); 
     b2.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       try { 
        tf2.setText("" + findExchangeRateAndConvert("USD", "EUR",Integer.parseInt(tf1.getText()))); 
       } catch(NumberFormatException nfe) { 

       } 
      } 
     }); 

     frame.setLayout(new FlowLayout()); 
     frame.add(tf1); 
     frame.add(b1); 
     frame.add(b2); 
     frame.add(tf2); 
     frame.pack(); 
     frame.setVisible(true); 

    } 

    private static Double findExchangeRateAndConvert(String from, String to, 
      int amount) { 
     try { 
      // Yahoo Finance API 
      URL url = new URL("http://finance.yahoo.com/d/quotes.csv?f=l1&s=" 
        + from + to + "=X"); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        url.openStream())); 
      String line = reader.readLine(); 
      if (line.length() > 0) { 
       return Double.parseDouble(line) * amount; 
      } 
      reader.close(); 
     } catch (Exception e) { 
      System.out.println(e.getMessage()); 
     } 
     return null; 
    } 

} 
+0

的curreny轉換工作,但它只是工作只是美元和NGN,其他curreny對沒有工作。如果我想從NGN轉換成GHC,歐元轉換爲美元,XAF轉換爲GHC,那麼該怎麼辦?每次轉換需要一個deffrent api嗎? – chopsticks

0

這可以在Netbeans中可以非常容易地實現, 所有你需要的是拖放JLabel一個JTextFieldJButton和調用setText(findExchangeRateAndConvert("NGN", "USD", 150000))。點擊該按鈕以獲取一種貨幣的文本,並設置JLabel的結果。
或者

public class NewClass{ 
private JFrame frame; //MainFrame 

//Text fields to take input 
private JTextField euro; 
private JTextField dollar; 

//Buttons to convert 
private JButton toUSD; 
private JButton toEuro; 

private JLabel euroLabel; 
private JLabel dollarLabel; 

public static void main(String[]args){ 
    frame = new JFrame("Currency Conversion"); 
    //init method call 
    init(); 

    toUSD.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      try { 
       dollar.setText(euro.getText().toString()+" -> " + findExchangeRateAndConvert("EUR", "USD",Integer.parseInt(euro.getText().toString()))); 
      } catch(Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 

    toEuro.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      try { 
       euro.setText(dollar.getText().toString()+" -> " + findExchangeRateAndConvert("USD", "EUR",Integer.parseInt(dollar.getText().toString()))); 
      } catch(Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 



} 

/** 
*init method 
**/ 
private void init(){ 

    euro = new JTextField(30); 
    dollar = new JTextField(30); 

    euroLabel = new JLabel(10); 
    dollarLabel = new JLabel(10); 

    toUSD = new JButton("To USD"); 
    toEuro = new JButton("To Euro"); 
} 

/** 
*Method to set the layout and show the Frame 
**/ 
private void showFrame(){ 
    //Set the layout of the JFrame 
    frame.setLayout(new FlowLayout()); 
    frame.setBounds(50, 120, 200, 20); 

    //Add the elements 
    frame.add(euro); 
    frame.add(toUSD); 
    frame.add(dollar); 
    frame.add(toEuro); 

    //Show the JFrame 
    frame.pack(); 
    frame.setVisible(true); 

} 
/** 
*your findExchangeRateAndConvert method 
**/ 
private static Double findExchangeRateAndConvert(String from, String to, int amount) { 
    try { 
     //Yahoo Finance API 
     URL url = new URL("http://finance.yahoo.com/d/quotes.csv?f=l1&s="+ from + to + "=X"); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); 
     String line = reader.readLine(); 
     if (line.length() > 0) { 
      return Double.parseDouble(line) * amount; 
     } 
     reader.close(); 
    } catch (Exception e) { 
     System.out.println(e.getMessage()); 
    } 
    return null; 
} 

}

+0

@LuxxMiner感謝它的工作原理,但如果我想CNY添加更多的貨幣波紋管這一塊,像英鎊瑞士法郎,並再次吼叫,以日元,略低於什麼。是否可能我想知道如何把所有的貨幣放入JScrollPane。謝謝你們,你們也拯救了我的世界 – chopsticks

+0

。我應該如何添加其他貨幣波紋管,疊加它們,我應該垂直說。 – chopsticks

+0

@chopsticks如果你在netbeans上這樣做,它可以非常容易,使用Netbeans SWING Palette元素可以進行大量的自定義,檢查[this](https://www.youtube.com/watch?v=JZTIIchUa6E) –