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

📄 requestfrm.java

📁 简单的聊天程序
💻 JAVA
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.*;
import java.util.StringTokenizer;

public class RequestFrm {
	private JFrame f;
	private JButton clearB,confirmB,iconBtn;
	private JLabel iconShowLab;
	private JTextField[] tf;
	private JPasswordField[] pas;
	private JTextArea sign;
	private JRadioButton boy;
	private String ip,port;
	private RequestAdapter adp;
	public RequestFrm(String ip,String port){
		init(ip,port);
		buildGUI();
		hookEvent();
	}
	private void init(String ip,String port){
		this.ip = ip;
		this.port = port;
	}
	private void buildGUI(){
		String[] labelNames = {"性别","昵称","邮箱","姓名",
								"年龄","密码","确认密码","头像","个性签名",};
		this.f = new JFrame("填写申请表");
		this.f.setIconImage(new ImageIcon("Icon/titleIcon.gif").getImage());
		JLabel[] label = new JLabel[labelNames.length];
		tf = new JTextField[4];
		for(int i =0;i < labelNames.length;++i){
			label[i] = new JLabel(labelNames[i]);
		}
		for(int i=0;i < tf.length; ++i){
			tf[i] = new JTextField(20);
		}
		JLabel[] tip = new JLabel[2];
		for(int i =0;i < tip.length; ++i){
			tip[i] = new JLabel("*必填*",SwingConstants.LEFT);
			tip[i].setForeground(Color.red);
		}
		iconShowLab = new JLabel();
		iconShowLab.setName("PersonIcon/13.jpg");
		iconShowLab.setIcon(new ImageIcon("PersonIcon/13.jpg"));
		iconBtn = new JButton("更换头像...");
		pas = new JPasswordField[2];
		for(int i=0;i < pas.length; ++i){
			pas[i] = new JPasswordField(20);
		}
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER,5,0));
		this.clearB = new JButton();
		this.confirmB = new JButton();
		buttonPane.add(clearB);
		buttonPane.add(confirmB);
		/*性别*/
		ButtonGroup sex = new ButtonGroup();
		boy = new JRadioButton("男",true);
		JRadioButton girl = new JRadioButton("女");
	    sex.add(boy);
		sex.add(girl);
		/*个性签名*/
		sign = new JTextArea(5,20);
		JScrollPane signP = new JScrollPane(sign);
		//////////////
		GridBagLayout gb = new GridBagLayout();
		GridBagConstraints gbc = new GridBagConstraints();
		Container c = this.f.getContentPane();
		c.setLayout(gb);
		for(int i=0;i < labelNames.length; ++i){
			MyUtilities.addGCmp(
					c, label[i], gb, gbc, 
					0,i,1,1,1,1);
		}
		for(int i=0;i < this.tf.length; ++i){
			MyUtilities.addGCmp(c,tf[i],gb,gbc,
					1,i+1,2,1,1,1);
		}
		/*性别和签名*/
		MyUtilities.addGCmp(c,boy,gb,gbc,1,0,1,1,1,1);
		MyUtilities.addGCmp(c,girl,gb,gbc,2,0,1,1,1,1);
		MyUtilities.addGCmp(c,pas[0],gb,gbc,1,5,2,1,1,1);
		MyUtilities.addGCmp(c,pas[1],gb,gbc,1,6,2,1,1,1);
		MyUtilities.addGCmp(c,iconShowLab,gb,gbc,1,7,1,1,1,1);
		MyUtilities.addGCmp(c,iconBtn,gb,gbc,2,7,1,1,1,1);
		MyUtilities.addGCmp(c,signP,gb,gbc,1,8,2,1,1,1);
		//必须填的加上特殊标签
		for(int i=0;i < tip.length; ++i){
			MyUtilities.addGCmp(c,tip[i],gb, gbc,
					3,i+1,1,1,1,1);
		}
		//按钮面板
		MyUtilities.addGCmp(c,buttonPane,gb,gbc,0,9,4,1,1,1);
		this.f.setSize(400,400);
		this.f.setResizable(false);
		this.f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		this.f.setVisible(true);
	}
	private void hookEvent(){
		Action a =  new AbstractAction(){
			public void actionPerformed(ActionEvent e){
				for(int i=0;i < tf.length; ++i){
					tf[i].setText("");
				}
				sign.setText("");
				pas[0].setText("");
				pas[1].setText("");
			}
		};
		a.putValue(Action.NAME, "清除");
		this.clearB.setAction(a);
		Action b = new AbstractAction(){
			public void actionPerformed(ActionEvent e){
				/*check is valid*/
				if(!isValid()){
					JOptionPane.showMessageDialog(null,"必填栏为空或者密码不一致",
							"填写错误",JOptionPane.ERROR_MESSAGE);
					return;
				}
				StringBuffer clientInfo = new StringBuffer();
				clientInfo.append(boy.isSelected()?"男&":"女&");
				/*插入&,方便取出来*/
				for(int i =0;i < tf.length; ++i){
					clientInfo.append(
							tf[i].getText()+"&");
				}
				clientInfo.append(
						String.valueOf(pas[0].getPassword())+"&");
				clientInfo.append(iconShowLab.getName()+"&");
				clientInfo.append(
						sign.getText()+"&");
				/*传给服务器*/
				try {
					adp = new RequestAdapter();
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(
							null, "服务器已经关闭了!","注册失败!",JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				if(adp.sendMsg(new String(clientInfo))){
					f.setTitle("请等待系统回应");
					f.setEnabled(false);
					waitFeedBack();
				}
				
			}
		};
		b.putValue(Action.NAME, "确定");
		this.confirmB.setAction(b);
		/**
		 * 设置选取图标的按钮
		 */
		Action c = new AbstractAction(){
			public void actionPerformed(ActionEvent e){
				FileDialog file = new FileDialog(
						f,"选取图标",FileDialog.LOAD);
				/*获得资源文件的目录*/
				file.setDirectory(
						new File("").getAbsolutePath()+
						File.separator+"PersonIcon");
				file.setModal(true);
				file.setVisible(true);
				String name = file.getFile();
				if(!name.equals("null")){
					try{
						int i = Integer.parseInt((String)(new StringTokenizer(name,".").nextElement()));
						if(i<=0 || i >25)
							throw new Exception();
						//防止图标不再资源文件中选取
						iconShowLab.setName(name);
						iconShowLab.setIcon(new ImageIcon("PersonIcon/"+name));
					}catch(Exception ex)
					{
						JOptionPane.showMessageDialog(null,
								"必须在资源文件里面选取","请重选图标",
								JOptionPane.ERROR_MESSAGE);
					}
					
				}
			}
		};
		c.putValue(Action.NAME, "更换头像...");
		this.iconBtn.setAction(c);
	}
	private void waitFeedBack(){
		Message s = adp.receiveMsg();
		if(s.sysMsg.equals("注册成功")){
			JOptionPane.showMessageDialog(
					null, "您的QQ号码为:"+s.msg,"注册成功!",JOptionPane.INFORMATION_MESSAGE);
		this.f.dispose();
			return;
		}else{
			JOptionPane.showMessageDialog(
					null, "注册失败,请重试!","注册失败!",JOptionPane.INFORMATION_MESSAGE);
			this.f.setEnabled(true);
		}
		this.adp.close();
	}
	private boolean isValid(){
		return !tf[0].getText().equals("")&&
		!tf[1].getText().equals("")&& 
		(String.valueOf(pas[0].getPassword()).equals(
				String.valueOf(pas[1].getPassword())));
	}
	class RequestAdapter{
		private Socket connection;
		private ObjectInputStream input;
		private ObjectOutputStream output;
		public RequestAdapter()throws Exception{
			this.connectS();
			output = new ObjectOutputStream(
					this.connection.getOutputStream());
			input = new ObjectInputStream(
					this.connection.getInputStream());
		}
		private void connectS()throws IOException
		{
			this.connection = new Socket(
					ip,Integer.parseInt(port));
		}
		private boolean sendMsg(String s){
			boolean isOk = true;
			try{
			this.output.writeObject(new Message("申请",s));
			}catch(Exception e){
				isOk = false;
			}
			return isOk;
		}
		private Message receiveMsg(){
			Message msg = null;
			try{
				msg = (Message)(this.input.readObject());
			}catch(Exception e)
			{}
			return msg;
		}
		private void close(){
			try {
				this.connection.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}

⌨️ 快捷键说明

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