Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- /**
- *
- */
- public class ClickerGUI extends JApplet
- implements ActionListener
- {
- private JTextField inputField;
- private int cookies = 0;
- private final String ADD = "Add";
- private final String CLEAR = "Clear";
- private JLabel prompt;
- public void init()
- {
- Container contentPane = getContentPane();
- // set layout with spacing
- contentPane.setLayout(new BorderLayout(12,12));
- // add the title label
- JLabel title = new JLabel("Joseph's cookie clicker and GUI Demonstartion.");
- contentPane.add(title, BorderLayout.NORTH);
- // create the bottom part with the number of cookies
- JPanel centerPanel = new JPanel();
- JLabel prompt = new JLabel("Number of cookies: " + cookies);
- centerPanel.add(prompt);
- contentPane.add(centerPanel, BorderLayout.NORTH);
- // make a panel for the buttons
- JPanel buttonPanel = new JPanel();
- // add the buttons to the button panel
- JButton Add = new JButton();
- Add.setIcon(new ImageIcon("PerfectCookie.jpg"));
- Add.addActionListener(this);
- buttonPanel.add(Add);
- JButton clear = new JButton(CLEAR);
- clear.addActionListener(this);
- buttonPanel.add(clear);
- // add the buttons panel to the content pane
- contentPane.add(buttonPanel, BorderLayout.CENTER);
- }
- public void actionPerformed(ActionEvent evt)
- {
- String command = evt.getActionCommand();
- // if clear button pressed
- if(CLEAR.equals(command))
- cookies = 0;
- // uppercase button pressed
- else if(ADD.equals(command)){
- cookies++;
- prompt.setText("Number of cookies: " + cookies);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment