Your First Frame
Home ] Up ] [ Your First Frame ] Sizing a Frame ] Positioning a Frame ] Exiting a Program ] A Centered Frame ]

 

 

Here's some code to show you how to create a very simple frame, using the Swing JFrame class:

package swingExamples;

import javax.swing.JFrame;

public class FirstFrame1 extends JFrame
{
  public FirstFrame1()
  {
    setTitle("FirstFrame");
    setSize(300, 200);
  }
  
  public static void main(String[] args)
  {
    FirstFrame1 frame = new FirstFrame1();
    frame.setVisible(true);
  }
}
Or we can move the main entry point out into its own class:
package swingExamples;

import javax.swing.JFrame;

class FirstFrame2 extends JFrame
{
  public FirstFrame2()
  {
    setTitle("FirstFrame");
    setSize(300, 200);
  }
}

public class FirstTest
{
  public static void main(String[] args)
  {
    FirstFrame2 frame = new FirstFrame2();
    frame.setVisible(true);
  }
}

Both of which produce the wonderfully informative window below:

 

 

This page was last modified on 02 October, 2007