First acquaintance with JFrame, JPanel and JLabel

JFrame: java graphical interface design. It is a top-level framework class, equivalent to a container. Like a window frame, this frame can be embedded in several glass windows.

JPanel: it is also a container class, which is equivalent to a large glass window.

JLabel: it is some basic components. It must be placed in a container, similar to paper cutting. It must be placed on the surface of the window.

JFrame comes with a glass window. You can use the statement Container c=getContentPane(); Get the content pane.

You can also customize a new glass panel to replace the original built-in glass window,

The code is as follows:

JPanel jp=new JPanel();

this.setContentPane(jp);

 1. JFrame

The basic idea of java GUI program is based on JFrame. It is the object of window on the screen, which can be maximized, minimized and closed.

The three basic building blocks of Swing: label, button and text field; But you need a place to put them and want users to know how to deal with them. The JFrame class solves this problem -- it is a container that allows programmers to add other components to it, organize them, and present them to users. JFrame actually doesn't just let programmers put components in it and present them to users. Compared with its apparent simplicity, it is actually the most complex component in Swing package. In order to simplify components to the greatest extent, JFrame acts as a bridge between Swing components independent of the operating system and the operating system actually running these components. JFrame is registered in the form of window in the native operating system. After doing so, you can get many familiar operating system window features: minimize / maximize, change size and move.

Construction method

JFrame()

Construct a new form that is not initially visible.

JFrame(GraphicsConfiguration gc)

Create a Frame with the specified GraphicsConfiguration of the screen device and a blank title.

JFrame(String title)

Create a new, initially invisible Frame with the specified title.

JFrame(String title, GraphicsConfiguration gc)

Create a JFrame with a GraphicsConfiguration with a specified title and a specified screen device.

common method

protected void

addImpl( Component comp,  Object constraints, int index)

Adds the specified child Component.

protected JRootPane

createRootPane()

Called by the constructor to create the default rootPane.

protected void

frameInit()

Called by the constructor to initialize the JFrame appropriately.

AccessibleContext

getAccessibleContext()

Gets the AccessibleContext associated with this JFrame.

Container

getContentPane()

Returns the contentPane object for this form

int

getDefaultCloseOperation()

Returns the action performed when the user initiates "close" on this form.

Component

getGlassPane()

Returns the glassPane object for this form.

Graphics

getGraphics()

Create a graphics context for the component.

JMenuBar

getJMenuBar()

Returns the menu bar set on this form.

JLayeredPane

getLayeredPane()

Returns the layeredPane object for this form.

JRootPane

getRootPane()

Returns the rootPane object for this form.

TransferHandler

getTransferHandler()

Gets the transferHandler property.

static boolean

isDefaultLookAndFeelDecorated()

Returns true if the newly created JFrame should be Window decorated by the current appearance.

protected boolean

isRootPaneCheckingEnabled()

Returns whether calls to add and setLayout are forwarded to contentPane.

protected String

paramString()

Returns the string representation of this JFrame.

protected void

processWindowEvent(WindowEvent e)

Handles window events that occur on this component.

void

removeComponent comp)

Removes the specified component from the container.

void

repaint(long time, int x, int y, int width, int height)

Redraws the specified rectangular area of this component within time milliseconds.

void

setContentPane( Container contentPane)

Set the contentPane property.

void

setDefaultCloseOperation(int operation)

Set the default action when users initiate "close" on this form.

static void

setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated)

Provide a prompt about whether the newly created JFrame should have the Window decoration provided for it by the current appearance (such as border, widget closing Window, title, etc.).

void

setGlassPane( Component glassPane)

Set the glassPane property.

void

setIconImage( Image image)

Sets the image to display as an icon for this window.

void

setJMenuBar(JMenuBar menubar)

Set the menu bar for this form.

void

setLayeredPane(JLayeredPane layeredPane)

Set the layeredPane property.

void

setLayout(LayoutManager manager)

Set LayoutManager.

protected void

