ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-GUI-事件监听+输入框

2021-04-13 12:02:32  阅读:192  来源: 互联网

标签:java 窗口 GUI 输入框 void frame new textField public


GUI-事件监听+输入框

事件监听

public class Application {
    public static void main(String[] args) {
        new MyFrame(); // 点击按钮
    }
}

class MyFrame{
    public MyFrame() {
        init();
    }

    public void init() {
        // 定义窗口
        Frame frame = new Frame();
        // 设置窗口大小,坐标
        frame.setBounds(200, 200, 400, 400);
        // 设置窗口颜色
        frame.setBackground(Color.BLACK);

        Button btn = new Button();
        frame.add(btn);
        // 添加事件监听
        btn.addActionListener(new MyActionListener());

        // 关闭窗口
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                // 终止程序
                System.exit(0);
            }
        });
        // 设置窗口显示
        frame.setVisible(true);
        // 设置窗口自适应
        frame.pack();
    }

    // 自定义事件监听
    class MyActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("点击按钮");
        }
    }
}

输入框

输入框:TextField

public class Application {
    public static void main(String[] args) {
        new MyFrame(); // 输入数据
    }
}

class MyFrame{
    public MyFrame() {
        init();
    }

    public void init() {
        // 定义窗口
        Frame frame = new Frame();
        // 设置窗口大小,坐标
        frame.setBounds(200, 200, 400, 400);
        // 设置窗口颜色
        frame.setBackground(Color.BLACK);

        // 输入框
        TextField textField = new TextField();
        frame.add(textField);
        // 输入框事件回车时触发
        textField.addActionListener(new MyActionListener());

        // 关闭窗口
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                // 终止程序
                System.exit(0);
            }
        });
        // 设置窗口显示
        frame.setVisible(true);
        // 设置窗口自适应
        frame.pack();
    }

    // 自定义事件监听
    class MyActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("输入数据");
        }
    }
}

按钮与输入框的结合-简易计算器

例子:

public class Application {
    public static void main(String[] args) {
        new MyFrame(); // 输入数据
    }
}

class MyFrame{

    private TextField textField_1;
    private TextField textField_2;
    private TextField textField_3;

    public MyFrame() {
        init();
    }

    public void init() {
        // 定义窗口
        Frame frame = new Frame();
        // 设置窗口大小,坐标
        frame.setBounds(200, 200, 400, 400);

        // 输入框 设置输入框的宽度
        textField_1 = new TextField(10);
        textField_2 = new TextField(10);
        textField_3 = new TextField(10);
        // 设置标签
        Label label = new Label("+");
        // 设置按钮
        Button btn = new Button("=");
        // 设置窗口为流式布局
        frame.setLayout(new FlowLayout());
        // 组件放入窗口
        frame.add(textField_1);
        frame.add(label);
        frame.add(textField_2);
        frame.add(btn);
        frame.add(textField_3);
        // 按钮设置监听按钮
        btn.addActionListener(new MyActionListener());

        // 关闭窗口
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                // 终止程序
                System.exit(0);
            }
        });
        // 设置窗口显示
        frame.setVisible(true);
        // 设置窗口自适应
        frame.pack();
    }

    // 自定义事件监听
    class MyActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            // 获取输入框数据
            int num1 = Integer.parseInt(textField_1.getText());
            int num2 = Integer.parseInt(textField_2.getText());
            // 设置输入框数据
            textField_3.setText(num1 + num2 + "");
            textField_1.setText("");
            textField_2.setText("");
        }
    }
}

标签:java,窗口,GUI,输入框,void,frame,new,textField,public
来源: https://www.cnblogs.com/longma-ling/p/14652481.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有