Overview of Java Swing JDialog form and

Swing definition:

There is a class cited AWT (Abstract Window Toolkit) in java, he can achieve a GUI (graphical user interface), but the AWT can not run on all platforms, Swing appear (in fact, enhancements AWT components) , but can not completely replace AWT components, both components need to appear in a graphical interface.

Swing Features:

The original AWT components from the java.awt package, when the java application program contains AWT components execute on different platforms, each platform GUI component display will be different, but the use of swing to develop applications on different platforms, you can style uniform GUI components.
Swing components are called "lightweight components", while the appearance of the insertion assemblyHe is completely dependent on java language, so you can cross-platform.

Common Swing components

Here Insert Picture Description

Common window:

JFrame form:

JFrame form is a container, which is the carrier Swing program various components, may be considered as a container JFrame Swing bearing assembly, a form can be created by extending java.swing.JFrame class. Because it is inherited from the JFrame class, so the window has to maximize, minimize, close buttons, etc.

Code:

import java.awt.*;//导入awt包
import javax.swing.*;//导入swing包
public class Main extends JFrame{//定义一个继承JFrame的类
	public void CreateJFrame(String title) {//定义一个CreateJFrame的方法
	JFrame jf=new JFrame(title);//实例化一个JFrame对象
	Container container=jf.getContentPane();//获取一个容器
	JLabel jl=new JLabel("这是一个Frame窗口");//创建一个JLable标签
	jl.setHorizontalAlignment(SwingConstants.CENTER);//使标签上的文字居中
	container.add(jl);//将标签添加到容器当中
	container.setBackground(Color.white);//设置容器的背景颜色
	jf.setVisible(true);//使窗口可视化
	jf.setSize(400	,550);//设置窗体的大小
	jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//设置窗体关闭方式
	}
	
	public static void main(String[] args) {
		new Main().CreateJFrame("mengtao");//在主方法调用CreateJFrame()方法
	}
	
	
}

FIG display

Here Insert Picture Description
note: (1) form Swing components typically associated with components and containers, so JFrame After creating an object, you need to call the getContentPane () method to convert the form into a container and then add the component layout manager set or container, if want to add a component to a container, it is necessary to use add container class () method is provided.
(2) where we can create a JFrame jf = new JFrame (); You can also create JFrame jf = new JFrame (String title ), there is an initial parameters can create invisible, but no title form, while the Senate You can create invisible but can have the title of the form,
(3) setSeize (the X-int, int the y-) method to set the size of the form,
(4) setDefaultOperation () method to set close a form:
DO_NOTHING_ON_CLOSE do nothing exit
DISPOSE_ON_CLOSE any and release form will automatically hide after registering listener objects
default window HIDE_ON_CLOSE hidden window closed
EXIT_ON_CLOSE the exit application default window close

Published 63 original articles · won praise 12 · views 4064

Guess you like

Origin blog.csdn.net/qq_45353823/article/details/102653723