setRootPane(JRootPane root)

Set the rootPane property.

protected void

setRootPaneCheckingEnabled(boolean enabled)

Sets whether calls to add and setLayout are forwarded to contentPane.

void

setTransferHandler(TransferHandler newHandler)

Set the transferHandler property, which is the mechanism that supports data transfer to this component.

void

updateGraphics g)

Just call paint(g).

2.JPanel

Official JavaDocsApi: javax.swing.JPanel

purpose

Components are the basic elements of the application interface. Buttons, text boxes, progress bars, etc. are all components. Visualization components can be divided into container components and non container components. Non container components such as buttons and text boxes can be placed in JPanel, or even. To place several more JPanel components in JPanel, the top-level container is also a container component, and there can only be one top-level container component in each window application.

Construction method

establish

JPanel myPanel = new JPanel();

Construction method

JPanel() creates a new JPanel with double buffering and streaming layout

JPanel(Boolean isDoubleBuffered) creates a new Jpanel with FlowLayout and specified buffering policy

JPanel(LayoutManager layout) creates a new buffered Jpanel with the specified layout manager

JPanel(LayoutManager layout, boolean isDoubleBuffered) creates a new Jpanel with a specified layout manager and buffering policy

Use example

(the following program is from the online sample program, and JPanel can be embedded in JFrame)

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
class Test extends JFrame 
{ 
JPanel contPanel = new JPanel(); 
JPanel pane1 = new JPanel(); 
JPanel pane2 = new JPanel(); 
JButton next = new JButton("next"); 
JButton pre = new JButton("the previous"); 
public Test() 
{ 
setBounds(0,0,200,300); 
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

contPanel.add(pane1); 

pane1.add(next); 
pane2.add(pre); 
next.addActionListener(new ActionListener(){ 
public void actionPerformed(ActionEvent e) 
{ 
contPanel.remove(pane1); 
contPanel.add(pane2); 
contPanel.revalidate(); 
contPanel.repaint(); 
} 
}); 
pre.addActionListener(new ActionListener(){ 
public void actionPerformed(ActionEvent e) 
{ 
contPanel.remove(pane2); 
contPanel.add(pane1); 
contPanel.revalidate(); 
contPanel.repaint(); 
} 
}); 

setContentPane(contPanel); 
setVisible(true); 
} 
public static void main(String [] args) 
{ 
new Test(); 
} 
}

3.Jlabel

Methods commonly used in JLabel classes

1.public JLabel(String text, Icon icon, int horizontalAlignment)

Constructor. The Label marking element is configured in the central part of the display area, the text on the button surface is text, the icon is icon, the horizontal alignment is horizontalAlignment, and its value can be swingconstants LEFT,SwingConstants.CENTER and swingconstants RIGHT.

2.public JLable(String text, int horizontalAlignment)

Constructor. The Label marking element is configured in the center of the display area. The text on the button surface is text and the horizontal alignment is horizontalAlignment.

3.public JLabel(String text)

Constructor. The Label marking element is configured in the central part of the display area. The text on the button surface is text and aligned on the left.

4.public JLabel(Icon image, int horizontalAlignment)

Constructor. The Label marking element is configured in the center of the display area, the button icon is icon, and the horizontal alignment is horizontalAlignment.

5.public JLabel(Icon image)   

Constructor. The Label marking element is configured in the center of the display area, the button surface icon is icon, and the left side is aligned.

6.public JLabel() constructor. Align the Label in the center of the component configuration.

7.public Icon getIcon() returns icon icon object.

8.public String getText() returns the button face text string.

9. public void setIcon(Icon icon) sets icon icon icon object as icon.

10.public void  setText(String text)

GUI interface development demonstration

/**

 * Function: gui interface development demonstration

 */

package com.test1;

import java.awt.*;

import javax.swing.*;



