学习GUI编程第二天笔记

学习GUI编程第二天笔记

文本框TextArea学习笔记

 package test;
 
 import java.awt.*;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 
 public class TextArea01 {
     public static void main(String[] args) {
         //启动
         new MyFrame();
    }
 }
 class MyFrame extends Frame{
     public MyFrame() {
         TextField textField = new TextField();
         add(textField);
 
         //监听这个文本框输入的文字
         MyActionListener myactionListener = new MyActionListener();
         //按下Enter,就会触发这个输入框的事件
         textField.addActionListener(myactionListener);
         
         //设置替换编码,提高安全性
         textField.setEchoChar('*');
 
         setVisible(true);
         pack();
    }
     class MyActionListener implements ActionListener{
 
 
         @Override
         public void actionPerformed(ActionEvent e) {
             TextField field = (TextField)e.getSource();//获得一些资源,返回的对象(因为返回的对象为Object类,故可以向下转型为TextField)
             System.out.println(field.getText());//获得输入框的文本
             field.setText("");//""设置文本为空
        }
    }
 }

简易计算器

oop原则:组合,大于继承!

 class A extends B{
     
 }
 
 class A{
     public B b;
 }

初始版

 package test;
 
 import java.awt.*;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 
 public class EasyCalc {
     public static void main(String[] args) {
         //启动
         new Calculator();
    }
 }
 
 //计算器类
 class Calculator extends Frame{
     public Calculator(){
         Frame frame = new Frame();
         //三个文本框
         TextField num1 = new TextField(10);
         TextField num2 = new TextField(10);
         TextField num3 = new TextField(20);
 
         //一个Label
         Label label = new Label("+");
 
         //一个Button
         Button button = new Button("=");
         button.addActionListener(new MyCalcaculatorListenr(num1,num2,num3));
 
         //布局
         setLayout(new FlowLayout());
 
         add(num1);
         add(label);
         add(num2);
         add(button);
         add(num3);
 
         pack();
         setVisible(true);
    }
 
     //监听器类
     class MyCalcaculatorListenr implements ActionListener{
         //获取三个变量
         private TextField num1,num2,num3;
         public  MyCalcaculatorListenr(TextField num1,TextField num2,TextField num3){
             this.num1 = num1;
             this.num2 = num2;
             this.num3 = num3;
 
        }
 
         @Override
         public void actionPerformed(ActionEvent e) {
             //1.获得加数和被加数
             int n1 = Integer.parseInt(num1.getText());
             int n2 = Integer.parseInt(num2.getText());
 
             //加法
             num3.setText(""+(n1+n2));
 
             //清空前两个框
             num1.setText("");
             num2.setText("");
        }
    }
 }

完全改造为面向对象写法(减少继承的耦合性)

 public class EasyCalc02 {
     public static void main(String[] args) {
         //启动
         new Calculator1().loadFrame();
    }
 }
 
 
 //计算器类
 class Calculator1 extends Frame{
 
     //属性
     TextField num1,num2,num3;
 
     //方法
     public void loadFrame(){
         num1 = new TextField(10);
         num2 = new TextField(10);
         num3 = new TextField(20);
         Button button = new Button("=");
         Label label = new Label("+");
 
         setLayout(new FlowLayout());
         button.addActionListener(new MyCalcaculatorListenr(this));
 
         add(num1);
         add(label);
         add(num2);
         add(button);
         add(num3);
         pack();
         setVisible(true);
    }
 
     //监听器类
     class MyCalcaculatorListenr implements ActionListener{
 
         //获取计算器这个类,在一个类中组合另外一个类
         Calculator1 calculator1 = null;
         public  MyCalcaculatorListenr(Calculator1 calculator){
             //1.获得加数和被加数
             //2.加法
             //3.清空前两个框
             this.calculator1 = calculator;
        }
 
         @Override
         public void actionPerformed(ActionEvent e) {
             int n1 = Integer.parseInt(num1.getText());
             int n2 = Integer.parseInt(num2.getText());
 
             calculator1.num3.setText(""+(n1+n2));
             calculator1.num1.setText("");
             calculator1.num2.setText("");
        }
    }
 }

内部类

  • 更好的包装(可以直接调用类里的属性,无需传参)

 public class EasyCalc02 {
     public static void main(String[] args) {
         //启动
         new Calculator1().loadFrame();
    }
 }
 
 //计算器类
 class Calculator1 extends Frame{
 
     //属性
     TextField num1,num2,num3;
     
     //方法
     public void loadFrame(){
         num1 = new TextField(10);
         num2 = new TextField(10);
         num3 = new TextField(20);
         Button button = new Button("=");
         Label label = new Label("+");
 
         setLayout(new FlowLayout());
         button.addActionListener(new MyCalcaculatorListenr());
 
         add(num1);
         add(label);
         add(num2);
         add(button);
         add(num3);
         pack();
         setVisible(true);
    }
 
     //监听器类
     private class MyCalcaculatorListenr implements ActionListener{
         @Override
         public void actionPerformed(ActionEvent e) {
             int n1 = Integer.parseInt(num1.getText());
             int n2 = Integer.parseInt(num2.getText());
 
             num3.setText(""+(n1+n2));
             num1.setText("");
             num2.setText("");
        }
    }
 }

画笔

 public class PaintTest {
     public static void main(String[] args) {
         new Mypaint().loadFrame();
    }
 }
 class Mypaint extends Frame {
     public void loadFrame(){
         setBounds(200,200,600,500);
         setVisible(true);
    }
     //画笔
     public void paint(Graphics g){
         //画笔,需要颜色,画笔可以画画
         g.setColor(Color.RED);
         g.drawOval(100,100,50,50);
 
         g.setColor(Color.BLUE);
         g.drawRect(200,200,50,50);
 
         g.setColor(Color.BLACK);
    }
 }

 

 

posted @ 2020-06-13 00:16  雷神宙斯  阅读(90)  评论(0编辑  收藏  举报