java GUI Quick Start

There are two written in java GUI tool kits, respectively, AWT, Swing.
Swing is AWT expansion, Swing has extensive than AWT components and methods.
AWT and Swing can be used across platforms; AWT will vary from system platforms, UI style will change, Swing is not consistent after completion of design styles on all platforms.

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

A awt example

The following is an example of a 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();
    }
}

file
Creating a window to inherit Frame, will also inherit some methods of Frame;
Frame in:

  • super window title method may be provided;
  • setSize set window size,(width,hight)
  • setVisible Sets whether the display window, true show, false to hide

setLayout(new FlowLayout());A window for setting layout;
add(组件);for adding components to a window, such as buttons and the like.

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 window in the main method

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

effect
file

event

java, the event is divided into three areas described,
Event Source: event object
event handling: delegate event model
event listeners: Responsible for handling events

First monitor B A, setting contents listening, and set the response content.
Once a state change A itself to trigger C;
C performs D

Mother and son said, you finished your homework, let Mom and Dad give you cut an apple to eat.
Mom: Event Monitoring by
his son: Event Source
Action: finish the job
event: Dad cut an apple to his son

Mom son monitoring, control whether to complete the job. After setting the job is completed, the triggering event: Xiaoping Guo;
son to finish the job, status changes, the triggering event; the event started.

B Tell A simple terms, if A how, just do one thing C.

To achieve the flow of events

We continue to use the above window, click the button to achieve, change the window background color.

Implement an event

Introduced

import java.awt.event.ActionListener;

Implement an interface

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

The event registered to 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("宋体",Font.BOLD,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);
    }
}

There is a button event listener addActionListener, we work to be performed actionPerformedbindings into it.
When the condition occurs, it will trigger this work.

Mom Yeah, I do not know what they are spade.
C #, the event is not so much trouble, so do not need to BB. . .

Further, actionEvent, there are two common methods

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

layout

AWT, the main 6 layout.

  • FlowLayout flow layout
    default layout, controls, from top to bottom, left to right order.
  • BorderLayout layout edge
    automatic docking window edge, and with changes in the length and width of the form.
    file
  • GridLayout grid layout
    of the container into a number of rows and columns.
  • CradLayout card layout
    like a deck of cards, each card is a control, multiple controls stack up, you can only view one.
  • GridBagLayout layout grid block
  • null coordinate positioning layout using

Create a form, and set the flow layout

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

Frame inherited form, the method can be set using setLayout layout.

Assembly method

As shown in GUI controls are common controls inheritance relationship diagram.
file

Assembly into the container (Container) and controls.
Container is divided into form (Window) and panel (Panel).

Component is the parent class of all components, Component commonly used method is 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

Text box (TextField) can only display a line of text, text fields (TextArea) you can display, edit multiple lines of text.

The constructor of the text box below

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

Constructor following text field

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

There are common methods of text controls

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

Text field (the TextArea), the conventional method has the following two

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

There are two common text box events
ActionEvent event
triggered when you press the Enter key in the text box
registration: addActionListener()Interface: ActionListener
Method:public void actionPerformed(ActionEvent e)

TextEvent events
change operations (add, modify, delete) the text input component data
registration: addTextListener()
Interface: TextListener
Method:public void textValueChanged(TextEvent e)

Guess you like

Origin www.cnblogs.com/whuanle/p/12539621.html