Skip to content

Commit e85e235

Browse files
author
shy-coder
committed
day20:JFrame学习
1 parent b3cfe22 commit e85e235

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.shy.day20;
2+
3+
import javax.swing.*;
4+
5+
/**
6+
* @ClassName HelloWorldSwiing
7+
* @Author shy
8+
* @Date 2020/11/8
9+
**/
10+
public class HelloWorldSwing {
11+
12+
public static void main(String[] args) {
13+
//显示应用 GUI
14+
javax.swing.SwingUtilities.invokeLater(HelloWorldSwing::createAndShowGUI);
15+
}
16+
17+
public static void createAndShowGUI() {
18+
//漂酿的外观
19+
JFrame.setDefaultLookAndFeelDecorated(true);
20+
21+
//创建及设置窗口
22+
JFrame frame = new JFrame("HelloWorldSwing");
23+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
24+
25+
//添加 "Hello World" 标签
26+
JLabel label = new JLabel("Hello World");
27+
frame.getContentPane().add(label);
28+
29+
//显示窗口
30+
frame.pack();
31+
frame.setVisible(true);
32+
}
33+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.shy.day20;
2+
3+
import javax.swing.*;
4+
5+
/**
6+
* @ClassName SwingLoginExample
7+
* @Author shy
8+
* @Date 2020/11/8
9+
**/
10+
public class SwingLoginExample {
11+
12+
public static void main(String[] args) {
13+
//创建 JFrame 实例
14+
JFrame frame = new JFrame("Login Example");
15+
//设置窗体的宽和高
16+
frame.setSize(350,200);
17+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
18+
19+
/*
20+
创建面板,这个类与HTML的div标签相似
21+
我们可以创建多个面板并在 JFrame 中指定位置
22+
面板中我们可以添加文本字段,按钮及其他组件
23+
*/
24+
JPanel panel = new JPanel();
25+
//添加面板
26+
frame.add(panel);
27+
/*
28+
调用用户定义的方法并添加组件到面板
29+
*/
30+
placeComponents(panel);
31+
32+
//设置界面可见
33+
frame.setVisible(true);
34+
}
35+
36+
private static void placeComponents(JPanel panel) {
37+
//布局部分不过多介绍,设置布局为 null
38+
panel.setLayout(null);
39+
40+
//创建 JLabel
41+
JLabel userLable = new JLabel("User");
42+
/**
43+
* 这个方法定义了组件的位置,
44+
* setBounds(x,y,width,height)
45+
* x 和 y 指定左上角的新位置,由width 和 height 指定新的大小
46+
*/
47+
userLable.setBounds(10,20,80,25);
48+
panel.add(userLable);
49+
50+
/**
51+
* 创建文本域用于用户输入
52+
*/
53+
JTextField userText = new JTextField(20);
54+
userText.setBounds(100,20,165,25);
55+
panel.add(userText);
56+
57+
/**
58+
* 输入密码的文本域
59+
*/
60+
JLabel passwordLabel = new JLabel("PassWord:");
61+
passwordLabel.setBounds(10,50,80,25);
62+
panel.add(passwordLabel);
63+
64+
/**
65+
* 这个类是用于输入的文本域
66+
* 输入的信息会以点号代替,用于包含密码的安全性
67+
*/
68+
JPasswordField passwordText = new JPasswordField(20);
69+
passwordText.setBounds(100,50,165,25);
70+
panel.add(passwordText);
71+
72+
/**
73+
*创建登录按钮
74+
*/
75+
JButton loginButton = new JButton("login");
76+
loginButton.setBounds(10,80,80,25);
77+
panel.add(loginButton);
78+
}
79+
}

0 commit comments

Comments
 (0)