JAVA-GUI AWT中的三种布局 事件监听 输入框 标签 用三种不同的代码形式搞定计算器2021.2.8

面板 panel

可以看作一个空间,但是不能单独存在,要放在

使用适配器模式,写关闭窗口办法

//监听窗口 System.exit(e) ,
this.addWindowListener(new WindowAdapter() {
    @Override
    //窗口关闭时需要做的事情
    public void windowClosing(WindowEvent e) {
        super.windowClosing(e);
        System.exit(0);

    }
});

panel的相关内容

要把面板放在窗口里,所以必须先建立一个窗口

package com.xiucai;

import java.awt.*;

public class textpanel {
    public static void main(String[] args) {
        //先实例化一个窗口
        Myframe myframe = new Myframe(200, 200, 200, 200, Color.RED);
        myframe.setLayout(null);
        //布局的概念
        Panel panel = new Panel();
        //设置布局
        panel.setLayout(null);
        //坐标,相对于frame,东西都是放在面板里的

       panel.setBounds(100,100,100,100);
        panel.setBackground(Color.GREEN);
   // 把这个面板放到窗口里
        myframe.add(panel);
      //  panel.setVisible(true);
    }
}

三种管理布局

setLayout

  • 流失布局:从左到右
  • 上下结构的
  • 表格布局
FlowLayout,流式布局
myframe.setLayout(new FlowLayout(FlowLayout.LEFT));

对象.setLayout(布局方式(所在位置))

东西南北中 BorderLayout

image-20210208170622547

关键代码:

myframe.setLayout(new BorderLayout());
myframe.add(east,BorderLayout.EAST);

对象.add(组件名,组件所在位置)

表格布局 GridLayout

自动排布

小练习

image-20210208182019496

package com.xiucai;
//使用表格跟流式布局完成的 
import java.awt.*;

public class testdomo1 {
    public static void main(String[] args) {
        //新建一个窗口
        Myframe myframe = new Myframe(300, 300, 300, 300, Color.white);
        //先分为上下两个部分
        myframe.setLayout(new GridLayout(2,1));
        // 上面部分
        Panel panel_up = new Panel();
        panel_up.setBackground(Color.red);
        panel_up.setLayout(new FlowLayout());
        myframe.add(panel_up);//加载面板

        //加载左右中结构部分
        Button left = new Button("lefr");
        Button right = new Button("right");
        Panel mid = new Panel(); //中部结构
        mid.setLayout(new GridLayout(2,1));
        mid.setBackground(Color.BLACK);
        panel_up.add(left);
        panel_up.add(mid);//中间
        panel_up.add(right);

       //往整个上面中间结构里边添加按钮
        Button mid_up = new Button("up");
        Button mid_down = new Button("down");
        mid.add(mid_up);
        mid.add(mid_down);

        //下面部分

 // 下面三个大布局完成
        Panel panel_down = new Panel();
        myframe.add(panel_down);
        panel_down.setLayout(new FlowLayout());
        panel_down.setBackground(Color.pink);
        Panel panel_down_left = new Panel();
        Panel panel_down_mid = new Panel();
        panel_down_mid.setLayout(new GridLayout(2,2));
        Panel panel_down_right = new Panel();
       panel_down.add(panel_down_left);
        panel_down.add(panel_down_mid);
        panel_down.add(panel_down_right);
        panel_down.setBackground(Color.BLACK);
        panel_down.setBackground(Color.BLUE);
        panel_down.setBackground(Color.white);
        //往里边增加按钮
        Button down_left = new Button("down_left");
        Button down_rigth = new Button("down_rigth");
        Button button1 = new Button("1");
        Button button2 = new Button("2");
        Button button3 = new Button("3");
        Button button4 = new Button("4");
        panel_down_left.add(down_left);
        panel_down_right.add(down_rigth);
        panel_down_mid.add(button1);
        panel_down_mid.add(button2);
        panel_down_mid.add(button3);
        panel_down_mid.add(button4);

        myframe.pack();

    }
}

事件监听

当事件发生的时候我们干什么。

先声明一个监听类,里面说明 该事件执行以后做什么

class MyActionListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("aaa");
        
    }
  MyActionListener myActionListener = new MyActionListener();
组件对象.addActionListener(myActionListener);

多个按钮使用一个监听事件类

需要使用两个命令

button.setActionCommand("1"); //设置活动标记,在组件里边设置
class MyActionListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(e.getActionCommand());
 } //在事件类里边设置,传一个可以区别判别的参数。

输入框

TextField textField=new TextField();
frame.add(textField); //基本显示代码
MyTextactionListnner myTextactionListnner = new MyTextactionListnner();
//按下回车就是
//谁知替换编码
textField.setEchoChar('*');//设置一个输入的时候不显示文本的内容
textField.addActionListener(myTextactionListnner);//事件

事件类:实现在文本框输入文字以后按下回车,文字消失

class MyTextactionListnner implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        TextField textField= (TextField) e.getSource();//获得一些资源,返回一个对象
        System.out.println(textField.getText());//获得输入框中的文本
        textField.setText("");
    }
}

标签 label

Label label=new Label("+");

不使用组合的计算器,面向过程

package com.xiucai;

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

public class TextCalc {
    public static void main(String[] args) {
        new Calculator(200,200,200,200,Color.white);

    }
}

class Calculator extends Myframe {
    public Calculator(int x, int y, int w, int h, Color color) throws HeadlessException {
        super(x, y, w, h, color);
        //定义组件 三个文本框 一个标签 一个按钮
        TextField textField = new TextField(10);//最大能写几个字符数
        TextField textFiel2 = new TextField(10);//最大能写几个字符数
        TextField textFiel3 = new TextField(10);//最大能写几个字符数
        Button button = new Button("=");
        Label label=new Label("+");
        //布局
        this.setLayout(new FlowLayout());
        add(textField);

        add(label);
        add(textFiel2);
        add(button);
        add(textFiel3);
        pack();
        //设置监听
        MyListenerAction myListenerAction = new MyListenerAction(textField, textFiel2, textFiel3);
     button.addActionListener(myListenerAction);



    };

}

class MyListenerAction implements ActionListener {
    // private TextField t1,t2, t3;
    //把上边的代码替换掉,用组合的方式进行计算器对象的声明
    Calculator calculator = null;
    
  //构造函数实现,不用进行传入的对象甄别,直接调用对象即可
    public MyListenerAction(TextField t1, TextField t2, TextField t3) {
        this.t1 = t1;
        this.t2 = t2;
        this.t3 = t3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Integer num1;
        Integer num2;

        num1 = Integer.parseInt(t1.getText());
        num2= Integer.parseInt(t2.getText());
      t3.setText(String.valueOf(num1+num2));
       t1.setText("");
       t2.setText("");

    }
}

使用组合的计算器

package com.xiucai;

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

public class TextCalc {
    public static void main(String[] args) {
        new Calculator(200,200,200,200,Color.white).loadFrame();

    }
}

class Calculator extends Myframe {

    //属性
    TextField textField,textFiel2, textFiel3;


    //方法
    public void loadFrame() {
         textField = new TextField(10);//最大能写几个字符数
         textFiel2 = new TextField(10);//最大能写几个字符数
         textFiel3 = new TextField(10);//最大能写几个字符数
        Button button = new Button("=");
        Label label=new Label("+");
        //布局
        this.setLayout(new FlowLayout());
        add(textField);

        add(label);
        add(textFiel2);
        add(button);
        add(textFiel3);
        pack();
        //设置监听
        MyListenerAction myListenerAction = new MyListenerAction(this);
        button.addActionListener(myListenerAction);

    }

    public Calculator(int x, int y, int w, int h, Color color) throws HeadlessException {
        super(x, y, w, h, color);
        //定义组件 三个文本框 一个标签 一个按钮



    };

}

class MyListenerAction implements ActionListener {
    // private TextField t1,t2, t3;
    //把上边的代码替换掉,用组合的方式进行计算器对象的声明
    Calculator calculator = null;

  //构造函数实现,不用进行传入的对象甄别,直接调用对象即可
    public MyListenerAction(Calculator calculator) {
        this.calculator = calculator;

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int num1, num2, num3;
        num1 = Integer.parseInt(calculator.textField.getText());
        num2 = Integer.parseInt(calculator.textFiel2.getText());
        num3 = num1 + num2;
        calculator.textFiel3.setText(String.valueOf(num3));
        calculator.textField.setText("");
        calculator.textFiel2.setText("");
    }
}

组合的思想就是传入对象。不再是传入参数

内部类的计算器

内部类最大的好处,就是可以畅通无阻的访问外部类的属性跟方法。

package com.xiucai;

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

public class TextCalc {
    public static void maAin(String[] args) {
        new Calculator(200,200,200,200,Color.white).loadFrame();

    }
}

class Calculator extends Myframe {

    //属性
    TextField textField,textFiel2, textFiel3;


    //方法
    public void loadFrame() {
         textField = new TextField(10);//最大能写几个字符数
         textFiel2 = new TextField(10);//最大能写几个字符数
         textFiel3 = new TextField(10);//最大能写几个字符数
        Button button = new Button("=");
        Label label=new Label("+");
        //布局
        this.setLayout(new FlowLayout());
        add(textField);

        add(label);
        add(textFiel2);
        add(button);
        add(textFiel3);
        pack();
        //设置监听
        MyListenerAction myListenerAction = new MyListenerAction();
        button.addActionListener(myListenerAction);

    }

    public Calculator(int x, int y, int w, int h, Color color) throws HeadlessException {
        super(x, y, w, h, color);
        //定义组件 三个文本框 一个标签 一个按钮



    };

    class MyListenerAction implements ActionListener {
        // private TextField t1,t2, t3;
        //把上边的代码替换掉,用组合的方式进行计算器对象的声明


        //构造函数实现,不用进行传入的对象甄别,直接调用对象即可


        @Override
        public void actionPerformed(ActionEvent e) {
            int num1, num2, num3;
            num1 = Integer.parseInt(textField.getText());
            num2 = Integer.parseInt(textFiel2.getText());
            num3 = num1 + num2;
            textFiel3.setText(String.valueOf(num3));
            textField.setText("");
           textFiel2.setText("");
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秀才大大

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值