Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //imports for the gui
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JComboBox;
- import javax.swing.JLabel;
- import javax.swing.JMenu;
- import javax.swing.JMenuBar;
- import javax.swing.JMenuItem;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- /**
- * The main graphical panel used to display conversion components.
- *
- * This is the starting point for the assignment.
- *
- * The variable names have been deliberately made vague and generic, and most comments have been removed.
- *
- * You may want to start by improving the variable names and commenting what the existing code does.
- *
- * @author mdixon
- */
- @SuppressWarnings("serial")
- public class MainPanel extends JPanel {
- //declerations
- //part 7
- private final static String[] list = { "inches/cm","nautical miles/miles",
- "hectares/acres","mph/kph","metres/yards","celsius/fahrenheit","degrees/radians"};
- private JTextField textField;
- private JLabel label;
- private JComboBox<String> combo;
- //part 6 this delcares the countlabel for the gui
- private JLabel countlabel;
- private int count = 0;
- JMenuBar setupMenu() {
- JMenuBar menuBar = new JMenuBar();
- JMenu m1 = new JMenu("File");
- JMenu m2 = new JMenu("Help");
- menuBar.add(m1);
- menuBar.add(m2);
- //part 3 this is the code to add the exit button to the gui and name it
- JMenuItem item1 = new JMenuItem("Exit");
- item1.addActionListener(new ExitListener());
- m1.add(item1);
- //part 1 this part of the code lets you use shortcuts to use the menu quicker
- m1.setMnemonic(KeyEvent.VK_F);
- m2.setMnemonic(KeyEvent.VK_H);
- //part 1 this is too add images
- m1.setIcon(new ImageIcon("img/folder.png"));
- m2.setIcon(new ImageIcon("img/question.png"));
- //part 2 this is the code for the about button to appear on the gui
- JMenuItem about = new JMenuItem("About");
- AboutListener aboutlisten = new AboutListener();
- about.addActionListener(aboutlisten);
- return menuBar;
- }
- MainPanel() {
- ActionListener convertlistner = new ConvertListener();
- combo = new JComboBox<String>(list);
- combo.addActionListener(convertlistner); //convert values when option changed
- JLabel inputLabel = new JLabel("Enter value:");
- JButton convertButton = new JButton("Convert");
- convertButton.addActionListener(convertlistner); // convert values when pressed
- label = new JLabel("---");
- textField = new JTextField(5);
- //part 4 this gives you the power to use the convert button
- textField.addActionListener(convertlistner);
- //part 5 this adds the clear button to the gui to help clear the value
- //and counter when the button is pressed
- JButton clear = new JButton("clear");
- clear.addActionListener(new ClearListener());
- countlabel = new JLabel(Integer.toString(count));
- add(combo);
- add(inputLabel);
- add(textField);
- add(convertButton);
- add(label);
- add(clear);
- add(countlabel);
- setPreferredSize(new Dimension(800, 100));
- setBackground(Color.white);
- // part 11 this adds help for when you hover over different parts of the gui
- // to show you what that part actually does
- combo.setToolTipText("Shows you all the different units to convert");
- convertButton.setToolTipText("This does the conversion");
- clear.setToolTipText("This clears the results and textbox");
- textField.setToolTipText("This is where you enter your value to convert");
- }
- //part 2 this is the code for when it is clicked it shows you this message
- private class AboutListener implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- JOptionPane.showMessageDialog(
- null, "Unit Converter"
- + "\nAuthor: Jamie Wilkinson"
- + "\nCopyright: Leeds beckett university"); }}
- //part 5 this is the code that causes the clear action to happen
- private class ClearListener implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- textField.setText(null);
- countlabel.setText(Integer.toString(count = 0)); }}
- //part 3 this is the code of when the exit button is clicked the gui closes
- private class ExitListener implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- System.exit(0); }}
- private class ConvertListener implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent event) {
- String text = textField.getText().trim();
- // part 9 this code makes it so that if you do not enter anything into the input boxes
- //then a popup will appear telling you about the error
- try {
- double value = Double.parseDouble(text);
- if (text.isEmpty() == false) {
- // the factor applied during the conversion
- double factor = 0;
- // the offset applied during the conversion.
- double offset = 0;
- // Setup the correct factor/offset values depending on required conversion
- switch (combo.getSelectedIndex()) {
- //part 7 this is the code for each of the different conversion rates to appear in the gui
- case 0: // inches/cm
- factor = 2.54;
- break;
- case 1: // nautical miles/miles
- factor = 1.151;
- break;
- case 2: // hectares/acres
- factor = 2.471;
- break;
- case 3: // mph/kph
- factor = 1.609;
- break;
- case 4: // metres/yards
- factor = 1.094;
- break;
- case 5: // celsius/fahrenheit
- factor = 1.8;
- offset = 32;
- break;
- }
- double result = factor * value + offset;
- //part 10 this addes the limit to 2 decimal places in the code
- String resultdec=String.format("%.2f", result);
- label.setText(resultdec);
- //part 6 this adds the number to the count label every time it is pressed
- countlabel.setText(Integer.toString(count++));
- }
- } catch(NumberFormatException e) {
- JOptionPane.showMessageDialog(null, "This input is not valid sorry");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement