Schiarizzi

cookie clicker applet for java class

May 30th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. /**
  6. *
  7. */
  8. public class ClickerGUI extends JApplet
  9. implements ActionListener
  10. {
  11. private JTextField inputField;
  12. private int cookies = 0;
  13. private final String ADD = "Add";
  14. private final String CLEAR = "Clear";
  15. private JLabel prompt;
  16.  
  17.  
  18.  
  19. public void init()
  20. {
  21.  
  22. Container contentPane = getContentPane();
  23.  
  24. // set layout with spacing
  25. contentPane.setLayout(new BorderLayout(12,12));
  26.  
  27. // add the title label
  28. JLabel title = new JLabel("Joseph's cookie clicker and GUI Demonstartion.");
  29. contentPane.add(title, BorderLayout.NORTH);
  30.  
  31. // create the bottom part with the number of cookies
  32. JPanel centerPanel = new JPanel();
  33. JLabel prompt = new JLabel("Number of cookies: " + cookies);
  34. centerPanel.add(prompt);
  35. contentPane.add(centerPanel, BorderLayout.NORTH);
  36.  
  37.  
  38.  
  39. // make a panel for the buttons
  40. JPanel buttonPanel = new JPanel();
  41.  
  42. // add the buttons to the button panel
  43. JButton Add = new JButton();
  44. Add.setIcon(new ImageIcon("PerfectCookie.jpg"));
  45. Add.addActionListener(this);
  46.  
  47.  
  48. buttonPanel.add(Add);
  49.  
  50.  
  51.  
  52. JButton clear = new JButton(CLEAR);
  53. clear.addActionListener(this);
  54. buttonPanel.add(clear);
  55.  
  56. // add the buttons panel to the content pane
  57. contentPane.add(buttonPanel, BorderLayout.CENTER);
  58. }
  59.  
  60.  
  61. public void actionPerformed(ActionEvent evt)
  62. {
  63. String command = evt.getActionCommand();
  64. // if clear button pressed
  65. if(CLEAR.equals(command))
  66. cookies = 0;
  67. // uppercase button pressed
  68. else if(ADD.equals(command)){
  69. cookies++;
  70. prompt.setText("Number of cookies: " + cookies);
  71. }
  72.  
  73.  
  74. }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment