javaGUI介紹簡單示例與接口回調

GUl編程( Graphic User Interface,圖形用戶接口)
GUI的各種元素,如:容器、按鈕、文本框等

1、Frame類
2、Button類
3、Panel類
5、Toolkit類
5、佈局管理器
6、基本組件
在這裏插入圖片描述
事件處理:
事件(Event): 用戶對組件的一個操作,稱之爲一個事件
事件源(Event source): 產生事件的對象
事件處理方法(Event handler): 能夠接收、解析和處理事件類對象、實現和用戶交互的方法,事件監聽器。

爲簡化編程,針對大多數事件監聽器接口定義了相應的實現類–事 件適配器類,在適配器類中,實現了相應監聽器接口中所有的方法,但不做任何事情。

代碼示例:
窗體的基本設置

package com.booy;

import java.awt.*;
import java.awt.event.*;

//繼承窗體類,實現單機事件接口
public class GuiDemo extends Frame implements ActionListener {
    public static void main(String[] args) {
        new GuiDemo();
    }
    //初始化窗體基本屬性
    public GuiDemo(){
        //窗體的寬高
        this.setSize(600,400);
        //窗體的title
        this.setTitle("這是一個GUI窗體");
        Button button = new Button("點擊領紅包");
        //給窗口添加單擊事件
        button.addActionListener(this);
        //創建一個線性佈局
        FlowLayout flowLayout = new FlowLayout();
        //線性佈局應用到窗體上
        this.setLayout(flowLayout);
        //給窗體添加關閉
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
        //按鈕應用到窗體上
        this.add(button);
        //顯示窗體
        this.setVisible(true);
    }
    //單機事件處理方法
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("恭喜你,獲得1000元紅包");
    }
}

事件處理窗口回調示例:
窗口2要爲窗口1設置值,窗口2內設置一個接口,併爲接口傳參,由窗口1實現窗口2的接口接收設置值
工作流程:通過主方法new Frame1打開Frame1窗體,點擊Frame1的按鈕,new Frame2打開 Frame2窗體,並將當前對象傳入Frame2的setMoneyListener,Frame2單擊時調用的爲實現這個接口的Frame1方法賦值。
Frame1:

package com.booy;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class Frame1 extends Frame implements Frame2.MoneyListener {
    private Button btn = new Button("購買:");
    private Label label = new Label("金額:");
    public Frame1(){
        setSize(400,200);
        //創建一個線性佈局
        FlowLayout flowLayout = new FlowLayout();
        //線性佈局應用到窗體上
        this.setLayout(flowLayout);
        this.add(btn);
        this.add(label);
        //btn添加單擊事件
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Frame2().setMoneyListener(Frame1.this);
            }
        });
        this.setVisible(true);
    }
    //實現Frame2接口
    @Override
    public void setMoney(String money) {
        label.setText(money);
    }

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

Frame2::

package com.booy;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Frame2 extends Frame {
    private Button btn = new Button("付款:");
    private TextField textField =new TextField(20);
    public Frame2(){
        setSize(400,200);
        //創建一個線性佈局
        FlowLayout flowLayout = new FlowLayout();
        //線性佈局應用到窗體上
        this.setLayout(flowLayout);
        this.add(textField);
        this.add(btn);

        //btn添加單擊事件
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //取出內容
                String money = textField.getText();
                //把值設置進接口
                moneyListener.setMoney(money);
            }
        });
        this.setVisible(true);
    }
    private MoneyListener moneyListener;
    public void setMoneyListener(MoneyListener moneyListener){
        this.moneyListener = moneyListener;
    }
    public static interface MoneyListener{
        public void setMoney(String money);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章