English Cathedral Frames and Windows (Using the API)
The Norman and English artisans created magnificent and beautiful cathedral windows and window frames.
It would be a difficult task for you to create all of the code necessary to display a Java frame or window. Therefore, Java programmers have already created object classes to perform the task for you. Thousands of predesigned classes await you in the Java API.
Here is the code to access the parent class: Jframe. Notice that you are using the same keyword extends that you used to establish a link to your own parent file in the subclasses training module. The import statements at the top are used to access both classes and methods that exist in the API.
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import java.awt.FlowLayout;
- // The files above are imported from the java API (Application Programming interface).
- // To access the parent file you can create a subclass that "extends" the Jframe class.
- // This is a primary advantage of Java's Object Oriented programming language.
- // In your code you can access thousands of useful classes that have already
- // been developed for your use.
- public class CathedralFrame extends JFrame {
- // Below is a constructor with methods
- // from the imported files.
- // This code creates a frame object that is visible.
- public CathedralFrame () {
- setTitle("Soon this frame will allow you to open a cathedral window.");
- setLayout(new FlowLayout());
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- add(new JButton("In the future, click here for cathedral window."));
- setSize(600,400);
- setVisible(true);
- }
- }
This code calls the CathedralFrame class.
- public class UseCathedralFrame {
- public static void main(String[] args) {
- new CathedralFrame();
- }
- }
Here is code that creates a window; again using classes and methods stored in the Java API (Application Programming Interface.). You will need to copy and paste the above image into your folder. You can also use other images as long as you use the correct suffix: jpg or png.
Note: In this line you will need to change the code to your folder and file. File file = new File("C:\\Javamike\\windows.png");
- import javax.imageio.ImageIO;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- public class CathedralWindow {
- public static void main(String[] args) throws IOException {
- File file = new File("C:\\Javamike\\windows.png");
- BufferedImage bufferedImage = ImageIO.read(file);
- ImageIcon imageIcon = new ImageIcon(bufferedImage);
- JFrame jFrame = new JFrame();
- jFrame.setLayout(new FlowLayout());
- jFrame.setSize(1000, 1000);
- JLabel jLabel = new JLabel();
- jLabel.setIcon(imageIcon);
- jFrame.add(jLabel);
- jFrame.setVisible(true);
- jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- }
Use Save As to save the code files to your folder. Then compile the code files in the Command prompt window. Run the UseCathedralFrame.java class and then the CathedralWindow.java class.
Your output should look like this: