Guest User

Untitled

a guest
Nov 14th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. import javax.swing.*;
  2. import javax.swing.border.EmptyBorder;
  3. import java.awt.*;
  4.  
  5. import static javax.swing.SwingConstants.CENTER;
  6.  
  7. /**
  8. * File Week4Discussion.java
  9. * Simple area calculator.
  10. *
  11. * @author Lance Gundersen
  12. * @version 1.0
  13. * @since 2018-11-13
  14. */
  15. class AreaCalculator {
  16.  
  17. private JButton calculateButton;
  18. private JTextField sideOneText, sideTwoText, resultsText;
  19.  
  20. private AreaCalculator() {
  21. this.CreateGui();
  22. // No validation at this time.
  23. this.calculateButton.addActionListener(e -> calculateArea(sideOneText.getText(), sideTwoText.getText()));
  24. }
  25.  
  26. public static void main(String[] args) {
  27. new AreaCalculator();
  28. }
  29.  
  30. // Calculate area and set results text to results. No error checking.
  31. private void calculateArea(String sideA, String sideB) {
  32. String results = String.valueOf(Double.parseDouble(sideA) * Double.parseDouble(sideB));
  33. this.resultsText.setText(results);
  34. }
  35.  
  36. private void CreateGui() {
  37. // Used border of 10 more than once, DRY
  38. final EmptyBorder borderSizeTen = new EmptyBorder(10, 10, 10, 10);
  39.  
  40. // Create frame
  41. JFrame frame = new JFrame("Area Calc");
  42. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  43. frame.setPreferredSize(new Dimension(350, 300));
  44.  
  45. // Create panel layouts
  46. JPanel panel = new JPanel(new BorderLayout());
  47. panel.setBorder(borderSizeTen);
  48. JPanel panelTop = new JPanel(new GridLayout(6, 5, 5, 5));
  49. panelTop.setBorder(borderSizeTen);
  50. JPanel panelBottom = new JPanel(new GridLayout(2, 5, 5, 5));
  51. panelBottom.setBorder(borderSizeTen);
  52.  
  53. // Side A input text field
  54. JLabel sideOneLabel = new JLabel("Length of Side A:", CENTER);
  55. this.sideOneText = new JTextField();
  56. this.sideOneText = new JTextField("", 10);
  57.  
  58. // Side B input text field
  59. JLabel sideTwoLabel = new JLabel("Length of Side B:", CENTER);
  60. this.sideTwoText = new JTextField();
  61. this.sideTwoText = new JTextField("", 10);
  62.  
  63. // Side A input text field
  64. JLabel resultsLabel = new JLabel("Area:", CENTER);
  65. this.resultsText = new JTextField();
  66. this.resultsText = new JTextField("", 10);
  67.  
  68. // Results text field
  69. calculateButton = new JButton("Calculate Area");
  70.  
  71. // Add all elements to the top panel
  72. panelTop.add(sideOneLabel);
  73. panelTop.add(this.sideOneText);
  74. panelTop.add(sideTwoLabel);
  75. panelTop.add(this.sideTwoText);
  76. panelTop.add(calculateButton);
  77.  
  78. // Add all elements to the bottom panel
  79. panelBottom.add(resultsLabel);
  80. panelBottom.add(this.resultsText);
  81.  
  82. // Add all elements to the main panel
  83. panel.add(panelTop, BorderLayout.NORTH);
  84. panel.add(panelBottom, BorderLayout.SOUTH);
  85.  
  86. // Add all elements to the frame
  87. frame.add(panel);
  88. frame.pack();
  89. frame.setVisible(true);
  90. }
  91.  
  92. }
Add Comment
Please, Sign In to add comment