简体   繁体   English

如何将其以jframe形式

[英]how to put this in jframe form

put this in JFrame form 把它放在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;
    }
}

I feel like I shouldn't do this, because SO is not a code-service site, but hey! 我觉得我不应该这样做,因为SO不是代码服务站点,但是! Here is an example! 这是一个例子! (Not very good looking and changed NGN to EUR) (外观不太好,将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;
    }

}

This can be very easily implemented in Netbeans, All you need is to drag and drop a JLabel a JTextField and a JButton and you call the setText(findExchangeRateAndConvert("NGN", "USD", 150000)) . 这可以在Netbeans中非常容易地实现,您所需要做的就是拖放JLabelJTextFieldJButton然后调用setText(findExchangeRateAndConvert("NGN", "USD", 150000)) when the button is clicked taking the text from one currency, and setting the JLabel with the result. 单击按钮时,从一种货币中获取文本,然后将JLabel设置为结果。
Or 要么

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;
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM