import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * A GUI application, just using FlowLayout, * including JTextField's for numeric * input and output, amount-owed formatting, and * exception handling for "bad" numeric input * * @author Sharon Tuttle * @version 2015-10-07 */ public class FlowOnlyEx { /** * creates a FlowOnlyExFrame * * @param args not used here */ public static void main(String args[]) { EventQueue.invokeLater( new Runnable() { public void run() { FlowOnlyExFrame mainFrame = new FlowOnlyExFrame(); mainFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); mainFrame.setVisible(true); } }); } } /** * A frame with a panel containing several elements, including * an uneditable JTextField, intended to be used for output only. */ class FlowOnlyExFrame extends JFrame { // data fields private final static int DEFAULT_WIDTH = 375; private final static int DEFAULT_HEIGHT = 300; /** * constructs a FlowOnlyExFrame instance */ public FlowOnlyExFrame() { this.setTitle("FlowLayout Only Example"); this.setSize(this.DEFAULT_WIDTH, this.DEFAULT_HEIGHT); // add FlowOnlyEx panel to frame FlowOnlyExPanel panel = new FlowOnlyExPanel(); this.add(panel); } } /** * A panel containing elements to determine the total cost * for some number of items */ class FlowOnlyExPanel extends JPanel { // data fields for FlowOnlyExPanel public final static Font DISPLAY_FONT = new Font( "Dialog", Font.PLAIN, 20); // a lighter gray than that provided by Color.LIGHT_GRAY (r=192, // g=192, b=192) public final static Color LIGHTER_GRAY = new Color(230, 230, 230); private JTextField visitorGreeting; private JTextField visitorNameField; private JTextField quantityField; private JTextField costField; private JTextField owedField; /** * constructs a FlowOnlyEx panel instance */ public FlowOnlyExPanel() { // create the labels, textfields, and button for this panel // set up the visitor-related components JLabel visitorPrompt = new JLabel("Type your name:"); visitorPrompt.setForeground(Color.RED); visitorPrompt.setFont(this.DISPLAY_FONT); this.add(visitorPrompt); visitorNameField = new JTextField(9); visitorNameField.setFont(this.DISPLAY_FONT); this.add(visitorNameField); visitorGreeting = new JTextField( "No visitor has registered yet", 15); visitorGreeting.setForeground(Color.BLUE); visitorGreeting.setFont(this.DISPLAY_FONT); visitorGreeting.setEditable(false); visitorGreeting.setBackground(this.LIGHTER_GRAY); this.add(visitorGreeting); // set up quantity, cost, and amount-owed textfields JLabel quantityPrompt = new JLabel("Quantity: "); quantityPrompt.setFont(this.DISPLAY_FONT); this.add(quantityPrompt); quantityField = new JTextField("0", 5); quantityField.setFont(this.DISPLAY_FONT); quantityField.setHorizontalAlignment(JTextField.RIGHT); this.add(quantityField); JLabel costPrompt = new JLabel("Cost per unit: "); costPrompt.setFont(this.DISPLAY_FONT); this.add(costPrompt); costField = new JTextField("0.00", 5); costField.setFont(this.DISPLAY_FONT); costField.setHorizontalAlignment(JTextField.RIGHT); this.add(costField); JLabel owedLabel = new JLabel(" Total owed: "); owedLabel.setFont(this.DISPLAY_FONT); this.add(owedLabel); owedField = new JTextField("0.00", 5); owedField.setFont(this.DISPLAY_FONT); owedField.setEditable(false); // an output field! owedField.setBackground(this.LIGHTER_GRAY); owedField.setHorizontalAlignment(JTextField.RIGHT); this.add(owedField); // set up the submit button JButton submitMe = new JButton("Submit"); submitMe.setFont(this.DISPLAY_FONT); this.add(submitMe); // create submission action SubmitAction aSubmitAction = new SubmitAction(); submitMe.addActionListener(aSubmitAction); } // end FlowOnlyExPanel constructor /** * An action listener that changes the greeting textfield to * reflect the entered visitor's name, and displays the amount * owed based on the quantity and cost entered. */ private class SubmitAction implements ActionListener { // default constructor will suffice, in this case /** * changes the greeting label to reflect the entered * visitor's name, and displays the amount owed based * on the entered quantity and cost. * * @param event a push of the Submit button */ public void actionPerformed(ActionEvent event) { visitorGreeting.setText("Hi, " + visitorNameField.getText()); // make sure these are still "visible" outside // of try-blocks int currQuantity; double currCost; try { currQuantity = Integer.parseInt( quantityField.getText()); } catch (NumberFormatException exc) { (new JOptionPane()).showMessageDialog(null, "Quantity must be an integer!"); quantityField.setText("0"); currQuantity = 0; } try { currCost = Double.parseDouble(costField.getText()); } catch (NumberFormatException exc) { (new JOptionPane()).showMessageDialog(null, "Cost must be numeric!"); costField.setText("0.00"); currCost = 0; } double currOwed = currQuantity * currCost; owedField.setText(String.format("$%5.2f", currOwed)); } } }