Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * @Sam Amirtahmasebi
- * @v0.3
- *
- */
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.PrintStream;
- import java.util.Scanner;
- import static javax.swing.JOptionPane.*;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class Valutakalkulator extends JFrame implements ActionListener
- {
- private JTextField txtNOK, txtAnnen;
- private JLabel label;
- public Valutakalkulator()
- {
- super("VALUTAKALKULATOR");
- JPanel pnlTop = new JPanel();
- pnlTop.setLayout(new GridLayout(1, 4));
- pnlTop.add(new JLabel("NOK"));
- txtNOK = new JTextField();
- pnlTop.add(txtNOK);
- label = new JLabel("Annen valuta");
- pnlTop.add(label);
- txtAnnen = new JTextField();
- pnlTop.add(txtAnnen);
- JPanel pnlCenter = new JPanel();
- pnlCenter.setLayout(new GridLayout(2, 2));
- JPanel pnlBottom = new JPanel();
- pnlBottom.setLayout(new GridLayout(1, 4));
- JButton btnRemoveNOK = new JButton("Fjern NOK");
- JButton btnRemoveCurrency = new JButton("Fjern annen valuta");
- JButton btnRemoveBoth = new JButton("Fjern begge");
- JButton btnExit = new JButton("Avslutt");
- JButton btnGBP = new JButton("GBP");
- JButton btnEUR = new JButton("EUR");
- JButton btnUSD = new JButton("USD");
- JButton btnSEK = new JButton("SEK");
- pnlCenter.add(btnGBP);
- pnlCenter.add(btnEUR);
- pnlCenter.add(btnUSD);
- pnlCenter.add(btnSEK);
- pnlBottom.add(btnRemoveNOK);
- pnlBottom.add(btnRemoveCurrency);
- pnlBottom.add(btnRemoveBoth);
- pnlBottom.add(btnExit);
- btnRemoveNOK.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- txtNOK.setText("");
- }
- });
- btnRemoveCurrency.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- txtAnnen.setText("");
- label.setText("Annen valuta");
- }
- });
- btnRemoveBoth.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- txtNOK.setText("");
- txtAnnen.setText("");
- }
- });
- btnExit.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- System.exit(0);
- }
- });
- btnGBP.addActionListener(this);
- btnEUR.addActionListener(this);
- btnUSD.addActionListener(this);
- btnSEK.addActionListener(this);
- add(pnlTop, BorderLayout.NORTH);
- add(pnlBottom, BorderLayout.SOUTH);
- add(pnlCenter, BorderLayout.CENTER);
- setSize(600, 135);
- setVisible(true);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public void actionPerformed(ActionEvent event)
- {
- JButton clicked = (JButton) event.getSource();
- String strNOK = txtNOK.getText();
- String strAnnen = txtAnnen.getText();
- double result;
- if (txtNOK.getText().length() == 0 && txtAnnen.getText().length() == 0)
- {
- showMessageDialog(this, "Du må skrive ett beløp!");
- }
- else if (txtNOK.getText().length() > 0 && txtAnnen.getText().length() > 0)
- {
- showMessageDialog(this, "Du må skrive bare ett beløp!");
- }
- else if (txtNOK.getText().length() > 0 && txtAnnen.getText().length() <= 0)
- {
- double norsk = Double.parseDouble(strNOK);
- if (event.getActionCommand().equals("GBP"))
- {
- result = norsk / 10.96;
- txtAnnen.setText("" + result);
- label.setText("GBP");
- }
- else if (event.getActionCommand().equals("EUR"))
- {
- result = norsk / 9.31;
- txtAnnen.setText("" + result);
- label.setText("EUR");
- }
- else if (event.getActionCommand().equals("USD"))
- {
- result = norsk / 8.55;
- txtAnnen.setText("" + result);
- label.setText("USD");
- }
- else if (event.getActionCommand().equals("SEK"))
- {
- result = norsk / 0.97;
- txtAnnen.setText("" + result);
- label.setText("SEK");
- }
- }
- else if (txtNOK.getText().length() <= 0 && txtAnnen.getText().length() > 0)
- {
- double valuta = Double.parseDouble(strAnnen);
- if (event.getActionCommand().equals("GBP"))
- {
- result = valuta * 10.96;
- txtNOK.setText("" + result);
- label.setText("GBP");
- }
- else if (event.getActionCommand().equals("EUR"))
- {
- result = valuta * 9.31;
- txtNOK.setText("" + result);
- label.setText("EUR");
- }
- else if (event.getActionCommand().equals("USD"))
- {
- result = valuta * 8.55;
- txtNOK.setText("" + result);
- label.setText("USD");
- }
- else if (event.getActionCommand().equals("SEK"))
- {
- result = valuta * 0.97;
- txtNOK.setText("" + result);
- label.setText("SEK");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement