java-GUI-事件监听+输入框
2021/4/13 12:25:12
本文主要是介绍java-GUI-事件监听+输入框,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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-事件监听+输入框的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)
- 2024-05-30【Java】百万数据excel导出功能如何实现