Advertisement
Guest User

Untitled

a guest
Dec 16th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. import javax.swing.*; // a package containing JPanel, JButton and JLabel
  2. import java.awt.event.*; // a package containing ActionListener and ActionEvent
  3. class Animal{
  4. public String name;
  5. public Animal(){
  6. name="";
  7. }
  8.  
  9. public Animal(String i){
  10. name=i;
  11. }
  12. public String Onomatopoeia(){
  13. return "There is no onomatopoeia for "+name;
  14. }
  15. }
  16. class Dog extends Animal{
  17. public Dog (String i){
  18. super(i);
  19. }
  20. public String Onomatopoeia(){
  21. return "The onomatopoeia for "+name+" is ARF ARF";
  22. }
  23. }
  24. class Lion extends Animal{
  25. public Lion (String i){
  26. super(i);
  27. }
  28. public String Onomatopoeia(){
  29. return "The onomatopoeia for "+name+" is ROAR";
  30. }
  31. }
  32. class Frog extends Animal{
  33. public Frog (String i){
  34. super(i);
  35. }
  36. public String Onomatopoeia(){
  37. return"The onomatopoeia for "+name+" e CROAK";
  38. }
  39. }
  40. class Maltesian extends Dog{
  41. public Maltesian (String i){
  42. super(i);
  43. }
  44.  
  45. public String description(){
  46. return "Maltesian "+ name +" is the sweetest dog in th world";
  47. }
  48. }
  49.  
  50. public class inputZOO2 extends JPanel implements ActionListener {
  51. static JFrame f;
  52. private JLabel message, message2;
  53. JTextField tf;
  54. JButton btn;
  55. Animal[] animal=new Animal[6];
  56. String[] petStrings = new String[6];
  57. JComboBox petList;
  58. JComboBox classes;
  59. String[] class_name={"Animal","Dog","Frog","Lion","Maltesian"};
  60. int count=0;
  61. public inputZOO2() {
  62. setLayout(null);
  63. tf=new JTextField("");
  64. btn=new JButton("Input");
  65. classes= new JComboBox(class_name);
  66. btn.addActionListener(this);
  67. message = new JLabel("");
  68. message2 = new JLabel("");
  69.  
  70. message.setBounds(5,105,380,30);
  71. message2.setBounds(5,120,380,30);
  72. tf.setBounds(5,5,150,25);
  73. classes.setBounds(5,35,150,25);
  74. btn.setBounds(5,75,150,20);
  75. add(tf);
  76. add(btn);
  77. add(classes);
  78.  
  79. add(message);
  80. add(message2);
  81.  
  82. }
  83.  
  84. public void actionPerformed(ActionEvent e) {
  85. if (e.getSource()== btn){
  86. String name=tf.getText();
  87.  
  88. String type = (String)classes.getSelectedItem();
  89. if (name.length()>0){
  90. if(type.equals("Animal")){
  91. Object[] options = {"Yes, I do", "No, it was a mistake!"};
  92. int n = JOptionPane.showOptionDialog(f, "Do you realy want to input an animal of the class \"Animal\"?", "Question", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
  93. null,
  94. options,
  95. options[0]);
  96.  
  97. if (n==0){
  98. animal[count]=new Animal(name);
  99. count++;
  100. message.setText("You have created an object - " + type+" with the name "+name);
  101. message2.setText("There are " +count +" animals in the array.");
  102. JOptionPane.showMessageDialog(f, "You have created an object of the class " + type+" with the name "+name,"Object created", JOptionPane.INFORMATION_MESSAGE);
  103. tf.setText("");
  104. }
  105. }
  106. else {
  107. if(type.equals("Dog")){
  108. animal[count]=new Dog(name);
  109. }
  110. if(type.equals("Lion")){
  111. animal[count]=new Lion(name);
  112. }
  113. if(type.equals("Frog")){
  114. animal[count]=new Frog(name);
  115. }
  116. if(type.equals("Maltesian")){
  117. animal[count]=new Maltesian(name);
  118. }
  119. count++;
  120. message.setText("You've created an object - "+type+" with the name "+name);
  121. message2.setText("There are " +count +" animals in the array.");
  122.  
  123. // display an informative dialog box demonstrating the successful input of an object in an array
  124.  
  125. tf.setText("");
  126. }
  127. }
  128. else{
  129.  
  130. // display a dialog box stating that an error has occurred and that the name of the animal needs to be input
  131. JOptionPane.showMessageDialog(f, "Enter the name of the anlimal and choose its type.", "Object created", JOptionPane.ERROR_MESSAGE);
  132. }
  133.  
  134. }
  135.  
  136. }
  137.  
  138. public static void main(String[] arg) {
  139. f = new JFrame("Animals");
  140. f.getContentPane().add(new inputZOO2());
  141. f.setSize(350,200);
  142. f.setLocation(300,300);
  143. f.setVisible(true);
  144. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  145. f.show();
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement