[JAVA] [Swing] About JFrame class

(JFrame class hierarchy)

java.lang.Object →java.awt.Component →→java.awt.Container →→→java.awt.Window →→→→java.awt.Frame →→→→→javax.swing.JFrame

(Constructor of JFrame class)

JFrame()
//Build a new frame whose initial state is invisible
JFrame(GraphicsConfiguration gc)
//Create a Frame with the Graphics Configuration specified on the screen device and an empty title
JFrame(String title)
//Create a new frame that is initially invisible using the specified title
JFrame(String title, GraphicsConfiguration gc)
//Creates a JFrame with the specified title and the specified GraphicsConfiguration for the screen device.

→ Mainly used are the first and third constructors → Usage example

import javax.swing.JFrame;

class Sample{
  public static void main(String args[]){
    JFrame frame = new JFrame("MyTitle");
  }
}

(Method)

public void setVisible(boolean b)
//Show frame if argument is true
public void setTitle(String title)
//Set the title as an argument
public void setSize(int width, int height)
//Define frame width and height with arguments
public void setLocation(int x, int y)
//Define frame position with arguments
public void setLocationRelativeTo(null);
//The frame position is centered with null argument
public void setBounds(int x, int y, int width, int height)
//Define frame position and width height with arguments
public void setDefaultCloseOperation(int operation)
//JFrame as an argument.EXIT_ON_CLOSE and x button to end the frame

(Subclass that inherits JFrame class)

→ If you want to add your own method to the frame, define a subclass that inherits the JFrame class. → Initialize the frame size and position in the subclass constructor.

import javax.swing.JFrame;

//Inherit JFrame
class JSample5_1 extends JFrame{
  public static void main(String args[]){
    JSample5_1 frame = new JSample5_1("MyTitle");
    frame.setVisible(true);
  }

//constructor
  JSample5_1(String title){
    setTitle(title);
    setBounds(100, 100, 600, 400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

(Getting panes and components)

→ To add a component → Get a pane from the frame and add a component to the pane → What is defined in the frame for different purposes from the pane

(ContentPane) → Use getContentPane method of JFrame class to get ContentPane

import javax.swing.JFrame;
import java.awt.Container;

class Sample extends JFrame{
  public static void main(String args[]){
  }

  Sample(String title){
    Container contentPane = getContentPane();//Get Content Pane
  }
}

→ Add component to ContentPane → Use the add method defined in the Container class

public void add(Component comp, Object constraints)
//The first argument is the component object
//The second argument is the position to add

→ Layout manager: What to do with components → There are multiple types of layout managers

//Layout manager that specifies the position in the north, south, east, and west
BorderLayout.NORTH
BorderLayout.SOUTH
BorderLayout.EAST
BorderLayout.WEST
BorderLayout.CENTER

(Add button)

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.BorderLayout;

class Sample extends JFrame{
	public static void main(String[] args){
	JFrame frame = new JFrame();
    	JButton btn = new JButton("aaaa");

	Container contentPane = frame.getContentPane();
	contentPane.add(btn, BorderLayout.NORTH);
	
	frame.setBounds(100, 100, 600, 400);
	frame.setTitle("MyTitle");
    	frame.setVisible(true);
	}

}

(Output result) image.png

(Omission of acquisition of Content Pane)

→ If it is a new environment, Content Pane will be acquired automatically, so it can be omitted.

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.BorderLayout;

class Sample extends JFrame{
	public static void main(String[] args){
	JFrame frame = new JFrame();
    	JButton btn = new JButton("aaaa");

	frame.add(btn, BorderLayout.NORTH);
	
	frame.setBounds(100, 100, 600, 400);
	frame.setTitle("MyTitle");
    	frame.setVisible(true);
	}

}

(Output result) image.png

(Layout manager settings)

→ To set the layout manager in the frame, get the ContentPane and set it for the pane

public void setLayout​(LayoutManager mgr)
//Set up a layout manager
//Example of use
import javax.swing.JFrame;
import java.awt.FlowLayout;

class Sample extends JFrame{
  public static void main(String args[]){
  }

  Sample(String title){
    Container contentPane = getContentPane();
    contentPane.setLayout(new FlowLayout());
  }
}

public void setLayout​(LayoutManager manager)
//Set up a layout manager
//Example of use
import javax.swing.JFrame;
import java.awt.FlowLayout;

class Sample extends JFrame{
  public static void main(String args[]){
  }

  Sample(String title){
    setLayout(new FlowLayout());
  }
}
(Background color)
public void setBackground​(Color bg)
//Use the argument of Color class
//Example:「Color.GREEN」
(Icon image)
public void setIconImage(Image image)
//Instantiate the Imagination class and pass the image path as an argument

Recommended Posts

[Swing] About JFrame class
About class inheritance.
About Java class
About the StringBuilder class
About class division (Java)
About Java StringBuilder class
[Java] About Singleton Class
About Java String class
About sorting java.util.Arrays class
About java abstract class
Find out about class methods
About the File :: Stat class
About Java class loader types
About Java class variables class methods
A murmur about the utility class
About =