[JavaSwing] HelloWorld

In this article we will learn some basic steps to create a JFrame – That's roughly the application framework.

Example 1: Create 1 frame 250×200 title “HelloWorld” appears in the coordinates (300, 200) on-screen.

package nguyenvanquan7826.helloworld;

import javax.swing.JFrame;

public class HelloWorld {
	public static void main(String[] args) {
		// create frame with title "HelloWorld"
		JFrame frame = new JFrame("HelloWorld");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// set Size and location frame
		frame.setSize(250, 200);
		frame.setLocation(300, 200);

		// display frame
		frame.setVisible(true);
	}
}

HelloWorld JavaSwing

We have 4 constructor 1 JFrame:

JFrame() : Create a new frame invisible
JFrame(GraphicsConfiguration qc) : Create a Frame is indicated by the display device GraphicsConfiguration and white title
JFrame(String title) : Create a new frame invisibile title specified
JFrame(String title, GraphicsConfiguration qc) : Create a Frame is indicated by the title and GraphicsConfiguration screen devices.

In the example above we use the constructor th 3 to set the title for the frame.

Ham setDefaultCloseOperation(int operation): Set the default action will occur when the user closes the JFrame. If this function is not used to set the default is HIDE_ON_CLOSE (1) – When closed frame will be hidden but not completely closed.
Other options include:
DO_NOTHING_ON_CLOSE (0) – otiose
DISPOSE_ON_CLOSE (2) – Just close this frame, Other related frames will not be closed.
EXIT_ON_CLOSE (3) – Close entire frame related to it.

The function setSize set size for the frame.
Ham setLocation placement of the frame appears on the screen. If you want the frame to appear in the middle of the screen we use the function setLocationRelativeTo(null)
Ham setVisible set the display to the frame. If you do not call or set value false for this function, the frame will not be displayed.

Example 2: Create 1 JFrame class inherited by JFrame.

package nguyenvanquan7826.helloworld;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class HelloWorld1 extends JFrame {
	public HelloWorld1() {
		// create frame with title "HelloWorld"
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("HelloWorld1");

		// set location
		setLocationRelativeTo(null);

		// add a JLabel
		add(new JLabel("HelloWorld"));

		// display frame
		pack();
		setVisible(true);
	}

	public static void main(String[] args) {
		new HelloWorld1();
	}
}

HelloWorld JavaSwing

In the example 2 This class inherits our JFrame class, it is 1 JFrame, so in initialization method HelloWorld1 we can call the method of JFrame.
Also in this example was used 2 method add() and pack(). The method helps us add one more object in frame (here is 1 JLabel), method helps frame pack size sufficient for the content of the frame even when we put the larger frame size.

Method setResizable(boolean resizable) used to set the frame can be resized or not.

Refer to: class JFrame