Java GUI quick start

There are two toolkits for writing GUI in Java, AWT and swing. Swing is an extension of AWT. Swing has more components and methods than AWT. Both AWT and swing can be used across platforms; The UI style of AWT will change with different system platforms, while swing will not. After the design is completed, the style will be the same under all platforms.

import java.awt.*;
import javax.swing.*;

An AWT example

The following is an example window

import java.awt.*;
public class MyFrame extends Frame {
    public  MyFrame(){
        super("测试");
        setSize(400,200);
        setVisible(true);
    }
    public  static  void Main(String args[]){
        new MyFrame();
    }
}

setLayout(new FlowLayout()); Used to set the window layout; Add (component); Used to add components to a window, such as buttons.

Let's add components to the window

import java.awt.*;
public class MyFrame extends Frame {
    public  MyFrame(){
        super("测试");

        setLayout(new FlowLayout());

        Button btn=new Button("Button");
        Font f=new Font("宋体",Font.BOLD,28);
        btn.setFont(f);
        add(btn);

        setSize(400,200);
        setVisible(true);
    }

Start the window in the main method

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

effect

event

In Java, events are described in three aspects: event source: the object where the event occurs, event processing: delegate event processing model, event listener: responsible for processing events

First, B listens to a, sets the listening content, and sets the response content. Once the state of a itself changes, C is triggered; C executive D

Mom told her son that when you finish your homework, mom will let dad peel an apple for you. Mom: event monitor son: event source action: finish your homework event: Dad peels an apple for his son

The mother monitors her son to see if he has finished his homework. After the setting operation is completed, the event is triggered: apple peeling; When the son finishes his homework, the status changes and an event is triggered; The event starts execution.

In short, B tells a to do one thing C if a is OK.

Implement event flow

We continue to use the above window to change the background color of the window when the button is clicked.

Implement an event

introduce

import java.awt.event.ActionListener;

Implementation interface

class  Test implements  ActionListener{   
    public void actionPerformed(ActionEvent actionEvent) {
        System.out.println("执行工作");
    }

Register events on the control

package com.company;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyFrame extends Frame implements ActionListener {
    public  MyFrame(){
        super("测试");

        setLayout(new FlowLayout());

        Button btn=new Button("Button");
        Font f=new Font("宋体",28);
        btn.setFont(f);
        btn.addActionListener(this::actionPerformed);
        add(btn);
        setSize(400,200);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent actionEvent) {
        System.out.println("执行工作");
        setBackground(Color.BLUE);
    }
}

The button has an event listener addactionlistener, in which we bind the work to be performed actionperformed. This job is triggered when a condition occurs.

Mom, I don't even know what I'm talking about. In C #, the event is not so troublesome, and there is no need for such BB...

In addition, ActionEvent has two common methods

        actionEvent.getActionCommand();		// 获取对象名称
        actionEvent.getSource()();			// 获取源对象

layout

In AWT, there are mainly 6 layout modes.

Create a form and set the streaming layout

public class MyFrame extends Frame {
    public  MyFrame(){
        super("测试");
        setLayout(new FlowLayout());
		}
		}

In a form that inherits the frame, you can use the setlayout method to set the layout.

Component method

The figure below is a control relationship inheritance diagram commonly used in Gui controls.

Components are divided into containers and controls. Containers are divided into windows and panels.

Component is the parent class of all components. The common methods of component are as follows

Component类(抽象类)主要方法 
Color getBackground() : 获取部件的背景色 
Font getFont() : 获取部件的显示字体 
Graphics getGraphics(): 获取部件的Graphics属性对象
void setBackground(Color c) : 设置部件的背景
void setEnabled(boolean b) : 是否让部件功能有效 
void setFont(Font f) : 设置部件的显示字体 
void setSize(int width,int height) : 设置部件大小 
void setVisible(boolean b) : 设置部件是否可见 
void setForeground(Color c) : 设置部件的前景色 
Tookit getToolkit() : 取得图形部件的工具集(Toolkit) 
void requestFocus() : 让部件得到焦点 
void add(PopupMenu popup) :给部件加入弹出菜单

Common controls

text

Only one line of text can be displayed in the textfield, and multiple lines of text can be displayed and edited in the textarea.

The constructor of the text box is as follows

TextField():构造一个单行文本输入框。 
TextField(int):指定长度的单行文本输入框。 
TextField(String):指定初始内容的单行文本输入框。
TextField(String,int):指定长度、指定初始内容。

The constructor for the text field is as follows

TextArea( ):构造一个文本域。 
TextArea(int,int):构造一个指定长度和宽度的文本域。 TextArea(String):构造一个显示指定文字的文本域。
TextArea(String,int,int):按指定长度、宽度和默认值构造文本域。

Common methods of text controls are

void setEchoChar(‘*’) 设置回显字符 
String getText( ) :获取输入框中的数据 
void setText(String s) :往输入框写入数据 
boolean isEditable( ):判断输入框是否可编辑。 
void select(int start,int end):选定由开始和结 束位置指定的文本。 void selectAll( ):选定所有文本。 

In the textarea, there are also two common methods

append(String s):将字符串添加到文本域的末尾 
insert(String s,int index):将字符串插入到文本域的指定位置

There are two common events in the text box. The ActionEvent event causes registration when pressing enter in the text box: addactionlistener() interface: actionlistener method: public void actionperformed (ActionEvent E)

Textevent event data change operation (add, modify, delete) registration of text input part: addtextlistener() interface: textlistener method: public void textvaluechanged (textevent E)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>