一、登陆界面的实现
登陆界面主要使用了JFrame,以及相关的一些组件,并且在界面中加上监听 登陆界面效果图 登陆界面代码Login类
package com.lding.login;
import com.lding.ui.MsgUI;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.DelayQueue;
public class Login {
private OutputStream outputStream;
private String port;
private InputStream inputStream;
private boolean flag=false;
private JTextField jTextField;
private JPasswordField jPasswordField;
public JTextField getjTextField() {
return jTextField;
}
public void setjTextField(JTextField jTextField) {
this.jTextField = jTextField;
}
public JPasswordField getjPasswordField() {
return jPasswordField;
}
public void setjPasswordField(JPasswordField jPasswordField) {
this.jPasswordField = jPasswordField;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public Login(String port){
this.port=port;
}
public Login(InputStream inputStream,OutputStream outputStream, String port) {
this.outputStream = outputStream;
this.port = port;
this.inputStream=inputStream;
}
public OutputStream getOutputStream() {
return outputStream;
}
public void setOutputStream(OutputStream outputStream) {
this.outputStream = outputStream;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public void showUI(Login login) {
JFrame jf=new JFrame();
jf.setTitle("??DDqq登陆界面??");
jf.setSize(500,400);
jf.setDefaultCloseOperation(3);
jf.setLocationRelativeTo(null);
jf.setResizable(false);
FlowLayout fl=new FlowLayout(FlowLayout.CENTER,5,5);
jf.setLayout(fl);
Dimension dim1=new Dimension(500,200);
Dimension dim2=new Dimension(100,50);
Dimension dim3=new Dimension(300,30);
Dimension dim4=new Dimension(100,40);
ImageIcon icon=new ImageIcon("source/Login1.jpg");
JLabel labimg=new JLabel(icon);
labimg.setPreferredSize(dim1);
jf.add(labimg);
JLabel labuser=new JLabel();
labuser.setText("user:");
labuser.setPreferredSize(dim2);
jf.add(labuser);
JTextField textuser=new JTextField();
textuser.setPreferredSize(dim3);
jf.add(textuser);
JLabel labpassword=new JLabel();
labpassword.setText("password:");
labpassword.setPreferredSize(dim2);
jf.add(labpassword);
JPasswordField textPassword=new JPasswordField();
textPassword.setPreferredSize(dim3);
jf.add(textPassword);
JButton button=new JButton();
button.setBorder(BorderFactory.createRaisedBevelBorder());
button.setText("login");
button.setPreferredSize(dim4);
jf.add(button);
jf.setVisible(true);
LoginListener ll=new LoginListener(jf,textuser,textPassword,login);
button.addActionListener(ll);
while (true){
System.out.println(1);
this.flag=ll.isFlag();
if(this.flag)
break;
}
this.jTextField=ll.getjTextField();
this.jPasswordField=ll.getjPasswordField();
System.out.println("ok");
}
}
login的监听类 LoginListener
package com.lding.login;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
public class LoginListener implements ActionListener {
private JTextField jTextField;
private JPasswordField jPasswordField;
private JFrame login;
private Login loginnew;
private boolean flag=false;
public JTextField getjTextField() {
return jTextField;
}
public void setjTextField(JTextField jTextField) {
this.jTextField = jTextField;
}
public JPasswordField getjPasswordField() {
return jPasswordField;
}
public void setjPasswordField(JPasswordField jPasswordField) {
this.jPasswordField = jPasswordField;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public LoginListener(JFrame login, JTextField jTextField, JPasswordField jPasswordField, Login loginnew) {
this.jTextField = jTextField;
this.jPasswordField = jPasswordField;
this.login = login;
this.loginnew=loginnew;
}
@Override
public void actionPerformed(ActionEvent e) {
if(jTextField.getText().equals("lding")&&String.valueOf(jPasswordField.getPassword()).equals("123456")){
this.flag=true;
login.dispose();
}else if(jTextField.getText().equals("Alice")&&String.valueOf(jPasswordField.getPassword()).equals("123456")){
System.out.println(2);
this.flag=true;
login.dispose();
}
}
}
在登陆完毕后,需要跳转到聊天界面,这里采用的方法是通过一个flag标志来判断是否登陆完毕,当登陆完毕后,会创建新的聊天界面,并且登陆页面会被关闭 依然是双端通信,假定一端为客户端,一端为服务端,其实两端都可为用户。
聊天界面运行图 先登陆Alice的账号 密码为123456 登陆成功后显示ok,并等待连接 再登陆冷丁的账号,密码为123456 当lding账号登陆成功后完成连接,这时候会弹出两个新的JFram窗口,为聊天见面 随后可以发送消息,消息可以实时显示时间,并显示用户名 一方发送的消息会在另一方的窗口上实时显示出来 MsgClient类代码
package com.lding.net;
import com.lding.login.Login;
import com.lding.ui.MsgUI;
import javax.swing.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MsgClient {
static public JTextField jTextField;
static private JPasswordField jPasswordField;
public JTextField getjTextField() {
return jTextField;
}
public void setjTextField(JTextField jTextField) {
this.jTextField = jTextField;
}
public JPasswordField getjPasswordField() {
return jPasswordField;
}
public void setjPasswordField(JPasswordField jPasswordField) {
this.jPasswordField = jPasswordField;
}
public static void main(String[] args) throws IOException {
String port="客户端";
Login login=new Login(port);
login.showUI(login);
if(login.isFlag()){
jTextField=login.getjTextField();
String name=jTextField.getText();
System.out.println(login.isFlag());
Socket socket=new Socket("127.0.0.1",8888);
InputStream inputStream=socket.getInputStream();
OutputStream outputStream=socket.getOutputStream();
MsgUI msgUI=new MsgUI(outputStream,name);
JTextPane showmsgpane=msgUI.initUI(name);
int count=1;
while(true){
int msglenth=inputStream.read();
System.out.println("消息长度:"+msglenth);
byte[] msgbytes=new byte[msglenth];
inputStream.read(msgbytes);
count++;
if(count%2==0){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = df.format(new Date());
String getmsg=new String(msgbytes);
String msg=showmsgpane.getText();
showmsgpane.setText(msg+date+" ["+getmsg+"]"+"说:"+"\n");
}else{
String getmsg=new String(msgbytes);
String msg=showmsgpane.getText();
showmsgpane.setText(msg+getmsg+"\n");
}
}
}
}
}
MsgServer代码
package com.lding.net;
import com.lding.login.Login;
import com.lding.ui.MsgUI;
import javax.swing.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MsgServer {
static public JTextField jTextField;
static private JPasswordField jPasswordField;
public JTextField getjTextField() {
return jTextField;
}
public void setjTextField(JTextField jTextField) {
this.jTextField = jTextField;
}
public JPasswordField getjPasswordField() {
return jPasswordField;
}
public void setjPasswordField(JPasswordField jPasswordField) {
this.jPasswordField = jPasswordField;
}
public void MsgServerInit(){
}
public static void main(String[] args) throws IOException {
String port="服务端";
Login login=new Login(port);
login.showUI(login);
ServerSocket serverSocket=new ServerSocket(8888);
System.out.println("等待连接!!");
Socket clientsocket=serverSocket.accept();
System.out.println("连接成功!!");
jTextField=login.getjTextField();
String name=jTextField.getText();
InputStream inputStream=clientsocket.getInputStream();
OutputStream outputStream=clientsocket.getOutputStream();
MsgUI msgUI=new MsgUI(outputStream,name);
JTextPane showmsgpane=msgUI.initUI(name);
int num=1;
while(true){
int msglenth=inputStream.read();
System.out.println("消息长度是:"+msglenth);
byte[] msgbytes=new byte[msglenth];
inputStream.read(msgbytes);
num++;
if(num%2==0){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = df.format(new Date());
String getmsg=new String(msgbytes);
String msg=showmsgpane.getText();
showmsgpane.setText(msg+date+" ["+getmsg+"]"+"说:"+"\n");
}else{
String getmsg=new String(msgbytes);
String msg=showmsgpane.getText();
showmsgpane.setText(msg+getmsg+"\n");
}
}
}
}
之后会丰富更多功能 敬请期待!
感谢您的观看!!!
|