Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // school work
- import java.awt.*; //using awt's layouts
- import java.awt.event.*; //using awt's event classes and listener interfaces
- import javax.swing.*; //using swing's components and container
- //a swing application extends from javax.swing.JFrame
- public class SwingCurrencyConverter extends JFrame {
- private JTextField tfSGD, tfUSD, tfEuro;
- private float singapore, usa, euro;
- public SwingCurrencyConverter() {
- //swing components should be added to the content-pane of the JFrame
- Container cp = getContentPane();
- //set this container to grid layout of 3 rows and 2 columns
- cp.setLayout(new GridLayout(3, 2, 10, 3));
- //components are added left-to-right, top-to-bottom
- cp.add(new JLabel("Singapore Dollars "));
- tfSGD = new JTextField(10);
- tfSGD.setHorizontalAlignment(JTextField.RIGHT);
- cp.add(tfSGD);
- tfSGD.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent evt) {
- singapore = Float.parseFloat(tfSGD.getText());
- usa = (float) (singapore * 0.74);
- euro = (float) (singapore * 0.65);
- tfUSD.setText(String.format("%.2f", usa));
- tfEuro.setText(String.format("%.2f", euro));
- }
- });
- cp.add(new JLabel("US Dollars "));
- tfUSD = new JTextField(10);
- tfUSD.setHorizontalAlignment(JTextField.RIGHT);
- cp.add(tfUSD);
- tfUSD.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent evt) {
- usa = Float.parseFloat(tfUSD.getText());
- singapore = (float) (usa * 1.41);
- euro = (float) (usa * 0.92);
- tfSGD.setText(String.format("%.2f", singapore));
- tfEuro.setText(String.format("%.2f", euro));
- }
- });
- cp.add(new JLabel("Euros "));
- tfEuro = new JTextField(10);
- tfEuro.setHorizontalAlignment(JTextField.RIGHT);
- cp.add(tfEuro);
- tfEuro.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent evt) {
- euro = Float.parseFloat(tfEuro.getText());
- singapore = (float) (euro * 1.44);
- usa = (float) (euro * 1.07);
- tfUSD.setText(String.format("%.2f", usa));
- tfSGD.setText(String.format("%.2f", singapore));
- }
- });
- setDefaultCloseOperation(EXIT_ON_CLOSE); // for the "window-close" button
- setTitle("Swing Currency Converter");
- setSize(300, 170);
- setVisible(true);
- }
- // The entry main() method
- public static void main(String[] args) {
- // For thread safety, use the event-dispatching thread to construct UI
- javax.swing.SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- new SwingCurrencyConverter(); // Let the constructor does the job
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement