Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // dev notes:
- // 1. Fix the issue with the layout of the cookie button. V1.1
- // 1. Add a cookie factory for a cost of 100 cookie, make 1 cookie per second. V1.2
- import java.awt.BorderLayout;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.BorderFactory;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- public class GUI implements ActionListener {
- private int count = 0;
- private JLabel label;
- private JFrame frame;
- private JPanel panel;
- public GUI() {
- frame = new JFrame();
- JButton buttonA = new JButton(new ImageIcon("C:\\Users\\LENOVO\\eclipse-workspace\\OurFirstGUI\\cookie.png"));
- buttonA.addActionListener(this);
- label = new JLabel("Number of cookies: 0");
- label = new JLabel("V1.0 By Someone on Internet");
- panel = new JPanel();
- panel.setBorder(BorderFactory.createEmptyBorder(10, 30, 30, 30));
- panel.setLayout(new GridLayout(0, 1));
- panel.add(buttonA);
- panel.add(label);
- frame.add(panel, BorderLayout.CENTER);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setTitle("Cookie Clicker");
- frame.pack();
- frame.setVisible(true);
- }
- public static void main(String[] args) {
- new GUI();
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- count++;
- label.setText("Number of cookies: "+ count );
- }
- }
- // Sources for this project (to make my french teacher happy):
- // Youtube Video of Alex Lee: https://www.youtube.com/watch?v=5o3fMLPY7qY
- // to place a cookie as the image for the button: https://stackoverflow.com/questions/4801386/how-do-i-add-an-image-to-a-jbutton
- // https://stackoverflow.com/questions/5657469/how-do-i-go-about-adding-an-image-into-a-java-project-with-eclipse
- // https://stackoverflow.com/questions/38046684/importing-images-into-eclipse
- // https://stackoverflow.com/questions/13594139/swing-change-image-with-a-button
- // to import an image on eclipse as a source
- // The famous cookie by Ryan Morrison JSY: https://pixabay.com/illustrations/cookies-food-dessert-biscuits-3551296/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement