Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*; // a package containing JPanel, JButton and JLabel
- import java.awt.event.*; // a package containing ActionListener and ActionEvent
- class Animal{
- public String name;
- public Animal(){
- name="";
- }
- public Animal(String i){
- name=i;
- }
- public String Onomatopoeia(){
- return "There is no onomatopoeia for "+name;
- }
- }
- class Dog extends Animal{
- public Dog (String i){
- super(i);
- }
- public String Onomatopoeia(){
- return "The onomatopoeia for "+name+" is ARF ARF";
- }
- }
- class Lion extends Animal{
- public Lion (String i){
- super(i);
- }
- public String Onomatopoeia(){
- return "The onomatopoeia for "+name+" is ROAR";
- }
- }
- class Frog extends Animal{
- public Frog (String i){
- super(i);
- }
- public String Onomatopoeia(){
- return"The onomatopoeia for "+name+" e CROAK";
- }
- }
- class Maltesian extends Dog{
- public Maltesian (String i){
- super(i);
- }
- public String description(){
- return "Maltesian "+ name +" is the sweetest dog in th world";
- }
- }
- public class inputZOO2 extends JPanel implements ActionListener {
- static JFrame f;
- private JLabel message, message2;
- JTextField tf;
- JButton btn;
- Animal[] animal=new Animal[6];
- String[] petStrings = new String[6];
- JComboBox petList;
- JComboBox classes;
- String[] class_name={"Animal","Dog","Frog","Lion","Maltesian"};
- int count=0;
- public inputZOO2() {
- setLayout(null);
- tf=new JTextField("");
- btn=new JButton("Input");
- classes= new JComboBox(class_name);
- btn.addActionListener(this);
- message = new JLabel("");
- message2 = new JLabel("");
- message.setBounds(5,105,380,30);
- message2.setBounds(5,120,380,30);
- tf.setBounds(5,5,150,25);
- classes.setBounds(5,35,150,25);
- btn.setBounds(5,75,150,20);
- add(tf);
- add(btn);
- add(classes);
- add(message);
- add(message2);
- }
- public void actionPerformed(ActionEvent e) {
- if (e.getSource()== btn){
- String name=tf.getText();
- String type = (String)classes.getSelectedItem();
- if (name.length()>0){
- if(type.equals("Animal")){
- Object[] options = {"Yes, I do", "No, it was a mistake!"};
- 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,
- null,
- options,
- options[0]);
- if (n==0){
- animal[count]=new Animal(name);
- count++;
- message.setText("You have created an object - " + type+" with the name "+name);
- message2.setText("There are " +count +" animals in the array.");
- JOptionPane.showMessageDialog(f, "You have created an object of the class " + type+" with the name "+name,"Object created", JOptionPane.INFORMATION_MESSAGE);
- tf.setText("");
- }
- }
- else {
- if(type.equals("Dog")){
- animal[count]=new Dog(name);
- }
- if(type.equals("Lion")){
- animal[count]=new Lion(name);
- }
- if(type.equals("Frog")){
- animal[count]=new Frog(name);
- }
- if(type.equals("Maltesian")){
- animal[count]=new Maltesian(name);
- }
- count++;
- message.setText("You've created an object - "+type+" with the name "+name);
- message2.setText("There are " +count +" animals in the array.");
- // display an informative dialog box demonstrating the successful input of an object in an array
- tf.setText("");
- }
- }
- else{
- // display a dialog box stating that an error has occurred and that the name of the animal needs to be input
- JOptionPane.showMessageDialog(f, "Enter the name of the anlimal and choose its type.", "Object created", JOptionPane.ERROR_MESSAGE);
- }
- }
- }
- public static void main(String[] arg) {
- f = new JFrame("Animals");
- f.getContentPane().add(new inputZOO2());
- f.setSize(350,200);
- f.setLocation(300,300);
- f.setVisible(true);
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- f.show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement