Problem 31

A Frame with a Panel with Writing on It

Type in the following code, and get it to compile. Run it, and see what it does.

import javax.swing.*;
import java.awt.*;

public class FrameWithPanelWithWriting {
    public static void main(String[] args) {
        MyCustomFrame f = new MyCustomFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

class MyCustomFrame extends JFrame {
    public MyCustomFrame() {
        setTitle("My Awesome Custom Frame(window)!");
        setSize(800, 600);
        setLocation(100, 200);

        MyCustomPanel panel = new MyCustomPanel();
        Container cp = getContentPane();
        cp.add(panel);
    }
}

class MyCustomPanel extends JPanel {
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawString("Hi", 75, 100);
    }
}

◄ 30: A Boring Window 32: Twenty Questions... well, actually just Two ►



Adapted from ProgrammingByDoing.com
©2013 Graham Mitchell

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License.