swing 登录窗口
public static void main(String[] args) {
// 创建 JFrame 实例
JFrame frame = new JFrame(“登录窗口”);
// Setting the width and height of frame
/设置样式/
frame.setSize(350, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15/* 创建面板,这个类似于 HTML 的 div 标签 * 我们可以创建多个面板并在 JFrame 中指定位置 * 面板中我们可以添加文本字段,按钮及其他组件。 */ JPanel panel = new JPanel(); // 添加面板 frame.add(panel); /* * 调用用户定义的方法并添加组件到面板 */ placeComponents(panel); // 设置界面可见 frame.setVisible(true); }
private static void placeComponents(JPanel panel) {
复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44/* 布局部分我们这边不多做介绍 * 这边设置布局为 null */ panel.setLayout(null); // 创建 JLabel JLabel userLabel = new JLabel("用户名:"); /* 这个方法定义了组件的位置。 * setBounds(x, y, width, height) * x 和 y 指定左上角的新位置,由 width 和 height 指定新的大小。 */ userLabel.setBounds(10,20,80,25); panel.add(userLabel); /* * 创建文本域用于用户输入 */ JTextField userText = new JTextField(20); userText.setBounds(100,20,165,25); panel.add(userText); // 输入密码的文本域 JLabel passwordLabel = new JLabel("密码:"); passwordLabel.setBounds(10,50,80,25); panel.add(passwordLabel); /* *这个类似用于输入的文本域 * 但是输入的信息会以点号代替,用于包含密码的安全性 */ JPasswordField passwordText = new JPasswordField(20); passwordText.setBounds(100,50,165,25); panel.add(passwordText); // 创建登录按钮 JButton loginButton = new JButton("登录"); loginButton.setBounds(50, 80, 80, 25); panel.add(loginButton); // 创建注册按钮 JButton registerButton = new JButton("注册"); registerButton.setBounds(200, 80, 80, 25); panel.add(registerButton); }
数据表格
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42public TableTest01() { JFrame f = new JFrame(); Object[][] playerInfo = { // 创建表格中的数据 { "hy", new Integer(25),"男","sx","打游戏"}, { "sym", new Integer(25),"男","hn","打游戏"}, { "wmh", new Integer(25),"男","hn","打游戏"}, { "zft",new Integer(25),"男","hn","打游戏"}, { "hmt",new Integer(25),"男","hn","打游戏"}, { "yjy",new Integer(25),"男","hn","打游戏"}, { "spf",new Integer(25),"男","hn","打游戏"}, { "ckf",new Integer(25),"男","hn","打游戏"}, }; // 创建表格中的横标题 String[] Names = { "姓名", "年龄", "性别", "地址", "爱好" }; // 以Names和playerInfo为参数,创建一个表格 JTable table = new JTable(playerInfo, Names); /*行高*/ table.setRowHeight(11); /*组件位置*/ /*table.setBounds(100,500,500,200);*/ // 设置此表视图的首选大小 table.setPreferredScrollableViewportSize(new Dimension(500, 150)); // 将表格加入到滚动条组件中 JScrollPane scrollPane = new JScrollPane(table); f.getContentPane().add(scrollPane, BorderLayout.CENTER); // 再将滚动条组件添加到中间容器中 f.setTitle("qbzlm"); f.pack(); f.setVisible(true); f.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } public static void main(String[] args) { TableTest01 t = new TableTest01(); }
最后
以上就是糊涂小蜜蜂最近收集整理的关于【_ 記 】swing 登录窗口和数据表格的全部内容,更多相关【_内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复