📄 login.java
字号:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.*;
import java.net.Socket;
import java.util.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class Login extends JFrame {
JLabel lblUsername, lblPassword;
JComboBox comboUsername;
JPasswordField txtPassword;
JButton jbnButton1, jbnButton2;
BufferedReader read1;
PrintWriter write1;
JLabel lblname,lblwelcome;
String s32;
TreeSet tr1;
Socket clientSocket;
BufferedReader sin;
PrintWriter sout;
Liaotianshi lt1;
String ServerIp="192.168.1.134";
int ServerPort=10002;
public Login(String title) {
super(title);
tr1=new TreeSet();
String s="";
try{
comboUsername = new JComboBox();
if ((new File("user.txt")).exists()){
read1 = new BufferedReader(new FileReader("user.txt"));
while ((s = read1.readLine()) != null)
{
comboUsername.addItem(s);
comboUsername.setSelectedIndex(0);
tr1.add(s);
}
read1.close();
}
lblname= new JLabel("好朋友聊天室");
lblname.setFont(new Font("宋体",Font.PLAIN,20));
lblname.setForeground(Color.RED);
lblwelcome= new JLabel("欢迎您!");
lblUsername = new JLabel("用户名:");
lblPassword = new JLabel("密 码:");
txtPassword = new JPasswordField(10);
comboUsername.setEditable(true);
// comboUsername.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent e) {
// //
//
// }
// });
jbnButton1 = new JButton("登录");
jbnButton2 = new JButton("取消");
jbnButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String user,pass;
user=(String)comboUsername.getSelectedItem();
pass=txtPassword.getText();
if (connectserver(user,pass)==1) {
//将登陆成功的用户记入用户列表.
tr1.add(user);
write1 =new PrintWriter( new BufferedWriter(new FileWriter("user.txt",false)));
Iterator it1;
it1=tr1.iterator();
while (it1.hasNext()){
write1.println(it1.next());
}
write1.close();
//打开聊天室界面并隐藏登陆界面.
lt1 = new Liaotianshi("聊天室",user,sin,sout);
lt1.pack();
setVisible(false);
lt1.setVisible(true);
lt1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
System.out.println("登录成功!");
}
else {
System.out.println("登录失败!");
}
}
catch (IOException e2){
System.out.println(e2.toString());
}
}
});
jbnButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//退出
sout.println("/logout");
sout.flush();
System.exit(0);
}
});
jbnButton1.setMnemonic(KeyEvent.VK_Y);
jbnButton2.setMnemonic(KeyEvent.VK_X);
// getContentPane().setLayout(new FlowLayout());
Insets is = new Insets(5,5,5,5);
getContentPane().setLayout(new GridBagLayout());
GridBagConstraints gBC = new GridBagConstraints();
gBC.anchor=GridBagConstraints.CENTER;
gBC.fill = GridBagConstraints.NONE;
gBC.gridx = 1;
gBC.gridy = 1;
gBC.gridwidth = 2;
gBC.gridheight=1;
gBC.insets=is;
getContentPane().add(lblname,gBC);
gBC.anchor=GridBagConstraints.CENTER;
gBC.fill = GridBagConstraints.NONE;
gBC.gridy = 2;
getContentPane().add(lblwelcome,gBC);
gBC.anchor=GridBagConstraints.WEST;
gBC.fill = GridBagConstraints.NONE;
gBC.gridx = 1;
gBC.gridy = 3;
gBC.gridwidth = 1;
gBC.gridheight=1;
gBC.insets=is;
getContentPane().add(lblUsername,gBC);
gBC.gridx = 1;
gBC.gridy = 4;
gBC.gridwidth = 1;
gBC.gridheight=1;
gBC.insets=is;
getContentPane().add(lblPassword,gBC);
gBC.anchor=GridBagConstraints.WEST;
gBC.fill = GridBagConstraints.HORIZONTAL;
gBC.gridx = 2;
gBC.gridy = 3;
gBC.gridwidth = 1;
gBC.gridheight=1;
gBC.insets=is;
getContentPane().add(comboUsername,gBC);
gBC.gridx = 2;
gBC.gridy = 4;
gBC.gridwidth = 1;
gBC.gridheight=1;
gBC.insets=is;
getContentPane().add(txtPassword,gBC);
JPanel jp1=new JPanel();
jp1.setLayout(new FlowLayout());
jp1.add(jbnButton1,gBC);
jp1.add(jbnButton2,gBC);
gBC.gridx = 1;
gBC.gridy = 5;
gBC.gridwidth = 2;
gBC.gridheight=1;
gBC.insets=new Insets(10,10,10,10);
getContentPane().add(jp1,gBC);
}
catch (IOException e){
System.out.println(e.toString());
}
}
public int connectserver(String username,String password){
String reply;
try {
clientSocket=new Socket(ServerIp, ServerPort);
sin = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
sout = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
sout.println(username);
sout.flush();
sout.println(password);
sout.flush();
reply=sin.readLine();
if (reply.equals("login succ") )
return 1;
else
return 0;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
public static void main(String[] args) {
Login frm = new Login("登陆界面");
frm.pack();
frm.setVisible(true);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class Liaotianshi extends JFrame {
JList ListUserName;
JTextArea Liaotianxinxi;
JTextArea Fayan;
JButton Fasong;
JLabel lblname;
String username3;
BufferedReader r3;
PrintWriter w3;
String SpeakUser;
public Liaotianshi(String title,String username2,BufferedReader r2,PrintWriter w2){
super(title);
username3=username2;
r3=r2;
w3=w2;
//处理聊天室界面
lblname= new JLabel("好朋友聊天室 ");
lblname.setFont(new Font("宋体",Font.PLAIN,20));
Liaotianxinxi = new JTextArea(15, 1);
Liaotianxinxi.setEditable(false);
String text=" 好朋友聊天室 欢迎你! \n";
Liaotianxinxi.append(text);
Fayan=new JTextArea("");
Fasong=new JButton("发送信息");
DefaultListModel lm = new DefaultListModel();
ListUserName=new JList(lm);
lm.add(0, "所有人");
JScrollPane scrollPane_3=new JScrollPane(ListUserName,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane_3.setPreferredSize(new Dimension(100, 400));
ListUserName.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ListUserName.setSelectedIndex(0);
Fasong.setMnemonic(KeyEvent.VK_Y);
JScrollPane scrollPane_1 = new JScrollPane(Fayan,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JScrollPane scrollPane_2 = new JScrollPane(Liaotianxinxi,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane_1.setPreferredSize(new Dimension(500,100));
scrollPane_2.setPreferredSize(new Dimension(400,400));
Insets is = new Insets(5,5,5,5);
JSplitPane jsp1=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
jsp1.setLeftComponent(scrollPane_2);
jsp1.setRightComponent(scrollPane_3);
jsp1.setDividerLocation(400);
jsp1.setAutoscrolls(true);
jsp1.setDividerSize(5);
getContentPane().setLayout(new BorderLayout(5,5));
getContentPane().add(lblname,BorderLayout.PAGE_START);
getContentPane().add(jsp1,BorderLayout.CENTER);
JPanel jp1=new JPanel();
jp1.setLayout(new BorderLayout(5,5));
jp1.add(scrollPane_1,BorderLayout.CENTER);
jp1.add(Fasong,BorderLayout.EAST);
jp1.setVisible(true);
JPanel jp2=new JPanel();
getContentPane().add(jp1,BorderLayout.PAGE_END);
//增加监听.
Fasong.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String speak1;
getspeakuser();
speak(username3,r3,w3,Fayan.getText(),"/TALK");
Fayan.setText("");
}
});
Fayan.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
// TODO 自动生成方法存根
if (e.isControlDown() && e.getKeyCode()==KeyEvent.VK_ENTER){
String speak1;
speak(username3,r3,w3,Fayan.getText(),"/TALK");
Fayan.setText("");
}
}
public void keyReleased(KeyEvent e) {
// TODO 自动生成方法存根
}
public void keyTyped(KeyEvent e) {
// TODO 自动生成方法存根
}
});
//创建接受线程.
ReceiveThread rt1 = new ReceiveThread(r2,Liaotianxinxi,lm);
}
public void getspeakuser(){
if (ListUserName.getSelectedValue() == null) {
ListUserName.setSelectedIndex(0);
}
SpeakUser=ListUserName.getSelectedValue().toString();
}
public void speak(String username,BufferedReader r1,PrintWriter w1,String word,String msg){
try {
if (word.equals("")){
return;
}
if (msg.equals("/TALK")){
w1.println("/TALK");
w1.println(username3+"对"+SpeakUser+"说:\n"+word);
w1.println("TALK/");
w1.flush();
}
// if (msg.equals("/GetUserList")){
// w1.println("/GetUserList");
// w1.flush();
// }
} catch (Exception e) {
e.printStackTrace();
}
}
}
class ReceiveThread extends Thread{
BufferedReader r5;
JTextArea liaotianxinxi2;
DefaultListModel lm2;
public ReceiveThread(BufferedReader r4,JTextArea liaotianxinxi,DefaultListModel lm)
{
r5=r4;
liaotianxinxi2=liaotianxinxi;
lm2=lm;
this.start();
}
public void run() {
String line;
try {
while (true) {
line = r5.readLine();
System.out.println(line);
if (line.equals("/TALK")){
line = r5.readLine();
while (line.equals("TALK/") == false){
liaotianxinxi2.append(line+"\n");
liaotianxinxi2.setCaretPosition(liaotianxinxi2.getText().length());
line = r5.readLine();
}
}
if (line.equals("/RefreshUserList")){
int i=0;
lm2.clear();
lm2.add(i, "所有人");
line=r5.readLine();
while (line.equals("SendUserList/") == false){
i++;
lm2.add(i, line);
line = r5.readLine();
}
}
if (line.equals("/adduser")){
line = r5.readLine();
liaotianxinxi2.append(line+" 微笑地走入聊天室.\n");
}
if (line.equals("/lessuser")){
line = r5.readLine();
liaotianxinxi2.append(line+" 匆匆忙忙地离开聊天室.\n");
}
if (line.equals("/QUIT")) break;
}
} catch (IOException e) {
//e.printStackTrace();
return;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -