feat(class38): 添加 Test3 类创建登陆窗口
- 新增 Test3 类,实现了一个简单的登陆窗口 - 窗口包含一个登陆按钮,并使用 AtomicInteger 实现点击计数 - 演示了使用 lambda 表达式简化事件监听器的创建
This commit is contained in:
32
src/class38/Test3.java
Normal file
32
src/class38/Test3.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user