English Cathedral Frames and Windows (Using the API)

Cathedral window

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.

  1. import javax.swing.JButton;
  2. import javax.swing.JFrame;
  3. import java.awt.FlowLayout;
  4. // The files above are imported from the java API (Application Programming interface).
  5. // To access the parent file you can create a subclass that "extends" the Jframe class.
  6. // This is a primary advantage of Java's Object Oriented programming language.
  7. // In your code you can access thousands of useful classes that have already
  8. // been developed for your use.
  9. Java codepublic class CathedralFrame extends JFrame {
  10. // Below is a constructor with methods
  11. // from the imported files.
  12. // This code creates a frame object that is visible.
  13. public CathedralFrame () {
  14. setTitle("Soon this frame will allow you to open a cathedral window.");
  15. setLayout(new FlowLayout());
  16. setDefaultCloseOperation(EXIT_ON_CLOSE);
  17. add(new JButton("In the future, click here for cathedral window."));
  18. setSize(600,400);
  19. setVisible(true);
  20. }
  21. }

This code calls the CathedralFrame class.

Java code

  1. public class UseCathedralFrame {
  2. public static void main(String[] args) {
  3. new CathedralFrame();
  4. }
  5. }

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");

  1. import javax.imageio.ImageIO;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.IOException;
  7. public class CathedralWindow {
  8. public static void main(String[] args) throws IOException {
  9. File file = new File("C:\\Javamike\\windows.png");
  10. BufferedImage bufferedImage = ImageIO.read(file);
  11. ImageIcon imageIcon = new ImageIcon(bufferedImage);
  12. JFrame jFrame = new JFrame();
  13. jFrame.setLayout(new FlowLayout());
  14. jFrame.setSize(1000, 1000);
  15. JLabel jLabel = new JLabel();
  16. jLabel.setIcon(imageIcon);
  17. jFrame.add(jLabel);
  18. jFrame.setVisible(true);
  19. jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  20. }
  21. }

Java code

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:

Java code

Java code

< Cathedrals (Enums) Regions (Passing Objects) >