public class Swing_1 extends JFrame{

//Define button components

JButton jb1 = null;

public static void main(String[] args) {

Swing_1 swing = new Swing_1();

}



//Constructor

public Swing_1(){

//JFrame is a top-level container

jb1 = new JButton("I'm the button");

//Sets the title of the form

this.setTitle("MyFirstSwing");

//Set size in pixels

this.setSize(200,200);

//Add JButton build

this.add(jb1);

//Set form position

this.setLocation(400, 200);

//Set to ensure that the jvm exits when the window is closed

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set form visibility

this.setVisible(true);

}

}

/*

 * Boundary layout manager

 * BorderLayout demonstration

 * 1.Inherit JFrame

 * 2.Define the components you need

 * 3.Create component

 * 4.Add build

 * 5.Set this on the form

 * 6.Display Form 

 */

package com.test1;

import java.awt.BorderLayout;

import javax.swing.*;

import javax.swing.*;



public class Border extends JFrame{

JButton jb1,jb2,jb3,jb4,jb5;



public static void main(String[] args) {

    Border border = new Border();

}



//Constructor

public Border(){

//Create component

jb1 = new JButton("in");

jb2 = new JButton("north");

jb3 = new JButton("east");

jb4 = new JButton("south");

jb5 = new JButton("west");



//Add individual components

//this. If add (JB1) does not specify a location, it will be overwritten

this.add(jb1, BorderLayout.CENTER);

this.add(jb2, BorderLayout.NORTH);

this.add(jb3, BorderLayout.EAST);

this.add(jb4, BorderLayout.SOUTH);

this.add(jb5, BorderLayout.WEST);



//Set form properties

this.setTitle("Boundary layout");

this.setSize(300,200);

this.setLocation(400,200);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);



}

}

 

 

/**

 * Function: flow layout case

 */
package com.test1;
import javax.swing.*;
import java.awt.*;

public class Flow extends JFrame{

//Define required components

JButton jb1,jb2,jb3,jb4,jb5,jb6;

public static void main(String[] args) {

Flow flow = new Flow();

}

//Constructor

public Flow(){

//Create component

jb1=new JButton("Guan Yu");

jb2=new JButton("Fei Zhang");

jb3=new JButton("Zhao Yun");

jb4=new JButton("ma chao");

jb5=new JButton("Huang Zhong");

jb6=new JButton("Wei Yan");


//add component

this.add(jb1);

this.add(jb2);

this.add(jb3);

this.add(jb4);

this.add(jb5);

this.add(jb6);


//Set up layout manager

this.setLayout(new FlowLayout(FlowLayout.LEFT));

//this.setLayout(new FlowLayout(FlowLayout.TRAILING));

//this.setLayout(new FlowLayout(FlowLayout.RIGHT) );

//this.setLayout(new FlowLayout(FlowLayout.CENTER));

//this.setLayout(new FlowLayout(FlowLayout.L));


//Set form properties

this.setTitle("Boundary layout");

this.setSize(300,200);

this.setLocation(400,200);

//Prevent users from changing the size of the form

this.setResizable(false);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


//display

this.setVisible(true);

}

}

​

 

/**

 * Function: grid layout

 */

package com.test1;

import java.awt.*;

import javax.swing.*;

public class Grid extends JFrame{

//Define components

int size = 9 ;

JButton jbs[] = new JButton[size];

public static void main(String[] args) {

//Create instance

Grid grid = new Grid();

}

public Grid(){

//Create component

for(int i = 0 ; i<9 ; i++){

jbs[i] = new JButton (String.valueOf(i));

}

//Set the grid layout (3,3) to represent 3 rows and 3 columns

this.setLayout(new GridLayout(3,3,10,10));

//add component

for(int i = 0 ; i < 9 ; i++){

this.add(jbs[i]);

}

//Set form properties

this.setTitle("Grid layout");

this.setSize(300,200);

this.setLocation(400,200);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Display Form 

this.setVisible(true);

}

}

 

Keywords: Java

Added by lpxxfaintxx on Mon, 03 Jan 2022 23:05:55 +0200