⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 regframe.java

📁 JAVA编写的聊天小程序!!程序默认需放到D:下
💻 JAVA
字号:
package group.client;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class RegFrame extends JFrame
{
	private Client client;

	private JLabel nameLabel;
	private JLabel passLabel;
	private JLabel repeatLabel;
	private JTextField nameText;
	private JPasswordField regPassText;
	private JPasswordField repeatText;
	private JButton OKButton;
	private JButton cancelButton;

	public RegFrame(Client c)
	{
		super("用户注册");
		setSize(300, 200);

		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
		int screenWidth = screenSize.width;
		int screenHeight = screenSize.height;

		int localX = (screenWidth - getSize().width) / 2;
		int localY = (screenHeight - getSize().height) / 2;
		setLocation(localX, localY);
		setResizable(false);

		Font f = new Font("Serif", Font.BOLD, 14);
		nameLabel = new JLabel("   昵      称 :  ");
		nameLabel.setFont(f);
		passLabel = new JLabel("   密      码 :  ");
		passLabel.setFont(f);
		repeatLabel = new JLabel("   确认密码 : ");
		repeatLabel.setFont(f);
		nameText = new JTextField(13);
		nameText.setFont(f);
		regPassText = new JPasswordField(13);
		regPassText.setFont(f);
		repeatText = new JPasswordField(13);
		repeatText.setFont(f);
		OKButton = new JButton("注   册");
		OKButton.setFont(f);
		cancelButton = new JButton("返   回");
		cancelButton.setFont(f);

		regPassText.addFocusListener(new FocusAdapter()
			{
				public void focusGained(FocusEvent e)
				{
					regPassText.setText("");
				}
			});
		repeatText.addFocusListener(new FocusAdapter()
			{
				public void focusGained(FocusEvent e)
				{
					repeatText.setText("");
				}
			});

		RegAction listener = new RegAction();
		nameText.addActionListener(listener);
		regPassText.addActionListener(listener);
		repeatText.addActionListener(listener);
		OKButton.addActionListener(listener);
		cancelButton.addActionListener(listener);

		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints gbc = new GridBagConstraints();

		JPanel regPane1 = new JPanel();
		regPane1.setLayout(gbl);

//		Icon i = new ImageIcon("\\date\\szn.gif");
		add(regPane1, new JLabel(), gbc, 0, 0, 9, 1, 0, 20);
		add(regPane1, nameLabel, gbc, 1, 1, 1, 1, 10, 5);
		add(regPane1, nameText, gbc, 2, 1, 3, 1, 0, 0);
		add(regPane1, passLabel, gbc, 1, 2, 1, 1, 10, 5);
		add(regPane1, regPassText, gbc, 2, 2, 3, 1, 0, 0);
		add(regPane1, repeatLabel, gbc, 1, 3, 1, 1, 10, 5);
		add(regPane1, repeatText, gbc, 2, 3, 3, 1, 0, 0);
		gbc.anchor = GridBagConstraints.EAST;
		add(regPane1, OKButton, gbc, 1, 4, 3, 1, 13, 10);
		gbc.anchor = GridBagConstraints.CENTER;
		add(regPane1, cancelButton, gbc, 4, 4, 3, 1, 5, 0);

		Container pane = getContentPane();
		pane.add(regPane1, BorderLayout.CENTER);

		client = c;

		addWindowListener(new CloseWindow());
	}

	private void add(Container c, Component c2, GridBagConstraints gbc, int gridx, int gridy,
		int gridwidth, int gridheight, int weightx, int weighty)
	{
		gbc.gridx = gridx;
		gbc.gridy = gridy;
		gbc.gridwidth = gridwidth;
		gbc.gridheight = gridheight;
		gbc.weightx = weightx;
		gbc.weighty = weighty;
		c.add(c2, gbc);
	}

	/*
	 处理注册窗口两个按钮的事的的内部类
	*/
	private class RegAction implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			JComponent tmp = (JComponent)e.getSource();

			if(tmp == nameText)
			{
				regPassText.requestFocus();
			}
			else if(tmp == regPassText)
			{
				repeatText.requestFocus();
			}
			else if((tmp == OKButton)  || (tmp == repeatText))
			{
				String name = nameText.getText();
				String pass1 = new String(regPassText.getPassword());
				String pass2 = new String(repeatText.getPassword());

				if(name.equals("") || pass1.equals(""))
				{
					JOptionPane.showMessageDialog(RegFrame.this, "昵称或者密码不能为空" +
						"请重新输入!!!", "错误", JOptionPane.WARNING_MESSAGE);
				}
				else if(!pass1.equals(pass2))
				{
					JOptionPane.showMessageDialog(RegFrame.this, "您两次输入的密码不同," +
						"请重新输入!!!", "错误", JOptionPane.WARNING_MESSAGE);
				}
				else if((name.indexOf("::") != -1) || (pass1.indexOf("::") != -1))
				{
					JOptionPane.showMessageDialog(RegFrame.this, "昵称或者密码不能包含两个冒号!!!",
						"错误", JOptionPane.WARNING_MESSAGE);
				}
				else
				{
					client.regInfo(pass1, nameText.getText());
					RegFrame.this.hide();
				}
			}
			else if(tmp == cancelButton)
			{
				RegFrame.this.hide();
				client.startLogin();
			}
		}
	}

	private class CloseWindow extends WindowAdapter
	{
		public void windowClosing(WindowEvent e)
		{
			RegFrame.this.hide();
			client.startLogin();
		}
	}

/*	public static void main(String[] args)
	{
		RegFrame r = new RegFrame();
		r.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		r.show();

	}*/
}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -