📄 loginchat.java
字号:
package client;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class LoginChat {
private JPasswordField passwordField;
private JTextField textField;
/**
* 给服务端发送消息的输出流
*/
public static DataOutputStream sendToServer;
/**
* 接受服务端发送的消息的输入流
*/
public static DataInputStream receiveFromServer;
/**
* 该客户端的Socket
*/
public static Socket clientSocket;
private JFrame frame;
/**
* Launch the application
*
* @param args
*/
public static void main(String args[]) {
try {
//UIManager.setLookAndFeel((LookAndFeel) Class.forName("com.incors.plaf.kunststoff.KunststoffLookAndFeel").newInstance());
//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
LoginChat window = new LoginChat();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the application
*/
public LoginChat() {
initialize();
}
/**
* Initialize the contents of the frame
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setLayout(null);
frame.setResizable(false);
frame.setBounds(100, 100, 500, 375);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JLabel label = new JLabel();
label.setBounds(106, 115, 42, 20);
label.setFont(new Font("", Font.PLAIN, 14));
label.setText("号码:");
frame.getContentPane().add(label);
textField = new JTextField();
textField.setBounds(148, 116, 196, 20);
frame.getContentPane().add(textField);
final JLabel label_1 = new JLabel();
label_1.setBounds(106, 172, 42, 20);
label_1.setFont(new Font("", Font.PLAIN, 14));
label_1.setText("密码:");
frame.getContentPane().add(label_1);
passwordField = new JPasswordField();
passwordField.setBounds(148, 172, 196, 21);
frame.getContentPane().add(passwordField);
final JEditorPane localhostEditorPane = new JEditorPane();
localhostEditorPane.setText("localhost");
localhostEditorPane.setBounds(106, 303, 176, 21);
frame.getContentPane().add(localhostEditorPane);
final JEditorPane editorPane_1 = new JEditorPane();
editorPane_1.setText("8000");
editorPane_1.setBounds(324, 303, 68, 21);
frame.getContentPane().add(editorPane_1);
final JLabel label_2 = new JLabel();
label_2.setBounds(10, 303, 100, 21);
label_2.setHorizontalAlignment(SwingConstants.CENTER);
label_2.setText("服务器IP地址:");
frame.getContentPane().add(label_2);
final JLabel label_3 = new JLabel();
label_3.setBounds(288, 306, 43, 15);
label_3.setText("端口:");
frame.getContentPane().add(label_3);
final JButton button = new JButton();
button.setText("登陆");
button.setBounds(149, 217, 80, 24);
button.addActionListener(new ActionListener() {
@SuppressWarnings("deprecation")
public void actionPerformed(final ActionEvent e) {
String text = passwordField.getText();
if (!textField.getText().trim().trim().equals("")
&& !text.trim().equals("")) {
try {
clientSocket = new Socket(localhostEditorPane.getText().trim(), new Integer(editorPane_1.getText().trim()));
receiveFromServer = new DataInputStream(clientSocket
.getInputStream());
sendToServer = new DataOutputStream(clientSocket
.getOutputStream());
/*
* 这个成员登陆
*/
sendToServer.writeUTF("Login@"
+ new Integer(textField.getText().trim()) + "@"
+ text.trim());
new Client(receiveFromServer,sendToServer,frame,new Integer(textField.getText().trim())).start();
} catch (UnknownHostException eee) {
JOptionPane.showMessageDialog(frame, "服务器没有开启", "连接失败",
JOptionPane.ERROR_MESSAGE);
} catch (IOException ee) {
JOptionPane.showMessageDialog(frame, "服务器没有开启", "连接失败",
JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(frame, "请完整输入号码和密码",
"输入完整性检查", JOptionPane.ERROR_MESSAGE);
}
}
});
frame.getContentPane().add(button);
final JButton button_1 = new JButton();
button_1.setText("取消");
button_1.setBounds(269, 217, 75, 24);
frame.getContentPane().add(button_1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -