Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.34 KB | None | 0 0
  1. //imports for the gui
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.KeyEvent;
  7. import javax.swing.ImageIcon;
  8. import javax.swing.JButton;
  9. import javax.swing.JComboBox;
  10. import javax.swing.JLabel;
  11. import javax.swing.JMenu;
  12. import javax.swing.JMenuBar;
  13. import javax.swing.JMenuItem;
  14. import javax.swing.JOptionPane;
  15. import javax.swing.JPanel;
  16. import javax.swing.JTextField;
  17.  
  18. /**
  19. * The main graphical panel used to display conversion components.
  20. *
  21. * This is the starting point for the assignment.
  22. *
  23. * The variable names have been deliberately made vague and generic, and most comments have been removed.
  24. *
  25. * You may want to start by improving the variable names and commenting what the existing code does.
  26. *
  27. * @author mdixon
  28. */
  29. @SuppressWarnings("serial")
  30. public class MainPanel extends JPanel {
  31. //declerations
  32. //part 7
  33. private final static String[] list = { "inches/cm","nautical miles/miles",
  34. "hectares/acres","mph/kph","metres/yards","celsius/fahrenheit","degrees/radians"};
  35. private JTextField textField;
  36. private JLabel label;
  37. private JComboBox<String> combo;
  38.  
  39. //part 6 this delcares the countlabel for the gui
  40. private JLabel countlabel;
  41. private int count = 0;
  42.  
  43.  
  44.  
  45. JMenuBar setupMenu() {
  46.  
  47. JMenuBar menuBar = new JMenuBar();
  48.  
  49. JMenu m1 = new JMenu("File");
  50. JMenu m2 = new JMenu("Help");
  51.  
  52. menuBar.add(m1);
  53. menuBar.add(m2);
  54. //part 3 this is the code to add the exit button to the gui and name it
  55. JMenuItem item1 = new JMenuItem("Exit");
  56. item1.addActionListener(new ExitListener());
  57. m1.add(item1);
  58.  
  59. //part 1 this part of the code lets you use shortcuts to use the menu quicker
  60. m1.setMnemonic(KeyEvent.VK_F);
  61. m2.setMnemonic(KeyEvent.VK_H);
  62.  
  63. //part 1 this is too add images
  64. m1.setIcon(new ImageIcon("img/folder.png"));
  65. m2.setIcon(new ImageIcon("img/question.png"));
  66.  
  67. //part 2 this is the code for the about button to appear on the gui
  68. JMenuItem about = new JMenuItem("About");
  69. AboutListener aboutlisten = new AboutListener();
  70. about.addActionListener(aboutlisten);
  71.  
  72. return menuBar;
  73. }
  74.  
  75.  
  76. MainPanel() {
  77.  
  78. ActionListener convertlistner = new ConvertListener();
  79.  
  80. combo = new JComboBox<String>(list);
  81. combo.addActionListener(convertlistner); //convert values when option changed
  82.  
  83. JLabel inputLabel = new JLabel("Enter value:");
  84.  
  85. JButton convertButton = new JButton("Convert");
  86. convertButton.addActionListener(convertlistner); // convert values when pressed
  87.  
  88. label = new JLabel("---");
  89. textField = new JTextField(5);
  90.  
  91. //part 4 this gives you the power to use the convert button
  92. textField.addActionListener(convertlistner);
  93.  
  94. //part 5 this adds the clear button to the gui to help clear the value
  95. //and counter when the button is pressed
  96. JButton clear = new JButton("clear");
  97. clear.addActionListener(new ClearListener());
  98. countlabel = new JLabel(Integer.toString(count));
  99.  
  100. add(combo);
  101. add(inputLabel);
  102. add(textField);
  103. add(convertButton);
  104. add(label);
  105. add(clear);
  106. add(countlabel);
  107.  
  108. setPreferredSize(new Dimension(800, 100));
  109. setBackground(Color.white);
  110.  
  111. // part 11 this adds help for when you hover over different parts of the gui
  112. // to show you what that part actually does
  113. combo.setToolTipText("Shows you all the different units to convert");
  114. convertButton.setToolTipText("This does the conversion");
  115. clear.setToolTipText("This clears the results and textbox");
  116. textField.setToolTipText("This is where you enter your value to convert");
  117. }
  118. //part 2 this is the code for when it is clicked it shows you this message
  119. private class AboutListener implements ActionListener {
  120. public void actionPerformed(ActionEvent event) {
  121. JOptionPane.showMessageDialog(
  122. null, "Unit Converter"
  123. + "\nAuthor: Jamie Wilkinson"
  124. + "\nCopyright: Leeds beckett university"); }}
  125.  
  126. //part 5 this is the code that causes the clear action to happen
  127. private class ClearListener implements ActionListener {
  128. public void actionPerformed(ActionEvent event) {
  129. textField.setText(null);
  130. countlabel.setText(Integer.toString(count = 0)); }}
  131.  
  132. //part 3 this is the code of when the exit button is clicked the gui closes
  133. private class ExitListener implements ActionListener {
  134. public void actionPerformed(ActionEvent event) {
  135. System.exit(0); }}
  136.  
  137.  
  138. private class ConvertListener implements ActionListener {
  139.  
  140. @Override
  141. public void actionPerformed(ActionEvent event) {
  142.  
  143. String text = textField.getText().trim();
  144.  
  145. // part 9 this code makes it so that if you do not enter anything into the input boxes
  146. //then a popup will appear telling you about the error
  147. try {
  148. double value = Double.parseDouble(text);
  149. if (text.isEmpty() == false) {
  150.  
  151. // the factor applied during the conversion
  152. double factor = 0;
  153.  
  154. // the offset applied during the conversion.
  155. double offset = 0;
  156.  
  157. // Setup the correct factor/offset values depending on required conversion
  158. switch (combo.getSelectedIndex()) {
  159. //part 7 this is the code for each of the different conversion rates to appear in the gui
  160. case 0: // inches/cm
  161. factor = 2.54;
  162. break;
  163. case 1: // nautical miles/miles
  164. factor = 1.151;
  165. break;
  166. case 2: // hectares/acres
  167. factor = 2.471;
  168. break;
  169. case 3: // mph/kph
  170. factor = 1.609;
  171. break;
  172. case 4: // metres/yards
  173. factor = 1.094;
  174. break;
  175. case 5: // celsius/fahrenheit
  176. factor = 1.8;
  177. offset = 32;
  178. break;
  179.  
  180.  
  181. }
  182.  
  183. double result = factor * value + offset;
  184. //part 10 this addes the limit to 2 decimal places in the code
  185. String resultdec=String.format("%.2f", result);
  186. label.setText(resultdec);
  187. //part 6 this adds the number to the count label every time it is pressed
  188. countlabel.setText(Integer.toString(count++));
  189. }
  190. } catch(NumberFormatException e) {
  191. JOptionPane.showMessageDialog(null, "This input is not valid sorry");
  192. }
  193. }
  194. }
  195.  
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement