Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Kennel;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.GridLayout;
- import java.awt.event.ActionListener;
- import javafx.event.ActionEvent;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- public class Animal extends JPanel implements ActionListener
- {
- JLabel[] pen;
- JButton addDog;
- JButton addCat;
- JButton addGoat;
- public Animal()
- {
- this.setBackground(Color.blue);
- this.setLayout(new GridLayout(1, 3));
- pen = new JLabel[3];
- addDog = new JButton("Dog");
- addCat = new JButton("Cat");
- addGoat = new JButton("Goat");
- Dimension btnSize = new Dimension(5, 5);
- addCat.setPreferredSize(btnSize);
- addDog.setPreferredSize(btnSize);
- addGoat.setPreferredSize(btnSize);
- this.add(addDog);
- this.add(addCat);
- this.add(addGoat);
- for (int i = 0; i < pen.length; i++)
- {
- pen[i] = new JLabel();
- pen[i].setBackground(Color.LIGHT_GRAY);
- pen[i].setOpaque(true);
- }
- for (int i = 0; i < 3; i++)
- {
- this.add(pen[i]);
- }
- addDog.addActionListener((ActionListener) this);
- addCat.addActionListener((ActionListener) this);
- addGoat.addActionListener((ActionListener) this);
- }
- public void actionPerformed(ActionEvent e)
- {
- if (e.getSource() == addDog)
- {
- ImageIcon dog = new ImageIcon("dog.jpg");
- pen[0].setIcon(dog);
- } else if (e.getSource() == addCat)
- {
- ImageIcon cat = new ImageIcon("cat.jpg");
- pen[1].setIcon(cat);
- } else if (e.getSource() == addGoat)
- {
- ImageIcon goat = new ImageIcon("goat.jpg");
- pen[2].setIcon(goat);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement