以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;
}
}
答案 0 :(得分:0)
我觉得我不应该这样做,因为SO不是代码服务网站,但是嘿!这是一个例子! (看起来不太好,并将NGN改为欧元)
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;
}
}
答案 1 :(得分:0)
这可以很容易地在Netbeans中实现,
您只需拖放JLabel
一个JTextField
和一个JButton
即可拨打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;
}
}