⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 convertergui.java

📁 Java经典例程 从外国一大学计算机教授出版物下载的代码 经典
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javagently.*;

class ConverterGUI extends Frame {

  /* The Converter Program     by J M Bishop  Dec 1998
   * ---------------------     Display version July 1999
   *                           GUI version July 1999
   *                           updated August 2000
   * Keeps the exchange rates from one currency into
   * many others and enables currency exchanges to be
   * estimated.
   *
   * Illustrates the use of a customised GUI
   * and managed sequences of events
   */

   ConverterGUI () throws IOException {
      DataHandler data = new DataHandler ();
      data.initialize();
      data.readIn();
  }

  class DataHandler extends Frame
               implements ActionListener, ItemListener {
    Hashtable table = new Hashtable();

    // read in each line of data and store in
    // the hash table with country as key

    Choice    fromChoice, toChoice;
    boolean   fromSelected, toSelected;
    TextField amountField;
    TextArea  resultField;
    String    toCountry, fromCountry;
    Button    goButton;

    void initialize () {
      Panel p = new Panel (new BorderLayout());

        // left hand side panel
        Panel q = new Panel();
          q.add ("North",new Label ("From"));
          fromChoice = new Choice();
            fromChoice.addItemListener (this);
            q.add("Center",fromChoice);
          p.add ("West", q);
        // right hand side panel
        q = new Panel();
          q.add ("North",new Label ("To"));
          toChoice = new Choice();
            toChoice.addItemListener (this);
            q.add("Center",toChoice);
          p.add ("East",q);
        // Centre panel
        q = new Panel(new BorderLayout());
          Panel r = new Panel();
            r.add(new Label("Amount"));
            amountField = new TextField("1000  ");
              amountField.addActionListener(this);
              r.add(amountField);
              q.add("North",r);
            resultField = new TextArea(8,20);
              q.add ("Center",resultField);
              resultField.append("First select the countries\n");
            goButton = new Button ("Convert");
              goButton.addActionListener(this);
              q.add("South",goButton);
          p.add("Center",q);
      add(p);
      setTitle("Currency Converter");
      setSize(610,300);
      addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          System.exit(0);
        }
      });
    }

    public void actionPerformed (ActionEvent e) {
      int amount = 1000;
      if (e.getSource() == amountField)
        amount = (int) Integer.parseInt(amountField.getText());
      else
      if (e.getSource() == goButton) {
        if (fromSelected && toSelected)
          transaction(amount);
        else
          resultField.append("First select the countries\n");
      }
    }

    public void itemStateChanged(ItemEvent e) {
      String s = (String) e.getItem();
      if (e.getItemSelectable() == fromChoice) {
        fromSelected = true;
        fromCountry = s;
      }
      else {
        toSelected = true;
        toCountry = s;
      }
    }

    void transaction (int amount) {
      Rates fromRate = (Rates) table.get(fromCountry);
      Rates toRate = (Rates) table.get(toCountry);
      resultField.append(amount+" "+fromRate.country+" "+
          fromRate.currency+
          "\n in "+toRate.country+" "+toRate.currency+"\n was "+
          Stream.format(amount/fromRate.conversion*
                      toRate.conversion,10,3)+"\n\n");
      fromSelected = false;
      toSelected = false;
    }

  void readIn() throws IOException {
    Stream fin = new Stream("Rates.dat", Stream.READ);
    Rates rate;
    try {
      for (int i = 0; ; i++) {
        rate = new Rates();
        rate.setRate(fin);
        table.put(rate.country, rate);
        toChoice.addItem(rate.country);
        fromChoice.addItem(rate.country);
      }
     }
     catch (EOFException e) {}
     fromSelected = false;
     toSelected = false;
     setVisible(true);
   }
  }

   public static void main(String[] args) throws IOException {
    new ConverterGUI();
  }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -