- 使用图形用户界面
- 有服务器和客户端,服务器监听客户端的登陆。即:如果A客户端登陆服务器,则服务器界面上的A字母对应的红灯变成绿色
- 绿灯表示连接,红灯表示断线
提示:socket的监听方法
Client.java
package keshe;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
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.*;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class Client extends JFrame implements ActionListener, KeyListener
{
JPanel contentPane;
BackgroundPanel bgp;
JTextField textField;
JPasswordField passwordField;
Socket socket;
DataOutputStream out=null;
DataInputStream in=null;
String name=null;
String password=null;
JButton Button1 = new JButton("登录");
JButton Button2 = new JButton("离线");
JLabel Label2 = new JLabel("");
JButton Button = new JButton("用户");
int flag=0;
int yonghu=0;
class BackgroundPanel extends JPanel
{
Image im;
public BackgroundPanel(Image im)
{
this.im=im;
this.setOpaque(true);
}
public void paintComponent(Graphics g)
{
super.paintComponents(g);
g.drawImage(im,0,0,this.getWidth(),this.getHeight(),this);
}
}
public Client()
{
bgp=new BackgroundPanel((new ImageIcon("1.jpg")).getImage());
bgp.setBounds(0,0,450,300);
setTitle("登录界面");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel Label = new JLabel("用户名");
Label.setFont(new Font("黑体", Font.PLAIN, 18));
Label.setForeground(Color.WHITE);
Label.setBounds(71, 119,