首页 > 编程语言 >java_day20~22

java_day20~22

时间:2022-10-31 11:55:23浏览次数:44  
标签:java 22 void day20 class add new TextField public

Java基础

GUI编程

核心技术:Swing、AWT

现在GUI并不流行 因为其界面不美观、需要依赖jre环境

事件监听

意为:当某个事情发生的时候,要干什么

/**
 * 事件监听
 * @author xue
 */
public class Demo5 {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button but = new Button("but1");

        //按钮事件 addActionListener需要一个ActionListener对象 需要构造出来
        MyActionListener myActionListener = new MyActionListener();
        but.addActionListener(myActionListener);

        frame.add(but,BorderLayout.CENTER);
        frame.setSize(500,500);
        frame.setVisible(true);

        windowClose(frame);






    }

    //关闭窗口
    //你猜这里为什么用private
    private static void windowClose(Frame frame) {
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

class MyActionListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("nihao");
    }
}
/**
 * 实现一个监听事件多个按钮使用
 * @author xue
 */

public class Demo6 {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button but1 = new Button("start");
        Button but2 = new Button("stop");

        but2.setActionCommand("bye");//原有的标签信息将会被覆盖为bye
        MyMonitor myMonitor = new MyMonitor();
        but1.addActionListener(myMonitor);
        but2.addActionListener(myMonitor);

        frame.add(but1,BorderLayout.EAST);
        frame.add(but2,BorderLayout.WEST);
        frame.setVisible(true);
        frame.setSize(500,500);



    }
}

class MyMonitor implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        //可以获取按钮的信息
        System.out.println(e.getActionCommand());

        if (e.getActionCommand().equals("start")) {
            System.out.println("是可以开始的时候了");
        }
        if (e.getActionCommand().equals("stop")) {
            System.out.println("是该结束了");
        }
    }
}

输入框

public class Demo7 {
    public static void main(String[] args) {
        MyFrame2 myFrame2 = new MyFrame2();
        
    }
}

class MyFrame2 extends Frame {
    public MyFrame2() {
        TextField tf = new TextField();
        add(tf);

        MyActionListener2 myActionListener2 = new MyActionListener2();
        //监听文本框输入的内容 enter为触发条件
        tf.addActionListener(myActionListener2);

        tf.setEchoChar('*');//设置输入的内容形式替换 输入内容全显示为* 但后台获取的实际输入的内容
        setVisible(true);
        pack();//窗口自适应
    }
}

class MyActionListener2 implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        TextField tf = (TextField) e.getSource();
        System.out.println(tf.getText());//获取输入框中的所有内容包括转义字符
        tf.setText(""); //回车即清空内容
    }
}

练习简易计算器

public class Caulator {
    public static void main(String[] args) {
        new NewFrame();

    }
}

class NewFrame extends Frame{
    public NewFrame() {
        //三个文本框
        TextField tf1 = new TextField(10);
        TextField tf2 = new TextField(10);
        TextField tf3 = new TextField(20);
        //一个按钮
        Button but = new Button("=");
        //一个标签
        Label label = new Label("+");

        //设置为流布局
        setLayout(new FlowLayout());
        add(tf1);
        add(label);
        add(tf2);
        add(but);
        add(tf3);
        setVisible(true);
        setSize(600,200);
        //按钮的监听事件
        but.addActionListener(new NewActionListener(tf1,tf2,tf3));

        //关闭窗口
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                 System.exit(0);
            }
        });


    }
}

class NewActionListener implements ActionListener{

    //通过构造方法传递文本属性
    private TextField tf1,tf2,tf3;

    public NewActionListener(TextField tf1,TextField tf2,TextField tf3){
        this.tf1=tf1;
        this.tf2=tf2;
        this.tf3=tf3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //获取文本的值
       int num1 = Integer.parseInt(tf1.getText());
       int num2 = Integer.parseInt(tf2.getText());


       tf3.setText(""+(num1+num2));

       //清空文本框
        tf1.setText("");
        tf2.setText("");

    }
}



/**
	优化1.1 减少代码用于
	使用组合
*/
public class Caulator {
    public static void main(String[] args) {
        new NewFrame().loadCaul();

    }
}

class NewFrame extends Frame{
    
    //对象-属性
    TextField tf1,tf2,tf3;

    //对象-方法
    public void loadCaul(){
        //三个文本框
        tf1 = new TextField(10);
        tf2 = new TextField(10);
        tf3 = new TextField(20);
        //一个按钮
        Button but = new Button("=");
        //一个标签
        Label label = new Label("+");

        //设置为流布局
        setLayout(new FlowLayout());
        add(tf1);
        add(label);
        add(tf2);
        add(but);
        add(tf3);
        setVisible(true);
        setSize(600,200);
        //按钮的监听事件
        but.addActionListener(new NewActionListener(this));

        //关闭窗口
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }
}

class NewActionListener implements ActionListener{

    //通过创建NewFrame对象获取其属性
    NewFrame nf = null;

    
    public NewActionListener(NewFrame nf){
        this.nf=nf;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //获取文本的值
       int num1 = Integer.parseInt(nf.tf1.getText());
       int num2 = Integer.parseInt(nf.tf2.getText());


       nf.tf3.setText(""+(num1+num2));

       //清空文本框
        nf.tf1.setText("");
        nf.tf2.setText("");

    }
}

/**
*优化1.2 内部类减少代码量
* 内部类的最大好处是可以直接使用外部类的属性和方法
*/
public class Caulator {
    public static void main(String[] args) {
        new NewFrame().loadCaul();

    }
}

class NewFrame extends Frame{

    //对象-属性
    TextField tf1,tf2,tf3;

    //对象-方法
    public void loadCaul(){
        //三个文本框
        tf1 = new TextField(10);
        tf2 = new TextField(10);
        tf3 = new TextField(20);
        //一个按钮
        Button but = new Button("=");
        //一个标签
        Label label = new Label("+");

        //设置为流布局
        setLayout(new FlowLayout());
        add(tf1);
        add(label);
        add(tf2);
        add(but);
        add(tf3);
        setVisible(true);
        setSize(600,200);
        //按钮的监听事件
        but.addActionListener(new NewActionListener());

        //关闭窗口
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }
    //私有化 监听事件自用
   private class NewActionListener implements ActionListener{

        //可以直接获取NewFrame对象的属性


        @Override
        public void actionPerformed(ActionEvent e) {
            //获取文本的值
            int num1 = Integer.parseInt(tf1.getText());
            int num2 = Integer.parseInt(tf2.getText());

            tf3.setText(""+(num1+num2));
            //清空文本框
            tf1.setText("");
            tf2.setText("");

        }
    }

}

画笔

/**
 * 画笔的使用Paint
 * @author xue
 */
public class Demo8 {
    public static void main(String[] args) {
        new MyPaint().loadFrame();
    }
}

class MyPaint extends Frame{

    public void loadFrame() {
        setBounds(200,200,600,500);
        setVisible(true);

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }


    //画笔方法 同toString 自动隐式调用
    @Override
    public void paint(Graphics g) {
//        g.setColor(Color.red);  //需要又颜色才能花花
        g.drawOval(100,100,100,100);
        g.fillOval(250,100,100,100);//实心的圆

        g.fillRect(400,100,100,100); //实心矩形

        //这里注意用完最好将颜色还原为最初的颜色
    }
}

鼠标监听

/**
 * 简易画图上画点
 */
public class Practice2 {
    public static void main(String[] args) {
        new NewPaint("画画");
    }

}

