javaswing_JavaSwing

javaswing

Java Swing is a GUI Framework that contains a set of classes to provide more powerful and flexible GUI components than AWT. Swing provides the look and feel of modern Java GUI. Swing library is an official Java GUI tool kit released by Sun Microsystems. It is used to create graphical user interface with Java.

Java Swing是一个GUI框架,其中包含一组类,以提供比AWT更强大,更灵活的GUI组件 Swing提供了现代Java GUI的外观。 Swing库是Sun Microsystems发行的官方Java GUI工具套件。 它用于使用Java创建图形用户界面。

Swing classes are defined in javax.swing package and its sub-packages.

Swing类在javax.swing包及其子包中定义。

摇摆功能 (Features of Swing)

  1. Platform Independent

    平台无关

  2. Customizable

    可订制

  3. Extensible

    可扩展的

  4. Configurable

    可配置的

  5. Lightweight

    轻巧的

  6. Rich Controls

    丰富的控件

  7. Pluggable Look and Feel

    可插拔外观

Swing和JFC (Swing and JFC)

JFC is an abbreviation for Java Foundation classes which encompass a group of features for building Graphical User Interfaces(GUI) and adding rich graphical functionalities and interactivity to Java applications. Java Swing is a part of Java Foundation Classes (JFC).

JFC是Java Foundation类的缩写,它包含用于构建图形用户界面(GUI)并向Java应用程序添加丰富的图形功能和交互性的一组功能。 Java Swing是Java基础类(JFC)的一部分。

JFC的特点 (Features of JFC)

  • Swing GUI components.

    Swing GUI组件。

  • Look and Feel support.

    外观支持。

  • Java 2D.

    Java 2D。

AWT和Swing层次结构 (AWT and Swing Hierarchy)

javaswing_JavaSwing_第1张图片

Swing班简介 (Introduction to Swing Classes)

JPanel : JPanel is Swing's version of AWT class Panel and uses the same default layout, FlowLayout. JPanel is descended directly from JComponent.

JPanel: JPanel是Swing的AWT类Panel的版本,并使用相同的默认布局FlowLayout。 JPanel直接来自JComponent。

JFrame : JFrame is Swing's version of Frame and is descended directly from Frame class. The component which is added to the Frame, is refered as its Content.

JFrame: JFrame是Swing的Frame版本,直接来自Frame类。 添加到Frame的组件称为其内容。

JWindow : This is Swing's version of Window and has descended directly from Window class. Like Window it uses BorderLayout by default.

JWindow:这是Swing的Window版本,直接来自Window类。 与Window一样,它默认使用BorderLayout。

JLabel : JLabel has descended from JComponent, and is used to create text labels.

JLabel: JLabel是JComponent的后代,用于创建文本标签。

JButton : JButton class provides the functioning of push button. JButton allows an icon, string or both associated with a button.

JButton: JButton类提供按钮的功能。 JButton允许图标,字符串或两者都与按钮关联。

JTextField : JTextFields allow editing of a single line of text.

JTextField: JTextFields允许编辑一行文本。

创建一个JFrame (Creating a JFrame)

There are two ways to create a JFrame Window.

有两种创建JFrame窗口的方法。

  1. By instantiating JFrame class.

    通过实例化JFrame类。

  2. By extending JFrame class.

    通过扩展JFrame类。

通过实例化JFrame类创建JFrame窗口 (Creating JFrame window by Instantiating JFrame class)

import javax.swing.*;  //importing swing package
import javax.swing.*;  //importing swing package
import java.awt.*;     //importing awt package
public class First
{
	JFrame jf;
	public First() {
		jf = new JFrame("MyWindow");            //Creating a JFrame with name MyWindow
		JButton btn = new JButton("Say Hello");//Creating a Button named Say Hello
		jf.add(btn);                            //adding button to frame
		jf.setLayout(new FlowLayout());        //setting layout using FlowLayout object
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      //setting close  operation.
		jf.setSize(400, 400);                   //setting size
		jf.setVisible(true);                    //setting frame visibility
	}
	public static void main(String[] args)
	{
		new First();
	}
}
javaswing_JavaSwing_第2张图片

通过扩展JFrame类创建JFrame窗口 (Creating JFrame window by extending JFrame class)

import javax.swing.*; //importing swing package
import java.awt.*; //importing awt package
public class Second extends JFrame
{
	public Second()
	{
		setTitle("MyWindow"); //setting title of frame as  MyWindow
		JLabel lb = new JLabel("Welcome to My Second Window");//Creating a label named Welcome to My Second Window
		add(lb);                        //adding label to frame.
		setLayout(new FlowLayout());    //setting layout using FlowLayout object.
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //setting close operation.
		setSize(400, 400);              //setting size
		setVisible(true);               //setting frame visibility
	}

	public static void main(String[] args)
	{
		new Second();
	}
}
javaswing_JavaSwing_第3张图片

要记住的要点 (Points To Remember)

  1. Import the javax.swing and java.awt package to use the classes and methods of Swing.

    导入javax.swing和java.awt包以使用Swing的类和方法。

  2. While creating a frame (either by instantiating or extending Frame class), following two attributes are must for visibility of the frame:

    在创建框架时(通过实例化或扩展Frame类),必须具有以下两个属性才能使框架可见:

    setSize(int width, int height);
    setVisible(true);
  3. When you create objects of other components like Buttons, TextFields, etc. Then you need to add it to the frame by using the method - add(Component's Object);

    当创建其他组件的对象(如Button,TextFields等)时,则需要使用以下方法将其添加到框架中: add(Component's Object);

  4. You can add the following method also for resizing the frame - setResizable(true);

    您还可以添加以下方法来调整框架的大小-setResizable(true);

翻译自: https://www.studytonight.com/java/java-swing.php

javaswing

你可能感兴趣的:(javaswing_JavaSwing)