📄 login.java
字号:
buttonClear = new JButton("清空");
buttonClear.setFont(new Font("宋体",Font.BOLD,25));
buttonClear.setForeground(Color.black);
buttonClear.addActionListener(this);
buttonUserLogin = new JButton("注册");
buttonUserLogin.setFont(new Font("宋体",Font.BOLD,25));
buttonUserLogin.setForeground(Color.black);
buttonUserLogin.addActionListener(this);
gbl = new GridBagLayout();
gbc = new GridBagConstraints();
panel.setLayout(gbl);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 0;
gbc.gridheight = 1;
gbl.setConstraints(labelWelcomeLogin,gbc);
panel.add(labelWelcomeLogin);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbl.setConstraints(labelUserName,gbc);
panel.add(labelUserName);
gbc.gridx = 2;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbl.setConstraints(textFieldUserName,gbc);
panel.add(textFieldUserName);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbl.setConstraints(labelUserPassword,gbc);
panel.add(labelUserPassword);
gbc.gridx = 2;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbc.gridheight =1;
gbl.setConstraints(textUserPassword,gbc);
panel.add(textUserPassword);
gbc.gridx = 1;
gbc.gridy = 3;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbl.setConstraints(buttonLogin,gbc);
panel.add(buttonLogin);
gbc.gridx = 2;
gbc.gridy = 3;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbl.setConstraints(buttonClear,gbc);
panel.add(buttonClear);
gbc.gridx = 3;
gbc.gridy = 3;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbl.setConstraints(buttonUserLogin,gbc);
panel.add(buttonUserLogin);
frame2.getContentPane().add(panel);
frame2.setSize(450,320);
frame2.setVisible(true);
frame2.setResizable(false);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]){
new Login();
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == buttonLogin){
new ChatClient();
System.out.println(textFieldUserName.getText()+"用户登录成功!!");
userName = textFieldUserName.getText();
}else{
System.out.println("请你先注册后登录!!");
}
if(e.getSource() == buttonClear){
textFieldUserName.setText(" ");
textUserPassword.setText(" ");
}
if(e.getSource()==buttonUserLogin){
new ChatClientLogin();
}
}
/*********************聊天室主界面*******************/
class ChatClient extends JFrame implements ActionListener,Runnable{
JFrame frame = new JFrame("蓝天在线聊天室客户主界面");
JPanel panel;
GridBagLayout gbl;
GridBagConstraints gbc;
JLabel labelChatWindow;
JLabel labelOnLineUser;
JLabel labelSendMessage;
JTextArea textAreaChat;
JTextArea textAreaOnLineUser;
JTextField textFieldSendMessage;
JComboBox comboBox,comboBoxName;
JScrollPane scrollPaneTextAreaChat,scrollPaneOnLineUser;
JButton buttonSend;
JRadioButton buttonPublic;
JRadioButton buttonPrivate;
JButton buttonClear;
JButton buttonExit;
Socket socket;
Thread thread = new Thread(this);
public ChatClient(){
panel = new JPanel();
gbl = new GridBagLayout();
gbc = new GridBagConstraints();
panel.setLayout(gbl);
panel.setBackground(Color.orange);
labelChatWindow = new JLabel("聊城欢迎用户进入社区!");
labelChatWindow.setFont(new Font("宋体",Font.BOLD,26));
labelChatWindow.setForeground(Color.black);
labelOnLineUser = new JLabel("在线用户:");
labelOnLineUser.setFont(new Font("宋体",Font.BOLD,26));
labelOnLineUser.setForeground(Color.black);
labelSendMessage = new JLabel("发送消息:");
labelSendMessage.setForeground(Color.black);
textAreaChat = new JTextArea(23,42);
textAreaOnLineUser = new JTextArea(23,18);
textFieldSendMessage = new JTextField(37);
comboBox = new JComboBox(new String []{"对大家说 ","对你悄悄地说"});
comboBoxName = new JComboBox();
comboBoxName.addItem("对大家说:");
scrollPaneTextAreaChat = new JScrollPane(textAreaChat);
scrollPaneOnLineUser = new JScrollPane(textAreaOnLineUser);
buttonSend = new JButton("发送 ");
buttonSend.setFont(new Font("宋体",Font.BOLD,18));
buttonSend.setForeground(Color.black);
buttonSend.addActionListener(this);
buttonPublic = new JRadioButton("公聊");
buttonPublic.setForeground(Color.black);
buttonPublic.addActionListener(this);
buttonPrivate = new JRadioButton("私聊");
buttonPrivate.setForeground(Color.black);
buttonPrivate.addActionListener(this);
buttonClear = new JButton("清空");
buttonClear.setFont(new Font("宋体",Font.BOLD,18));
buttonClear.setForeground(Color.black);
buttonClear.addActionListener(this);
buttonExit = new JButton("关闭");
//buttonExit.setFont(new Font("宋体",Font.BOLD,18));
buttonExit.setForeground(Color.black);
buttonExit.addActionListener(this);
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.WEST;
gbc.weightx =0.1;
gbc.weighty =1;
gbc.gridx =2;
gbc.gridy =0;
gbc.gridwidth =1;
gbc.gridheight = 1;
gbl.setConstraints(labelChatWindow,gbc);
panel.add(labelChatWindow);
gbc.gridx =0;
gbc.gridy =1;
gbc.gridwidth =4;
gbc.gridheight =1;
gbl.setConstraints(scrollPaneTextAreaChat,gbc);
panel.add(scrollPaneTextAreaChat);
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridx =4;
gbc.gridy =0;
gbc.gridwidth =1;
gbc.gridheight = 1;
gbl.setConstraints(labelOnLineUser,gbc);
panel.add(labelOnLineUser);
gbc.anchor = GridBagConstraints.EAST;
gbc.gridx =4;
gbc.gridy =1;
gbc.gridwidth =1;
gbc.gridheight =1;
gbl.setConstraints(scrollPaneOnLineUser,gbc);
panel.add(scrollPaneOnLineUser);
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx = 0;
gbc.gridy = 2 ;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbl.setConstraints(labelSendMessage,gbc);
panel.add(labelSendMessage);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth =3;
gbc.gridheight = 1;
gbl.setConstraints(textFieldSendMessage,gbc);
panel.add(textFieldSendMessage);
gbc.gridx = 4;
gbc.gridy = 2;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbl.setConstraints(comboBox,gbc);
panel.add(comboBox);
gbc.gridy = 3;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbl.setConstraints(buttonClear,gbc);
panel.add(buttonClear);
gbc.gridx = 1;
gbc.gridy = 3;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbl.setConstraints(buttonSend,gbc);
panel.add(buttonSend);
gbc.gridx = 2;
gbc.gridy = 3;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbl.setConstraints(buttonPublic,gbc);
panel.add(buttonPublic);
gbc.gridx = 3;
gbc.gridy = 3;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbl.setConstraints(buttonPrivate,gbc);
panel.add(buttonPrivate);
gbc.anchor = GridBagConstraints.EAST;
gbc.gridx = 4;
gbc.gridy = 3;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbl.setConstraints(buttonExit,gbc);
panel.add(buttonExit);
thread.start();
frame.getContentPane().add(panel);
frame.setSize(680,570);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void run(){
try{
StringTokenizer tokenizer;
String string;
InetAddress address=InetAddress.getByName(null);
Socket socket=new Socket(address,ChatServer.PORT);
try{
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
output = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
output.println(userName);
output.flush();
while(true){
string = input.readLine();
tokenizer = new StringTokenizer(string,"\t");
loginName = tokenizer.nextToken();
if(loginName.equals("^userName^")){
loginName = tokenizer.nextToken();
loginNameList.add(loginName);
textAreaOnLineUser.setText("");
for(int i = 0;i< loginNameList.size();i++){
textAreaOnLineUser.append("欢迎" + " " + loginNameList.get(i) + " " + "用户聊天!\n");
}
}else{
if(loginName.length()!=0){
textAreaChat.append(string + "\n");
}
}
}
}catch(Exception e){}
}catch(IOException e){}
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == buttonSend){
if((textFieldSendMessage.getText().length()) != 0){
try{
if(comboBoxName.getSelectedItem().equals(loginName)){
JOptionPane.showMessageDialog(null,"请不要同自己说话! ! !","错误操作",JOptionPane.INFORMATION_MESSAGE);
}else{
output.println(userName + " " + (String)comboBoxName.getSelectedItem() + " " + textFieldSendMessage.getText());
output.flush();
textFieldSendMessage.setText("");
}
}catch(Exception ex){
textAreaChat.append( textFieldSendMessage.getText() + "发送消息失败!");
}
}
}
if(e.getSource() == buttonClear){
textFieldSendMessage.setText("");
}
if(e.getSource() == buttonExit){
System.exit(1);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -