设计注册窗体实 现注册信息确认提示取消功能 能 有图片 欢迎大家踊跃作答
import javax.swing.*;
import java.awt.*;
public class RegistUI {
public void initUI(){
JFrame jf = new JFrame("注册界面");
jf.setSize(500,300);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
FlowLayout flow = new FlowLayout();
jf.setLayout(flow);
JLabel nameJla = new JLabel("请输入你的账号:");
JLabel pwdJla = new JLabel("请输入你的密码:");
Dimension dim = new Dimension(340,35);
JTextField nameInput = new JTextField();
JPasswordField pwdInput = new JPasswordField();
nameInput.setPreferredSize(dim);
pwdInput.setPreferredSize(dim);
JButton btn1 = new JButton("确定");
JButton btn2 = new JButton("取消");
jf.add(nameJla);
jf.add(nameInput);
jf.add(pwdJla);
jf.add(pwdInput);
jf.add(btn1);
jf.add(btn2);
jf.setVisible(true);
}
public static void main(String[] args) {
RegistUI registUI=new RegistUI();
registUI.initUI();
}
}
package test;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;
public class Enrolled extends JFrame {//注册账号
public static String name1;
private JPanel contentPane;
private JTextField textField;
private JPasswordField passwordField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Enrolled frame1 = new Enrolled();
frame1.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Enrolled() {
setIconImage(Toolkit.getDefaultToolkit().getImage("D:\\JAVA\\My\\\u56FE\u6807\\ConstructionBank.jpg"));
setTitle("\u6B22\u8FCE\u4F7F\u7528\u94F6\u884CATM\u673A");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 625, 390);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel_1 = new JLabel("\u8D26\u53F7\uFF1A");
lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 20));
lblNewLabel_1.setBounds(109, 83, 78, 44);
contentPane.add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("\u5BC6\u7801\uFF1A");
lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 20));
lblNewLabel_2.setBounds(109, 178, 78, 44);
contentPane.add(lblNewLabel_2);
textField = new JTextField();
textField.setFont(new Font("宋体", Font.PLAIN, 20));
textField.setColumns(10);
textField.setBounds(219, 83, 253, 44);
contentPane.add(textField);
passwordField = new JPasswordField();
passwordField.setFont(new Font("宋体", Font.PLAIN, 20));
passwordField.setEchoChar('*');
passwordField.setBounds(219, 181, 253, 44);
contentPane.add(passwordField);
JButton btnNewButton = new JButton("\u53D6\u6D88");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(null);
passwordField.setText(null);
}
});
btnNewButton.setFont(new Font("宋体", Font.PLAIN, 20));
btnNewButton.setBounds(109, 274, 106, 53);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("\u786E\u8BA4");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
DBHelper dbHelper=new DBHelper();
String sql2 = "select * from atm where binary name ="+"'"+textField.getText()+"'";
ResultSet rsResultSet = dbHelper.DB(sql2);
if(rsResultSet.next()==true){//判断该用户名是否存在
JOptionPane.showMessageDialog(contentPane, "该用户名已存在!", "提示:",JOptionPane.WARNING_MESSAGE);
dbHelper.downConn();
}
else {
if(textField.getText().matches("[a-zA-Z\\w\\p{Punct}]{5,17}$")
&& passwordField.getText().matches("[a-zA-Z\\w\\p{Punct}]{5,17}$")) {//限制账户和密码的长度
String sql = "insert into atm (balance,name,password) values(?,?,?)";
PreparedStatement ps = dbHelper.connection.prepareStatement(sql);
ps.setString(1, "0");
ps.setString(2, textField.getText());//向数据库中添加数据
ps.setString(3, passwordField.getText());
ps.executeUpdate();
JOptionPane.showMessageDialog(contentPane, "注册成功!\n请登录!", "提示:",JOptionPane.WARNING_MESSAGE);
// System.exit(0);
dispose ();//关闭当前窗口
}
else {
JOptionPane.showMessageDialog(contentPane, "账户名和密码有误!", "错误提示:",JOptionPane.WARNING_MESSAGE);
JOptionPane.showMessageDialog(contentPane, "账户名和密码由5-17位\n(字母、数字或符号)组成!", "错误提示:",JOptionPane.WARNING_MESSAGE);
}
}
}
catch (Exception e1) {
}
}
});
btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 20));
btnNewButton_1.setBounds(413, 274, 106, 53);
contentPane.add(btnNewButton_1);
}
}
参考一下
一个简单的会员注册窗体
package memberRegistration;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import javax.swing.*;
public class MemberRegistration extends JFrame {
private static final long serialVersionUID = 1L;
public MemberRegistration() {
Label name = new Label("姓名"); //创建一个标签
name.setBackground(Color.pink); //标签的颜色设置为粉红色
Label pwd = new Label("密码");
Label pwdAgain = new Label("再次输入\n密码");
pwdAgain.setBackground(Color.pink);
pwd.setBackground(Color.pink);
JTextField username = new JTextField(10);//创建一个普通文本输入框
JPasswordField userPwd = new JPasswordField(10);//创建一个密码输入框,这个组件自动隐藏密码
JPasswordField userPwdAgain = new JPasswordField(15);//创建再次输入密码框
JPanel one = new JPanel(new FlowLayout(6, 10, 10)); //创建面板,并设置布局
one.add(name);//将组件逐个添加至面板
one.add(username);
one.add(pwd);
one.add(userPwd);
one.add(pwdAgain);
one.add(userPwdAgain);
Label sex = new Label("性别");
sex.setBackground(Color.pink);
JCheckBox man = new JCheckBox("男"); //设置复选框
JCheckBox women = new JCheckBox("女");
ButtonGroup group = new ButtonGroup(); //设置按钮组
group.add(man); //将man和women两个按钮捆绑在一起,这样就只能选择其中的一个
group.add(women);
one.add(sex);
one.add(man);
one.add(women);
Label agree = new Label("同意注册协议");
agree.setBackground(Color.pink);
JRadioButton rb1 = new JRadioButton("是"); //创建单选框
JRadioButton rb2 = new JRadioButton("否");
ButtonGroup rbgroup = new ButtonGroup();
rbgroup.add(rb1);
rbgroup.add(rb2);
one.add(agree);
one.add(rb1);
one.add(rb2);
Label Grade = new Label("年级");
Grade.setBackground(Color.pink);
String[] grade = { "大一", "大二", "大三", "大四" };
JComboBox<String> jcb = new JComboBox<String>(grade);
jcb.setPreferredSize(new Dimension(100, 20));
one.add(Grade);
one.add(jcb);
JButton ok = new JButton("确认");
one.add(ok);
ok.addActionListener((event) -> {
if (username.getText().trim().length() == 0) {
errorDialog("用户名不能为空,请填写用户名");
return;
}
String reg = "^[\u4E00-\u9FA5A-Za-z]+$"; // 正则表达式判断输入的用户名是否合法
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(username.getText().trim());
if (!m.matches()) {
errorDialog("用户名不合法,请重新输入");
return;
}
if (new String(userPwd.getPassword()).trim().length() == 0) {
errorDialog("密码不能为空,请设置密码");
return;
}
if (new String(userPwdAgain.getPassword()).trim().length() == 0) {
errorDialog("请再次输入您的密码");
return;
}
if (!new String(userPwd.getPassword()).trim().toString()
.equals(new String(userPwdAgain.getPassword()).trim().toString())) {
errorDialog("两次密码输入不一致,请重新输入");
userPwd.setText("");
userPwdAgain.setText("");
return;
}
if (!man.isSelected() && !women.isSelected()) {
errorDialog("请选择性别");
return;
}
if (!rb1.isSelected()) {
errorDialog("请同意用户协议,否则无法完成注册");
return;
}
this.dispose();
JFrame frame1 = new JFrame("注册成功");
frame1.setLocation(500, 200);
frame1.setSize(300, 275);
Container C = frame1.getContentPane();
JPanel success = new MyPanel("icon2.png");
Label info = new Label("注册信息");
JTextArea content = new JTextArea();
content.setBackground(new Color(225, 255, 255)); //
content.setAutoscrolls(true);
content.setPreferredSize(new Dimension(215, 75));
content.setLineWrap(true);
content.append("姓名:" + username.getText() + "\n");
content.append("性别:" + (man.isSelected() == true ? "男" : "女") + "\n");
content.append(grade[jcb.getSelectedIndex()]);
content.setEditable(false);
JButton confirm = new JButton("确认");
confirm.addActionListener((e) -> {
confirm.setBackground(new Color(255, 218, 185));
frame1.dispose();
});
success.add(info);
success.add(content);
success.add(confirm);
C.add(success);
frame1.setVisible(true);
});
JPanel jpanel = new MyPanel("icon.png");
jpanel.setBackground(null);
JSplitPane jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, jpanel, one); //使用分隔面板
jSplitPane.setOneTouchExpandable(true);
jSplitPane.setDividerSize(1);
Container c = getContentPane();
c.add(jSplitPane);
this.setLocation(500, 200);
setSize(400, 350);
jSplitPane.setDividerLocation(175);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setIcon();
setVisible(true);
}
private void setIcon() { //重新设置窗体的图标
BufferedImage image = null;
try {
System.out.println("从"+this.getClass().getResource("")+"获取图片");
image = ImageIO.read(this.getClass().getResource("frame.png"));
} catch (IOException e) {
e.printStackTrace();
}
this.setIconImage(image);
}
public static void errorDialog(String message) {
JOptionPane.showMessageDialog(null, message, "警告", JOptionPane.ERROR_MESSAGE); //弹出对话框
}
public static void main(String[] args) {
new MemberRegistration();
}
}
package memberRegistration;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.Jpanel;
public class MyPanel extends JPanel { //自定义一个面板,为该面板设置好背景图片
private static final long serialVersionUID = 1L;
ImageIcon icon;
Image img;
public MyPanel(String pic) {
icon = new ImageIcon(this.getClass().getResource(pic));
img = icon.getImage();
}
@Override
public void paintComponent(Graphics g) {
/*当java认为需要重新绘制组件的时候由java调用。
例如你在程序中repaint();或者程序窗口最小化,然后恢复。或者程序窗口被遮挡,又显现的时候。你注意观察,这个方法是个受保护的方法,这就是说我们平常并不用管这个方法,这个方法只在你需要继承paintComponent(一般是JFrame)的时候,重写方法,也可以不重新方法,如果你不需要改变绘制组件动作的话)。*/
super.paintComponent(g);
g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
}