- 实验目的
(1)掌握Swing常见的布局管理器。
(2)掌握文本框的用法。
(3)掌握密码框的用法。
- 实验内容
(1)掌握Java图形界面设计。
(2)了解其中的相关概念、类的使用。
(3)编写RegistDemo类,创建窗体实现用户注册功能。
- 实验步骤
编写RegistDemo类,该类继承JFrame类。在窗体中增加文本域、密码域、按钮等组件实现注册功能。代码如下:
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.lang.reflect.Array;
import java.util.Arrays;
public class RegistDemo extends JFrame {
private static final long serialVersionUID = -7774133711807576073L;
private JPanel contentPane;
private JTextField textField;
private JPasswordField passwordField1;
private JPasswordField passwordField2;
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Throwable e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
RegistDemo frame = new RegistDemo();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public RegistDemo() {
setTitle("用户注册");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 300, 250);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new GridLayout(5, 1, 5, 5));
JPanel panel1 = new JPanel();
contentPane.add(panel1);
JLabel label1 = new JLabel("新用户注册");
panel1.add(label1);
JPanel panel2 = new JPanel();
FlowLayout flowLayout = (FlowLayout) panel2.getLayout();
flowLayout.setAlignment(FlowLayout.LEFT);
contentPane.add(panel2);
JLabel label2 = new JLabel("用 户 名:");
panel2.add(label2);
textField = new JTextField();
panel2.add(textField);
textField.setColumns(18);
JPanel panel3 = new JPanel();
FlowLayout flowLayout1 = (FlowLayout) panel3.getLayout();
flowLayout1.setAlignment(FlowLayout.LEFT);
contentPane.add(panel3);
JLabel label3 = new JLabel(" 密 码 :");
panel3.add(label3);
passwordField1 = new JPasswordField();
passwordField1.setColumns(18);
panel3.add(passwordField1);
JPanel panel4 = new JPanel();
FlowLayout flowLayout2 = (FlowLayout) panel4.getLayout();
flowLayout2.setAlignment(FlowLayout.LEFT);
contentPane.add(panel4);
JLabel label4 = new JLabel("确认密码:");
panel4.add(label4);
passwordField2 = new JPasswordField();
passwordField2.setColumns(18);
panel4.add(passwordField2);
JPanel panel5 = new JPanel();
contentPane.add(panel5);
JButton button = new JButton("提交");
button.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
do_button_actionPerformed(e);
}
});
panel5.add(button);
}
protected void do_button_actionPerformed(ActionEvent e) {
char[] password1 = passwordField1.getPassword();
char[] password2 = passwordField2.getPassword();
boolean equals = Arrays.equals(password1, password2);
if (equals) {
JOptionPane.showMessageDialog(this, "用户注册成功!", null, JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "两次密码不同!", null, JOptionPane.WARNING_MESSAGE);
}
Arrays.fill(password1, '0');
Arrays.fill(password2, '0');
}
}
- 实验结果
运行程序,效果如图所示。
- 当两次输入密码不同时:
图 1
- 当两次输入密码相同时:
图 2