Part 2: Using the JFrame Class In this section, you will add some code to your m
ID: 3903199 • Letter: P
Question
Part 2: Using the JFrame Class
In this section, you will add some code to your main method that creates an empty window on the screen. Locate the JFrame class in the Java API documentation. Then follow these steps to use that class in your program.
1.Add the required statement to the top of your class.
2.In your method, create a variable of type object. Then, assign a new object to that variable. Use the constructor that will allow you to set the window title to ?
3.Next, you need to instruct the that it is to be visible on the screen.
a.The method for doing this has been inherited by from another class. Find this method and use it in your code to make the visible.
b.Add a comment above that line of code to indicate which class the method was inherited from.
4.Now we need to set the size (width and height) of the window.
a.Locate the required method. (It has also been inherited.)
b.To call this method, you will need an instance of the class.
i.Add a new import statement to your code so that you have access to the class
ii.Then create a variable and assign it a new that is 300x300
c.Use the object you created to set the size of your window. Be sure to import any Java packages that you need to utilize this object.
5.Finally, we want the program to exit when the user clicks the red close button on the top-left corner of the window frame. has a particular method for specifying this behavior. Locate and use this method on your window to get the desired behavior. Hint: You will need to pass in a particular value that has been declared in the class. Access the value via that class like so, THE_VALUE.
here you can find lab materials :
https://www.dropbox.com/s/mo4y2p61bawof4o/lab11%202.zip?dl=0
Explanation / Answer
//////////////////////////// CODE ////////////////////////////////
import java.awt.Dimension;
import javax.swing.JFrame;
public class JavaAPILabMain {
public static void main(String[] args) {
JFrame myFrame = new JFrame("Fresh Green Beans");
//Inherited from java.awt.Frame which inherits it from java.awt.Window
myFrame.setVisible(true);
Dimension d = new Dimension(300, 300);
myFrame.setSize(d);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
////////////////////////////////////////////////////////////////////////////////////////////
/*
1. The required import statement is "import javax.swing.JFrame;"
2. We did this by passing it as an argument to the Constructor, done in the line 'JFrame myFrame = new JFrame("Fresh Green Beans");'
3. a) Done in line "myFrame.setVisible(true);"
b) Inherited from java.awt.Window
4. a) The required method is setSize()
b) i. This imports the Dimension class "import java.awt.Dimension;"
ii. This creates a Dimension object of 300x300 and assigns it to d "Dimension d = new Dimension(300, 300);"
c) This sets size of window to Dimension assigned to d "myFrame.setSize(d);"
5. This does it "myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);"
*/