From 145ecbc6aa94b467fa9bb3ad92399f848c75ca35 Mon Sep 17 00:00:00 2001 From: NCJOAQ <2627723488@qq.com> Date: Tue, 29 Jul 2025 20:12:51 +0800 Subject: [PATCH] =?UTF-8?q?feat(class38):=20=E6=B7=BB=E5=8A=A0=20Test3=20?= =?UTF-8?q?=E7=B1=BB=E5=88=9B=E5=BB=BA=E7=99=BB=E9=99=86=E7=AA=97=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 Test3 类,实现了一个简单的登陆窗口 - 窗口包含一个登陆按钮,并使用 AtomicInteger 实现点击计数 - 演示了使用 lambda 表达式简化事件监听器的创建 --- src/class38/Test3.java | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/class38/Test3.java diff --git a/src/class38/Test3.java b/src/class38/Test3.java new file mode 100644 index 0000000..ed8578c --- /dev/null +++ b/src/class38/Test3.java @@ -0,0 +1,32 @@ +package class38; + +import javax.swing.*; +import java.util.concurrent.atomic.AtomicInteger; + +public class Test3 { + public static void main(String[] args) { + //创建一个登陆窗口,窗口上只有一个登陆按钮 + JFrame win = new JFrame("登陆窗口"); + win.setSize(300,200); + win.setLocationRelativeTo( null); + win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + JPanel p = new JPanel(); + win.add(p); + + JButton btn = new JButton("登陆"); + p.add(btn); +// btn.addActionListener(new ActionListener() { +// private int count = 0; +// @Override +// public void actionPerformed(ActionEvent e) { +// System.out.println("登陆"+count++); +// } +// }); + //简化 + AtomicInteger count = new AtomicInteger();//创建一个原子类 + btn.addActionListener((e) -> System.out.println("登陆"+(count.incrementAndGet()))); + + win.setVisible(true); + } +} \ No newline at end of file