| 1 | import javax.swing.*; |
| 2 | import java.awt.*; |
| 3 | import java.awt.event.*; |
| 4 | |
| 5 | |
| 6 | /************************************************************************ |
| 7 | * Convert circle dimensions between radius and area. |
| 8 | * A demo program to serve as a target for a coverage testing tool. |
| 9 | ***********************************************************************/ |
| 10 | public class CircleConverter extends JFrame |
| 11 | { |
| 12 | private JTextField radiusField; // entry field for radius |
| 13 | private JTextField areaField; // entry field for area |
| 14 | private JButton clearButton; // clear the fields button |
| 15 | private JButton exitButton; // exit button |
| 16 | |
| 17 | private Container contents; |
| 18 | |
| 19 | // Constructor and GUI widget layout |
| 20 | public CircleConverter() |
| 21 | { |
| 22 | setTitle("Circle Converter"); |
| 23 | |
| 24 | contents = getContentPane(); |
| 25 | contents.setLayout(new FlowLayout()); |
| 26 | |
| 27 | JPanel row1 = new JPanel(); |
| 28 | JLabel radiusLabel = new JLabel("radius: "); |
| 29 | row1.add(radiusLabel); |
| 30 | |
| 31 | radiusField = new JTextField(4); |
| 32 | row1.add(radiusField); |
| 33 | contents.add(row1); |
| 34 | |
| 35 | JPanel row2 = new JPanel(); |
| 36 | JLabel areaLabel = new JLabel("area: "); |
| 37 | row2.add(areaLabel); |
| 38 | |
| 39 | areaField = new JTextField(8); |
| 40 | row2.add(areaField); |
| 41 | contents.add(row2); |
| 42 | |
| 43 | clearButton = new JButton ("Clear"); |
| 44 | clearButton.setMnemonic(KeyEvent.VK_C); |
| 45 | contents.add(clearButton); |
| 46 | |
| 47 | exitButton = new JButton ("Exit"); |
| 48 | exitButton.setMnemonic(KeyEvent.VK_X); |
| 49 | contents.add(exitButton); |
| 50 | |
| 51 | TextFieldHandler handler = new TextFieldHandler(); |
| 52 | areaField.addActionListener(handler); |
| 53 | radiusField.addActionListener(handler); |
| 54 | ButtonHandler bhandler = new ButtonHandler(); |
| 55 | clearButton.addActionListener(bhandler); |
| 56 | exitButton.addActionListener(bhandler); |
| 57 | |
| 58 | setDefaultCloseOperation(EXIT_ON_CLOSE); |
| 59 | setSize( 200, 150 ); |
| 60 | |
| 61 | setVisible(true); |
| 62 | } // end constructor |
| 63 | |
| 64 | |
| 65 | public static void main(String args[]) { |
| 66 | JFrame window = new CircleConverter(); |
| 67 | } |
| 68 | |
| 69 | /** An inner class for handling compute requests */ |
| 70 | private class TextFieldHandler implements ActionListener |
| 71 | { |
| 72 | |
| 73 | |
| 74 | // Handle the user's action. |
| 75 | // Called when user presses Enter in either of the text fields |
| 76 | public void actionPerformed(ActionEvent e) |
| 77 | { |
| 78 | // Did user enter a radius? |
| 79 | if ( e.getSource() == radiusField ) |
| 80 | { |
| 81 | double radius = Circle.toDouble(radiusField.getText()); |
| 82 | Circle aCircle = new Circle(radius,0); |
| 83 | areaField.setText(aCircle.getFormattedArea()); |
| 84 | } |
| 85 | // Did user enter an area? |
| 86 | else if (e.getSource() == areaField ) |
| 87 | { |
| 88 | double area = Circle.toDouble(areaField.getText()); |
| 89 | Circle aCircle = new Circle(0,area); |
| 90 | radiusField.setText(aCircle.getFormattedRadius()); |
| 91 | } |
| 92 | |
| 93 | } // end actionPerformed |
| 94 | } // end inner class |
| 95 | |
| 96 | /** An inner class for button actions */ |
| 97 | private class ButtonHandler implements ActionListener |
| 98 | { |
| 99 | // Handle the user's action. |
| 100 | // Called when the clear or exit buttons are pressed |
| 101 | public void actionPerformed(ActionEvent e) |
| 102 | { |
| 103 | if (e.getSource() == clearButton ) |
| 104 | { |
| 105 | radiusField.setText(""); |
| 106 | areaField.setText(""); |
| 107 | radiusField.requestFocus(); |
| 108 | } |
| 109 | else if (e.getSource() == exitButton ) |
| 110 | { |
| 111 | dispose(); |
| 112 | } |
| 113 | } // end actionPerformed |
| 114 | } // end inner class |
| 115 | } |