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;
7: import java.awt.HeadlessException;
9: /**
10: * Just creating some objects of various classes
11: *
12: * @author Mark Young (A00000000)
13: */
14: public class ObjectClasses {
16: public static void main(String[] args) {
17: // declare some objects
18: String s1, s2;
19: Scanner kbd, str;
20: Animal rover, hammy;
21: Color c1, c2;
22: JButton okButton, cancelButton;
23: JFrame win;
25: // create some objects
26: kbd = new Scanner(System.in);
27: str = new Scanner("This is the String this Scanner scans!");
28: rover = new Animal(Animal.DOG, "Rover");
29: hammy = new Animal(Animal.HAMSTER, "Hammy");
30: c1 = new Color(255, 127, 0);
31: c2 = new Color(17, 79, 251);
32: okButton = new JButton("OK");
33: cancelButton = new JButton("Cancel");
34: s1 = "Strings are special!";
35: s2 = new String("But this works, too!");
37: // access some object data
38: System.out.printf("\"%s\".length() is %d.\n", s1, s1.length());
39: System.out.printf("\"%s\".charAt(0) is '%c'.\n", s1, s1.charAt(0));
40: System.out.printf("\"%s\".charAt(1) is '%c'.\n", s1, s1.charAt(1));
41: System.out.printf("\"%s\".charAt(2) is '%c'.\n", s1, s1.charAt(2));
42: System.out.printf("\"%s\".charAt(2) is '%c'.\n", s2, s2.charAt(2));
43: System.out.printf("%s's species is %s.\n",
44: rover.getName(), rover.getSpecies());
45: System.out.printf("%s's species is %s.\n",
46: hammy.getName(), hammy.getSpecies());
48: // NOTE: You do not need to understand ANY of the following code.
49: // It's just here so I can show you the colors and buttons I created.
51: try {
52: // create a window to show some of the other objects
53: win = new JFrame("This window has two buttons"); // title of window
54: win.setSize(300, 200); // make window big enuf to see!
56: // change colors of the buttons
57: okButton.setBackground(c1); // orange background
58: cancelButton.setBackground(c2); // blue background
59: cancelButton.setForeground(new Color(255, 255, 0)); // yellow text
61: // make the cancel button exit the program (OK button does nothing!)
62: cancelButton.addActionListener(e -> System.exit(0));
64: // put the buttons and a label into the window
65: win.getContentPane().add(okButton, BorderLayout.NORTH); // at top
66: win.getContentPane().add(cancelButton, BorderLayout.SOUTH); // at bottom
67: win.getContentPane().add(new JLabel("And the buttons have colors!"),
68: BorderLayout.CENTER); // in the middle
70: // show the window we just created
71: win.setVisible(true);
73: // program won't end until user clicks cancel button
74: } catch (HeadlessException he) {
75: Utilities.printParagraph("I'm sorry. I have a demo of a window "
76: + "with buttons and labels and colours, but the system "
77: + "you're using can't show it. Try again using a GUI.");
78: }
79: }
81: }