java按钮接口_java----GUI和接口回调

GUI:

图形用户接口

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

public class Demo {

public static void main(String[] args) {

Mygson();

}

private static void Mygson() {

new MyFrame();

}

}

class MyFrame extends Frame implements Action {

public MyFrame(){

//设置宽高

this.setSize(600,400);

//设置窗体的标题

this.setTitle("GUI");

//创建按钮

Button button = new Button("点击");

//按钮添加点击事件

button.addActionListener(this);

//给窗体添加点击事件

this.addWindowListener(new WindowAdapter() {

@Override

public void windowClosing(WindowEvent e) {

super.windowClosing(e);

System.exit(0);

}

});

//创建一个线性布局

FlowLayout flowLayout = new FlowLayout();

//把布局应用到窗体上

this.setLayout(flowLayout);

//把按钮添加窗体上

this.add(button);

this.setVisible(true);

}

@Override

public Object getValue(String key) {

return null;

}

@Override

public void putValue(String key, Object value) {

}

//单机事件处理方法;

@Override

public void actionPerformed(ActionEvent e) {

System.out.println("点击事件触发");

}

}

事件处理:

接口回调

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

public class Demo {

public static void main(String[] args) {

Mygson();

}

private static void Mygson() {

new MyFrame();

}

}

class MyFrame extends Frame implements MoneySetListen{

private Label label = new Label("金额");

private Button button = new Button("购买");

public MyFrame(){

this.setSize(400,200);

FlowLayout flowLayout = new FlowLayout();

this.setLayout(flowLayout);

this.setTitle("GUI");

this.add(label);

this.add(button);

this.setVisible(true);

button.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

new MyFrame2().setMoneySetListen(MyFrame.this);

}

});

this.addWindowListener(new WindowAdapter() {

@Override

public void windowClosing(WindowEvent e) {

super.windowClosing(e);

System.exit(0);

}

});

}

@Override

public void setMoney(String money) {

label.setText(money);

}

}

class MyFrame2 extends Frame{

private MoneySetListen moneySetListen;

private TextField text = new TextField(20);

private Button button = new Button("确认");

public MyFrame2(){

this.setSize(400,200);

FlowLayout flowLayout = new FlowLayout();

this.setLayout(flowLayout);

this.setTitle("GUI");

this.add(text);

this.add(button);

this.setVisible(true);

button.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

String money = text.getText();

moneySetListen.setMoney(money);

}

});

this.addWindowListener(new WindowAdapter() {

@Override

public void windowClosing(WindowEvent e) {

super.windowClosing(e);

System.exit(0);

}

});

}

//MyFrame 继承了MoneySetListen 接口,则可以用接口来接受this,然后调用this的setMoney方法;

public void setMoneySetListen(MoneySetListen moneySetListen){

this.moneySetListen = moneySetListen;

}

}

interface MoneySetListen{

public void setMoney(String money);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值