public class ObjectClasses
1: import java.util.Scanner;
2: import java.awt.Color;
3: import javax.swing.JButton;
4: import javax.swing.JFrame;
5: import javax.swing.JLabel;
6: import java.awt.BorderLayout;
8: /**
9: * Just creating some objects of various classes
10: *
11: * @author Mark Young (A00000000)
12: */
13: public class ObjectClasses {
15: public static void main(String[] args) {
16: // declare some objects
17: String s1, s2;
18: Scanner kbd, str;
19: Animal rover, hammy;
20: Color c1, c2;
21: JButton okButton, cancelButton;
22: JFrame win;
24: // create some objects
25: kbd = new Scanner(System.in);
26: str = new Scanner("This is the String this Scanner scans!");
27: rover = new Animal(Animal.DOG, "Rover");
28: hammy = new Animal(Animal.HAMSTER, "Hammy");
29: c1 = new Color(255, 127, 0);
30: c2 = new Color(17, 79, 251);
31: okButton = new JButton("OK");
32: cancelButton = new JButton("Cancel");
33: s1 = "Strings are special!";
34: s2 = new String("But this works, too!");
36: // access some object data
37: System.out.printf("\"%s\".length() is %d.\n", s1, s1.length());
38: System.out.printf("\"%s\".charAt(0) is '%c'.\n", s1, s1.charAt(0));
39: System.out.printf("\"%s\".charAt(1) is '%c'.\n", s1, s1.charAt(1));
40: System.out.printf("\"%s\".charAt(2) is '%c'.\n", s1, s1.charAt(2));
41: System.out.printf("\"%s\".charAt(2) is '%c'.\n", s2, s2.charAt(2));
42: System.out.printf("%s's species is %s.\n",
43: rover.getName(), rover.getSpecies());
44: System.out.printf("%s's species is %s.\n",
45: hammy.getName(), hammy.getSpecies());
47: // NOTE: You do not need to understand ANY of the following code.
48: // It's just here so I can show you the colors and buttons I created.
50: // create a window to show some of the other objects
51: win = new JFrame("This window has two buttons"); // title of window
52: win.setSize(300, 200); // make window big enuf to see!
54: // change colors of the buttons
55: okButton.setBackground(c1); // orange background
56: cancelButton.setBackground(c2); // blue background
57: cancelButton.setForeground(new Color(255, 255, 0)); // yellow text
59: // make the cancel button exit the program (OK button does nothing!)
60: cancelButton.addActionListener(e -> System.exit(0));
62: // put the buttons and a label into the window
63: win.getContentPane().add(okButton, BorderLayout.NORTH); // at top
64: win.getContentPane().add(cancelButton, BorderLayout.SOUTH); // at bottom
65: win.getContentPane().add(new JLabel("And the buttons have colors!"),
66: BorderLayout.CENTER); // in the middle
68: // show the window we just created
69: win.setVisible(true);
71: // program won't end until user clicks cancel button
72: }
74: }