package gw2.tot; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class ToTGUI { ToTDrops records ; JButton button, button2 ; JFrame frame ; JLabel item, number, errorDisplay ; JTextField itemTxt, numberTxt ; final static boolean shouldFill = true; final static boolean shouldWeightX = true; final static boolean RIGHT_TO_LEFT = false; public ToTGUI() { records = new ToTDrops() ; frame = new JFrame("Trick or treat bag drops") ; frame.setSize(500,300) ; frame.setLayout(new GridBagLayout()) ; frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ; frame.setVisible(true) ; //declare and initialise constraints object GridBagConstraints c = new GridBagConstraints(); if (shouldFill) { //natural height, maximum width c.fill = GridBagConstraints.HORIZONTAL; } item = new JLabel("Enter the item that you got from the bag(s):") ; //constraints for item if (shouldWeightX) { c.weightx = 0.5; } c.gridx = 0; c.gridy = 0; frame.add(item, c); itemTxt = new JTextField("") ; itemTxt.setPreferredSize(new Dimension(100,20)) ; //constraints for itemTxt if (shouldWeightX) { c.weightx = 0.5; } c.gridx = 1; c.gridy = 0; frame.add(itemTxt, c); number = new JLabel("Enter the amount obtained") ; if (shouldWeightX) { c.weightx = 0.5; } c.gridx = 0; c.gridy = 1; frame.add(number, c); numberTxt = new JTextField("") ; numberTxt.setPreferredSize(new Dimension(100,20)) ; if (shouldWeightX) { c.weightx = 0.5; } c.gridx = 1; c.gridy = 1; frame.add(numberTxt, c); button = new JButton("Add item(s)") ; button.addActionListener(new AddButtonListener(this)) ; if (shouldWeightX) { c.weightx = 1; } c.gridx = 0; c.gridy = 2; frame.add(button, c); button2 = new JButton("Subtract item(s)") ; button2.addActionListener(new SubtractButtonListener(this)) ; if (shouldWeightX) { c.weightx = 1; } c.gridx = 0; c.gridy = 3; frame.add(button2, c); errorDisplay = new JLabel("") ; errorDisplay.setPreferredSize(new Dimension(frame.getWidth(),20)) ; if (shouldWeightX) { c.weightx = 1; } c.gridx = 0; c.gridy = 4; frame.add(errorDisplay, c); frame.pack() ; //pack the frame frame.setResizable(false) ; } public static void main(String[] args) { ToTGUI gui = new ToTGUI() ; } } class AddButtonListener implements ActionListener { private ToTGUI obj ; public AddButtonListener(ToTGUI obj) { this.obj = obj ; } public void actionPerformed(ActionEvent e) { try { obj.records.incrementTotal(obj.itemTxt.getText(),Integer.parseInt(obj.numberTxt.getText())) ; obj.records.generateEntryList() ; } catch (ItemNotFoundException ex) { obj.errorDisplay.setText(ex.getMessage()) ; } catch (NumberFormatException ex) { obj.errorDisplay.setText(ex.getMessage()) ; } catch (IllegalArgumentException ex) { obj.errorDisplay.setText(ex.getMessage()) ; } //System.out.println(obj.itemTxt.getText()) ; catch (Exception ex) { // TODO Auto-generated catch block ex.printStackTrace(); } } } class SubtractButtonListener implements ActionListener { private ToTGUI obj ; public SubtractButtonListener(ToTGUI obj) { this.obj = obj ; } public void actionPerformed(ActionEvent e) { try { obj.records.decrementTotal(obj.itemTxt.getText(),Integer.parseInt(obj.numberTxt.getText())) ; obj.records.generateEntryList() ; } catch(ItemNotFoundException ex) { obj.errorDisplay.setText(ex.getMessage()) ; } catch(NumberFormatException ex) { obj.errorDisplay.setText(ex.getMessage()) ; } catch (IllegalArgumentException ex) { obj.errorDisplay.setText(ex.getMessage()) ; } catch (Exception ex) { // TODO Auto-generated catch block ex.printStackTrace(); } } }