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);
}

  

  

最新文章

  1. Java中的经典算法之选择排序(SelectionSort)
  2. Lab1--关于安装JUnit的简要描述
  3. Symfony2 资料篇
  4. 在stackoverflow上使用markdown
  5. Codeforces Round #FF/#255 D DZY Loves Modification --贪心+优先队列
  6. linux-阿里云ECS部署PPTP(centos)
  7. POJ3690+位运算
  8. Hibernate中的query.setFirstResult(),query.setMaxResults();
  9. Linker Special Section Types
  10. Java面向对象程序设计--接口和内部类
  11. zoj 1586
  12. centos7上使用yum安装mysql
  13. 【linux驱动笔记】linux模块机制浅析
  14. hdu_5964:平行四边形
  15. Could not create pool connection. The DBMS driver exception was: null, message from server: "Host '192.168.XX.XX' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'
  16. JS里浮点数的运算
  17. FeatureTools
  18. Nvivo
  19. poj3481 splaytree模板题
  20. linux每日命令(32):gzip命令

热门文章

  1. 配置spring的监听器 让spring随项目的启动而启动
  2. dp回文
  3. 【CF715E】Complete the Permutations 第一类斯特林数
  4. Floyd-蒟蒻也能看懂的弗洛伊德算法(当然我是蒟蒻)
  5. nginx 提示the "ssl" directive is deprecated, use the "listen ... ssl" directive instead
  6. 算法-动态规划 Dynamic Programming--从菜鸟到老鸟
  7. Spring data jpa 调用存储过程处理返回参数及结果集
  8. docke 基本安装使用
  9. ASP.NET Web API 2 OData v4教程
  10. Flask-Web开发(第一部分)