Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. /**
  2. *
  3. * @Sam Amirtahmasebi
  4. * @v0.3
  5. *
  6. */
  7.  
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.io.PrintStream;
  11. import java.util.Scanner;
  12. import static javax.swing.JOptionPane.*;
  13. import javax.swing.*;
  14. import java.awt.*;
  15. import java.awt.event.*;
  16.  
  17. public class Valutakalkulator extends JFrame implements ActionListener
  18. {
  19. private JTextField txtNOK, txtAnnen;
  20. private JLabel label;
  21.  
  22. public Valutakalkulator()
  23. {
  24. super("VALUTAKALKULATOR");
  25. JPanel pnlTop = new JPanel();
  26. pnlTop.setLayout(new GridLayout(1, 4));
  27.  
  28. pnlTop.add(new JLabel("NOK"));
  29. txtNOK = new JTextField();
  30. pnlTop.add(txtNOK);
  31.  
  32. label = new JLabel("Annen valuta");
  33. pnlTop.add(label);
  34. txtAnnen = new JTextField();
  35. pnlTop.add(txtAnnen);
  36.  
  37. JPanel pnlCenter = new JPanel();
  38. pnlCenter.setLayout(new GridLayout(2, 2));
  39.  
  40. JPanel pnlBottom = new JPanel();
  41. pnlBottom.setLayout(new GridLayout(1, 4));
  42.  
  43. JButton btnRemoveNOK = new JButton("Fjern NOK");
  44. JButton btnRemoveCurrency = new JButton("Fjern annen valuta");
  45. JButton btnRemoveBoth = new JButton("Fjern begge");
  46. JButton btnExit = new JButton("Avslutt");
  47.  
  48. JButton btnGBP = new JButton("GBP");
  49. JButton btnEUR = new JButton("EUR");
  50. JButton btnUSD = new JButton("USD");
  51. JButton btnSEK = new JButton("SEK");
  52.  
  53. pnlCenter.add(btnGBP);
  54. pnlCenter.add(btnEUR);
  55. pnlCenter.add(btnUSD);
  56. pnlCenter.add(btnSEK);
  57.  
  58. pnlBottom.add(btnRemoveNOK);
  59. pnlBottom.add(btnRemoveCurrency);
  60. pnlBottom.add(btnRemoveBoth);
  61. pnlBottom.add(btnExit);
  62.  
  63. btnRemoveNOK.addActionListener(new ActionListener()
  64. {
  65. public void actionPerformed(ActionEvent e)
  66. {
  67. txtNOK.setText("");
  68. }
  69. });
  70.  
  71. btnRemoveCurrency.addActionListener(new ActionListener()
  72. {
  73. public void actionPerformed(ActionEvent e)
  74. {
  75. txtAnnen.setText("");
  76. label.setText("Annen valuta");
  77. }
  78. });
  79.  
  80. btnRemoveBoth.addActionListener(new ActionListener()
  81. {
  82. public void actionPerformed(ActionEvent e)
  83. {
  84. txtNOK.setText("");
  85. txtAnnen.setText("");
  86. }
  87. });
  88.  
  89. btnExit.addActionListener(new ActionListener()
  90. {
  91. public void actionPerformed(ActionEvent e)
  92. {
  93. System.exit(0);
  94. }
  95. });
  96.  
  97. btnGBP.addActionListener(this);
  98. btnEUR.addActionListener(this);
  99. btnUSD.addActionListener(this);
  100. btnSEK.addActionListener(this);
  101.  
  102. add(pnlTop, BorderLayout.NORTH);
  103. add(pnlBottom, BorderLayout.SOUTH);
  104. add(pnlCenter, BorderLayout.CENTER);
  105.  
  106. setSize(600, 135);
  107. setVisible(true);
  108. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  109. }
  110.  
  111. public void actionPerformed(ActionEvent event)
  112. {
  113. JButton clicked = (JButton) event.getSource();
  114.  
  115. String strNOK = txtNOK.getText();
  116. String strAnnen = txtAnnen.getText();
  117. double result;
  118.  
  119. if (txtNOK.getText().length() == 0 && txtAnnen.getText().length() == 0)
  120. {
  121. showMessageDialog(this, "Du må skrive ett beløp!");
  122. }
  123. else if (txtNOK.getText().length() > 0 && txtAnnen.getText().length() > 0)
  124. {
  125. showMessageDialog(this, "Du må skrive bare ett beløp!");
  126. }
  127. else if (txtNOK.getText().length() > 0 && txtAnnen.getText().length() <= 0)
  128. {
  129. double norsk = Double.parseDouble(strNOK);
  130.  
  131. if (event.getActionCommand().equals("GBP"))
  132. {
  133. result = norsk / 10.96;
  134. txtAnnen.setText("" + result);
  135. label.setText("GBP");
  136. }
  137. else if (event.getActionCommand().equals("EUR"))
  138. {
  139. result = norsk / 9.31;
  140. txtAnnen.setText("" + result);
  141. label.setText("EUR");
  142. }
  143. else if (event.getActionCommand().equals("USD"))
  144. {
  145. result = norsk / 8.55;
  146. txtAnnen.setText("" + result);
  147. label.setText("USD");
  148. }
  149. else if (event.getActionCommand().equals("SEK"))
  150. {
  151. result = norsk / 0.97;
  152. txtAnnen.setText("" + result);
  153. label.setText("SEK");
  154. }
  155. }
  156. else if (txtNOK.getText().length() <= 0 && txtAnnen.getText().length() > 0)
  157. {
  158. double valuta = Double.parseDouble(strAnnen);
  159.  
  160. if (event.getActionCommand().equals("GBP"))
  161. {
  162. result = valuta * 10.96;
  163. txtNOK.setText("" + result);
  164. label.setText("GBP");
  165. }
  166. else if (event.getActionCommand().equals("EUR"))
  167. {
  168. result = valuta * 9.31;
  169. txtNOK.setText("" + result);
  170. label.setText("EUR");
  171. }
  172. else if (event.getActionCommand().equals("USD"))
  173. {
  174. result = valuta * 8.55;
  175. txtNOK.setText("" + result);
  176. label.setText("USD");
  177. }
  178. else if (event.getActionCommand().equals("SEK"))
  179. {
  180. result = valuta * 0.97;
  181. txtNOK.setText("" + result);
  182. label.setText("SEK");
  183. }
  184. }
  185. }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement