java实现简单的会员注册窗体

最近在学习Java图形界面,一下实现了一个简单的会员注册窗体,可能会有bug

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);
	}

}

运行界面如下:
示例图

该项目bin文件夹下,要有这三张图片,可以自己替换,但要注意图片名
示例图

  • 4
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值