Guest User

ToTGUI

a guest
Jun 17th, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.89 KB | None | 0 0
  1. package gw2.tot;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.GridBagConstraints;
  5. import java.awt.GridBagLayout;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8.  
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JTextField;
  13.  
  14. public class ToTGUI
  15. {
  16.     ToTDrops records ;
  17.     JButton button, button2 ;
  18.     JFrame frame ;
  19.     JLabel item, number, errorDisplay ;
  20.     JTextField itemTxt, numberTxt ;
  21.     final static boolean shouldFill = true;
  22.     final static boolean shouldWeightX = true;
  23.     final static boolean RIGHT_TO_LEFT = false;
  24.  
  25.     public ToTGUI()
  26.     {
  27.         records = new ToTDrops() ;
  28.        
  29.         frame = new JFrame("Trick or treat bag drops") ;
  30.         frame.setSize(500,300) ;
  31.         frame.setLayout(new GridBagLayout()) ;
  32.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
  33.         frame.setVisible(true) ;
  34.  
  35.         //declare and initialise constraints object
  36.         GridBagConstraints c = new GridBagConstraints();
  37.         if (shouldFill) {
  38.             //natural height, maximum width
  39.             c.fill = GridBagConstraints.HORIZONTAL;
  40.             }
  41.  
  42.        
  43.         item = new JLabel("Enter the item that you got from the bag(s):") ;
  44.         //constraints for item
  45.         if (shouldWeightX) {
  46.             c.weightx = 0.5;
  47.             }
  48.             c.gridx = 0;
  49.             c.gridy = 0;
  50.             frame.add(item, c);
  51.            
  52.         itemTxt = new JTextField("") ;
  53.         itemTxt.setPreferredSize(new Dimension(100,20)) ;
  54.         //constraints for itemTxt
  55.         if (shouldWeightX) {
  56.             c.weightx = 0.5;
  57.             }
  58.             c.gridx = 1;
  59.             c.gridy = 0;
  60.             frame.add(itemTxt, c);
  61.         number = new JLabel("Enter the amount obtained") ;
  62.         if (shouldWeightX) {
  63.             c.weightx = 0.5;
  64.             }
  65.             c.gridx = 0;
  66.             c.gridy = 1;
  67.             frame.add(number, c);
  68.         numberTxt = new JTextField("") ;
  69.         numberTxt.setPreferredSize(new Dimension(100,20)) ;
  70.         if (shouldWeightX) {
  71.             c.weightx = 0.5;
  72.             }
  73.             c.gridx = 1;
  74.             c.gridy = 1;
  75.             frame.add(numberTxt, c);
  76.         button = new JButton("Add item(s)") ;
  77.         button.addActionListener(new AddButtonListener(this)) ;
  78.         if (shouldWeightX) {
  79.             c.weightx = 1;
  80.             }
  81.             c.gridx = 0;
  82.             c.gridy = 2;
  83.             frame.add(button, c);
  84.         button2 = new JButton("Subtract item(s)") ;
  85.         button2.addActionListener(new SubtractButtonListener(this)) ;
  86.         if (shouldWeightX) {
  87.             c.weightx = 1;
  88.             }
  89.             c.gridx = 0;
  90.             c.gridy = 3;
  91.             frame.add(button2, c);
  92.         errorDisplay = new JLabel("") ;
  93.         errorDisplay.setPreferredSize(new Dimension(frame.getWidth(),20)) ;
  94.         if (shouldWeightX) {
  95.             c.weightx = 1;
  96.             }
  97.             c.gridx = 0;
  98.             c.gridy = 4;
  99.             frame.add(errorDisplay, c);
  100.            
  101.         frame.pack() ; //pack the frame
  102.         frame.setResizable(false) ;
  103.     }
  104.  
  105.     public static void main(String[] args)
  106.     {
  107.         ToTGUI gui = new ToTGUI() ;
  108.     }
  109. }
  110.  
  111. class AddButtonListener implements ActionListener
  112. {
  113.     private ToTGUI obj ;
  114.  
  115.     public AddButtonListener(ToTGUI obj)
  116.     {
  117.         this.obj = obj ;
  118.     }
  119.  
  120.     public void actionPerformed(ActionEvent e)
  121.     {
  122.         try {
  123.             obj.records.incrementTotal(obj.itemTxt.getText(),Integer.parseInt(obj.numberTxt.getText())) ;
  124.             obj.records.generateEntryList() ;
  125.         } catch (ItemNotFoundException ex) {
  126.             obj.errorDisplay.setText(ex.getMessage()) ;
  127.         } catch (NumberFormatException ex) {
  128.             obj.errorDisplay.setText(ex.getMessage()) ;
  129.         } catch (IllegalArgumentException ex) {
  130.             obj.errorDisplay.setText(ex.getMessage()) ;
  131.         }
  132.         //System.out.println(obj.itemTxt.getText()) ;
  133.         catch (Exception ex) {
  134.             // TODO Auto-generated catch block
  135.             ex.printStackTrace();
  136.         }
  137.     }
  138. }
  139.  
  140. class SubtractButtonListener implements ActionListener
  141. {
  142.     private ToTGUI obj ;
  143.  
  144.     public SubtractButtonListener(ToTGUI obj)
  145.     {
  146.         this.obj = obj ;
  147.     }
  148.  
  149.     public void actionPerformed(ActionEvent e)
  150.     {
  151.         try {
  152.             obj.records.decrementTotal(obj.itemTxt.getText(),Integer.parseInt(obj.numberTxt.getText())) ;
  153.             obj.records.generateEntryList() ;
  154.         } catch(ItemNotFoundException ex) {
  155.             obj.errorDisplay.setText(ex.getMessage()) ;
  156.         } catch(NumberFormatException ex) {
  157.             obj.errorDisplay.setText(ex.getMessage()) ;
  158.         } catch (IllegalArgumentException ex) {
  159.             obj.errorDisplay.setText(ex.getMessage()) ;
  160.         } catch (Exception ex) {
  161.             // TODO Auto-generated catch block
  162.             ex.printStackTrace();
  163.         }
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment