Advertisement
thotfrnk

SwingCurrencyConverter.java

Jun 7th, 2023
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.13 KB | None | 0 0
  1. // school work
  2. import java.awt.*; //using awt's layouts
  3. import java.awt.event.*; //using awt's event classes and listener interfaces
  4. import javax.swing.*; //using swing's components and container
  5.  
  6. //a swing application extends from javax.swing.JFrame
  7. public class SwingCurrencyConverter extends JFrame {
  8.     private JTextField tfSGD, tfUSD, tfEuro;
  9.     private float singapore, usa, euro;
  10.  
  11.     public SwingCurrencyConverter() {
  12.         //swing components should be added to the content-pane of the JFrame
  13.         Container cp = getContentPane();
  14.         //set this container to grid layout of 3 rows and 2 columns
  15.         cp.setLayout(new GridLayout(3, 2, 10, 3));
  16.  
  17.         //components are added left-to-right, top-to-bottom
  18.         cp.add(new JLabel("Singapore Dollars "));
  19.         tfSGD = new JTextField(10);
  20.         tfSGD.setHorizontalAlignment(JTextField.RIGHT);
  21.         cp.add(tfSGD);
  22.  
  23.         tfSGD.addActionListener(new ActionListener() {
  24.             @Override
  25.             public void actionPerformed(ActionEvent evt) {
  26.                 singapore = Float.parseFloat(tfSGD.getText());
  27.  
  28.                 usa = (float) (singapore * 0.74);
  29.                 euro = (float) (singapore * 0.65);
  30.  
  31.                 tfUSD.setText(String.format("%.2f", usa));
  32.                 tfEuro.setText(String.format("%.2f", euro));
  33.             }
  34.         });
  35.  
  36.         cp.add(new JLabel("US Dollars "));
  37.         tfUSD = new JTextField(10);
  38.         tfUSD.setHorizontalAlignment(JTextField.RIGHT);
  39.         cp.add(tfUSD);
  40.  
  41.         tfUSD.addActionListener(new ActionListener() {
  42.             @Override
  43.             public void actionPerformed(ActionEvent evt) {
  44.                 usa = Float.parseFloat(tfUSD.getText());
  45.  
  46.                 singapore = (float) (usa * 1.41);
  47.                 euro = (float) (usa * 0.92);
  48.  
  49.                 tfSGD.setText(String.format("%.2f", singapore));
  50.                 tfEuro.setText(String.format("%.2f", euro));
  51.             }
  52.         });
  53.  
  54.         cp.add(new JLabel("Euros "));
  55.         tfEuro = new JTextField(10);
  56.         tfEuro.setHorizontalAlignment(JTextField.RIGHT);
  57.         cp.add(tfEuro);
  58.  
  59.         tfEuro.addActionListener(new ActionListener() {
  60.             @Override
  61.             public void actionPerformed(ActionEvent evt) {
  62.                 euro = Float.parseFloat(tfEuro.getText());
  63.  
  64.                 singapore = (float) (euro * 1.44);
  65.                 usa = (float) (euro * 1.07);
  66.  
  67.                 tfUSD.setText(String.format("%.2f", usa));
  68.                 tfSGD.setText(String.format("%.2f", singapore));
  69.             }
  70.         });
  71.  
  72.         setDefaultCloseOperation(EXIT_ON_CLOSE); // for the "window-close" button
  73.         setTitle("Swing Currency Converter");
  74.         setSize(300, 170);
  75.         setVisible(true);
  76.     }
  77.  
  78.     // The entry main() method
  79.     public static void main(String[] args) {
  80.         // For thread safety, use the event-dispatching thread to construct UI
  81.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  82.             @Override
  83.             public void run() {
  84.                 new SwingCurrencyConverter(); // Let the constructor does the job
  85.             }
  86.         });
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement