Computer Programming Objects and GUIs Review
Classes and Objects
An object is non-primative data type. Object types are generally capitalized in Java, like JFrame, String, and Color.
To declare an object variable is to tell Java what type of data your variable will hold, and what its name will be.
For instance: Robot rob; declares a Robot variable named rob.
To instantiate an object variable is to give it a value by calling a constructor method.
For instance: rob = new Robot(); instantiates my rob variable by calling a Robot constructor method.
If you want, you can declare and instantiate a variable on the same line, like
Robot rob = new Robot();
The name of a constructor method is always the same as the name of the class. In other words, the JFrame class's
constructor is named JFrame(), the Robot class's constructor is named Robot(), and the JFileChooser class's
constructor is named JFileChooser().
A default constructor is a constructor method that doesn't take in any input parameters. Most but not all classes in
the API have a default constructor that builds a generic, basic instance of the class. An example of a class that does not
have a default constructor is Scanner…to make a Scanner object, you have to say what it will be scanning as an input
parameter for its constructor.
GUI Stuff
GUI stands for Graphical User Interface
The Color class is used to create a color. The Font class is used to create a font.
The JFrame class is described in class as a display container, whose purpose (like an easel or picture frame) is to hold
other components so they can be seen.
The JPanel class is like a canvas or panel that you put all of your other work on before placing it on the frame to be
displayed.
A package is a group of related classes.
All the GUI classes are located in the swing package that can be imported at javax.swing.*;
Almost all the GUI classes in the swing package start with a "J": JFrame, JButton, JOptionPane, JPanel, etc.
When you construct a JFrame, it is initially invisible, so you need to call a non-static method to set its size, set its
default close operation, and make it visible:
JFrame frame = new JFrame("My Frame");
//declares and instantiates a JFrame variable
frame.setSize(200,200);
//sets the frame size to 200 by 200 pixels
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//make program exit when frame is closed
frame.setVisible(true);
//make the frame show up
Other Classes
The Scanner class is used to read in int, double, and String values from either the command line or from files. It has
the non-static methods next(), nextInt(), nextDouble(), and hasNext(). To make the scanner read from the
command line, when you instantiate it give the constructor System.in as its input parameter. To make it read in from
a file, give the constructor a File variable as its input parameter.
JFileChooser is a swing class that allows the user to select a File
Robot is a class that can be used to simulate user interaction like mouse clicks, key presses, and mouse movement
File is a class representing a file
Color is a class that generates a color that can be used with GUIs.
Reminders
A static method is called by saying ClassName.methodName(inputParameters); like Math.pow(2,5);
A non-static method, also called an object method, is called by saying
variableName.methodName(inputParameters); like frame.setVisible(true); or rob.mouseMove(500, 500);
A constructor method is called by saying new ClassName(inputParameters); like JFrame frame = new JFrame();
To find out what kind of constructors and methods a class has, you can go to the Java API and look up the class
If you are given a method signature from the API (showing whether the method is non-static or static, what its return
type is, what its name is, and what it takes in as input parameters), you should be able to use that method without any
problems or difficulties, even if you have never used that method before.
© Copyright 2025 Paperzz