//需要一个画板画画
class NewPaint extends Frame{
    ArrayList points;
    public NewPaint(String str){
        super(str);
        setBounds(500,500,800,600);//画板的位置和大小
        setVisible(true);
        points = new ArrayList();
        addMouseListener(new MouseLister());

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    @Override
    public void paint(Graphics g) {
        Iterator it = points.iterator();
        while (it.hasNext()){
            Point p = (Point) it.next();
            g.setColor(Color.cyan);
            g.fillOval(p.x,p.y,5,5);
        }
    }

    //把点存入集合
    public void addPoint(Point p) {
        points.add(p);
    }

    //监听内部类
    private class MouseLister extends MouseAdapter {


        @Override
        public void mousePressed(MouseEvent e) {
            NewPaint mouPre = (NewPaint) e.getSource();
            mouPre.points.add(new Point(e.getX(),e.getY()));
//            mouPre.addPoint(new Point(e.getX(),e.getY()));

            //画完一次就刷新重新开始
            mouPre.repaint();
        }
    }

}

窗口监听

public class Demo9 {
    public static void main(String[] args) {
        new NewFrame2();
    }
}

class NewFrame2 extends Frame{
    public NewFrame2() {
        setVisible(true);
        setSize(600,400);
        this.addWindowListener(new WindowAdapter() {


            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("正在关闭");
                System.exit(0);
            }

            @Override
            public void windowClosed(WindowEvent e) {
                System.out.println("我关闭了");
            }

            
            @Override
            public void windowActivated(WindowEvent e) {
                e.getSource();//是获取事件的发起者
                System.out.println("快来点我");
                setTitle("快来点我");
            }


        });

    }
    

}

键盘监听

public class Demo10 {
    public static void main(String[] args) {
        new KeyBord();
    }
}

class KeyBord extends Frame{
    public KeyBord() {
        setVisible(true);
        setSize(300,500);

        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                //e.getKeyCode();获取键盘上键的码
                String str = String.valueOf((char)e.getKeyCode());
                System.out.println(str);
            }
        });
    }
}

标签:java,22,void,day20,class,add,new,TextField,public
From: https://www.cnblogs.com/onlyxue/p/16843805.html

相关文章

  • CSPS2022 题解
    T1容易想到枚举\(B,C\),然后\(A,D\)可以预处理,即对于\(i\)处理存在路径\(1\rightarrowj\rightarrowi\)中\(j\)的权值最大的,那么只需枚举\(B,C\)然后分别取最......
  • CSPS2022 游记
    CSPS2022又寄在役期间的最后一次CSP,本来以为能留下一个辉煌的战绩,可惜寄了。Day-114514停课第一周没考试,看了一车没看过的算法,感觉良好。大家都停课之后每天晚上一......
  • Java 语言编码规范(Java Code Conventions)
    目录 ​​1介绍​​​​• 1.1为什么要有编码规范​​​​• 1.2版权声明​​​​2文件名​​​​2.1文件后缀​​​​2.2常用文件名​​​​3文件组织​​​​......
  • Java IDE Maven Git配置
    mavenconf<localRepository>D:/localRepository</localRepository>  永久配置jdk1.8 ......
  • java tomcat按天生成日志
    tomcat按天生成日志,亲测可行~1.安装cronolog安装方式一:shell>sudoyuminstallcronolog或shell>sudoapt-getinstallcronolog验证cronolog:shell>whichcronol......
  • JavaScript常用方法和一些封装
    博主在js上已经花费了很长时间,不禁深深地被其轻巧而强大的功能,以及优雅灵活的写法所折服,一直没找到机会来总结一下,正好把学习的东西做一个汇总。题外话我始终认为,学习编程最......
  • 《JavaScript百炼成仙》续集01. let强者,竟恐怖如斯
     前些天发现了一个巨牛的人工智能学习博客,通俗易懂,风趣幽默,忍不住分享一下给大家。​​点击跳转​​这一日夜晚,月光皎洁,洒洒地落在青山院西南边的一座小山上。这座小山大约......
  • 【博学谷学习记录】超强总结,用心分享|Java基础分享-树
    目录1.树的简介2.一些有关于树的概念3.树的种类3.1B-树、B+树简介3.2B+树简介3.3B+树和B-树的主要区别3.4B+树的插入4.B+树经典面试题 前言当我们发现SQL......
  • Java通过注解运行方法
    //上下文@ResourceprivateApplicationContextapplicationContext;@Beanpublicvoidtest(){//扫描ControllerReflectionsreflections=newReflection......
  • javaScript 中的布尔类型
    https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Language_Overview......