Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. package Kennel;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.GridLayout;
  6. import java.awt.event.ActionListener;
  7. import javafx.event.ActionEvent;
  8. import javax.swing.ImageIcon;
  9. import javax.swing.JButton;
  10. import javax.swing.JLabel;
  11. import javax.swing.JPanel;
  12.  
  13. public class Animal extends JPanel implements ActionListener
  14. {
  15.  
  16. JLabel[] pen;
  17. JButton addDog;
  18. JButton addCat;
  19. JButton addGoat;
  20.  
  21. public Animal()
  22. {
  23. this.setBackground(Color.blue);
  24. this.setLayout(new GridLayout(1, 3));
  25.  
  26. pen = new JLabel[3];
  27. addDog = new JButton("Dog");
  28. addCat = new JButton("Cat");
  29. addGoat = new JButton("Goat");
  30.  
  31. Dimension btnSize = new Dimension(5, 5);
  32. addCat.setPreferredSize(btnSize);
  33. addDog.setPreferredSize(btnSize);
  34. addGoat.setPreferredSize(btnSize);
  35. this.add(addDog);
  36. this.add(addCat);
  37. this.add(addGoat);
  38.  
  39. for (int i = 0; i < pen.length; i++)
  40. {
  41. pen[i] = new JLabel();
  42. pen[i].setBackground(Color.LIGHT_GRAY);
  43. pen[i].setOpaque(true);
  44.  
  45. }
  46. for (int i = 0; i < 3; i++)
  47. {
  48. this.add(pen[i]);
  49. }
  50.  
  51. addDog.addActionListener((ActionListener) this);
  52. addCat.addActionListener((ActionListener) this);
  53. addGoat.addActionListener((ActionListener) this);
  54. }
  55.  
  56. public void actionPerformed(ActionEvent e)
  57. {
  58. if (e.getSource() == addDog)
  59. {
  60. ImageIcon dog = new ImageIcon("dog.jpg");
  61. pen[0].setIcon(dog);
  62.  
  63. } else if (e.getSource() == addCat)
  64. {
  65. ImageIcon cat = new ImageIcon("cat.jpg");
  66. pen[1].setIcon(cat);
  67. } else if (e.getSource() == addGoat)
  68. {
  69. ImageIcon goat = new ImageIcon("goat.jpg");
  70. pen[2].setIcon(goat);
  71. }
  72. }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement