一个c/s的聊天室(1)
一个c/s的聊天室(1)
//Client
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.util.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.*;
//登录窗口
public class Frame2 extends JFrame {
JPanel contentPane;
JLabel jLabel1 = new JLabel();
JTextField jTextField1 = new JTextField();
ButtonGroup buttonGroup1 = new ButtonGroup();
JRadioButton jRadioButton1 = new JRadioButton();
JRadioButton jRadioButton2 = new JRadioButton();
JLabel jLabel2 = new JLabel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
String sex="男";
String name=null;
boolean bool=false;
Socket socket;
DataInputStream in;
DataOutputStream out;
public Frame2() {
try {
// setDefaultCloseOperation(EXIT_ON_CLOSE);
addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
}
}
);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
this.setResizable(false);
setSize(new Dimension(400, 300));
setTitle("登录界面");
jLabel1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 15));
jLabel1.setHorizontalAlignment(SwingConstants.RIGHT);
jLabel1.setText("请输入您的昵称:");
jLabel1.setBounds(new Rectangle(53, 70, 113, 44));
jTextField1.setBounds(new Rectangle(182, 75, 154, 33));
jTextField1.addKeyListener(new Frame2_jTextField1_keyAdapter(this));
jRadioButton1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 13));
jRadioButton1.setSelected(true);
jRadioButton1.setText("男");
jRadioButton1.setBounds(new Rectangle(186, 114, 91, 25));
jRadioButton1.addActionListener(new Frame2_jRadioButton1_actionAdapter(this));
jRadioButton2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 13));
jRadioButton2.setText("女");
jRadioButton2.setBounds(new Rectangle(187, 140, 91, 25));
jRadioButton2.addActionListener(new Frame2_jRadioButton2_actionAdapter(this));
jLabel2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 15));
jLabel2.setToolTipText("");
jLabel2.setText("性别:");
jLabel2.setBounds(new Rectangle(136, 131, 34, 16));
jButton1.setBounds(new Rectangle(182, 218, 71, 25));
jButton1.setText("确定");
jButton1.addActionListener(new Frame2_jButton1_actionAdapter(this));
jButton2.setBounds(new Rectangle(264, 218, 71, 25));
jButton2.setText("取消");
jButton2.addActionListener(new Frame2_jButton2_actionAdapter(this));
contentPane.add(jLabel1);
contentPane.add(jTextField1);
contentPane.add(jRadioButton1);
contentPane.add(jRadioButton2);
contentPane.add(jLabel2);
contentPane.add(jButton2);
contentPane.add(jButton1);
buttonGroup1.add(jRadioButton1);
buttonGroup1.add(jRadioButton2);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
setVisible(true);
}
public void setClection(Socket soc,DataInputStream din,DataOutputStream dout ){
socket=soc;
in=din;
out=dout;
}
public boolean getYesOrNo(){
return bool;
}
public String getName(){
return name;
}
public String getSex(){
return sex;
}
public void jRadioButton1_actionPerformed(ActionEvent e) {
sex="男";
}
public void jRadioButton2_actionPerformed(ActionEvent e) {
sex="女";
}
public void jButton1_actionPerformed(ActionEvent e) {
if(jTextField1.getText().equals("")){
jTextField1.setText("请输入昵称");
return;
}
if(jTextField1.getText().equals(" ")){
jTextField1.setText("请输入昵称");
return;
}
if(jTextField1.getText().equals(" ")){
jTextField1.setText("请输入昵称");
return;
}
if(jTextField1.getText().equals("请输入昵称")){
jTextField1.setText("请输入昵称");
return;
}
name=jTextField1.getText();
try{
out.writeUTF("新的朋友:" + name +"/"+ sex);
}
catch(Exception ee){
return;
}
}
public void jButton2_actionPerformed(ActionEvent e) {
jTextField1.setText("");
}
public void jTextField1_keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER ){
if(jTextField1.getText().equals("")){
jTextField1.setText("请输入昵称");
return;
}
if(jTextField1.getText().equals(" ")){
jTextField1.setText("请输入昵称");
return;
}
if(jTextField1.getText().equals(" ")){
jTextField1.setText("请输入昵称");
return;
}
if(jTextField1.getText().equals("请输入昵称")){
jTextField1.setText("请输入昵称");
return;
}
name=jTextField1.getText();
try{
out.writeUTF("新的朋友:" + name +"/"+ sex);
if(in.readUTF().startsWith("可以") ){
bool=true;
}
else{
jTextField1.setText("请重新输入昵称") ;
return;
}
}
catch(Exception ee){
return;
}
setVisible(false);
}
}
} (未完待续)
//Client
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.util.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.*;
//登录窗口
public class Frame2 extends JFrame {
JPanel contentPane;
JLabel jLabel1 = new JLabel();
JTextField jTextField1 = new JTextField();
ButtonGroup buttonGroup1 = new ButtonGroup();
JRadioButton jRadioButton1 = new JRadioButton();
JRadioButton jRadioButton2 = new JRadioButton();
JLabel jLabel2 = new JLabel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
String sex="男";
String name=null;
boolean bool=false;
Socket socket;
DataInputStream in;
DataOutputStream out;
public Frame2() {
try {
// setDefaultCloseOperation(EXIT_ON_CLOSE);
addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
}
}
);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
this.setResizable(false);
setSize(new Dimension(400, 300));
setTitle("登录界面");
jLabel1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 15));
jLabel1.setHorizontalAlignment(SwingConstants.RIGHT);
jLabel1.setText("请输入您的昵称:");
jLabel1.setBounds(new Rectangle(53, 70, 113, 44));
jTextField1.setBounds(new Rectangle(182, 75, 154, 33));
jTextField1.addKeyListener(new Frame2_jTextField1_keyAdapter(this));
jRadioButton1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 13));
jRadioButton1.setSelected(true);
jRadioButton1.setText("男");
jRadioButton1.setBounds(new Rectangle(186, 114, 91, 25));
jRadioButton1.addActionListener(new Frame2_jRadioButton1_actionAdapter(this));
jRadioButton2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 13));
jRadioButton2.setText("女");
jRadioButton2.setBounds(new Rectangle(187, 140, 91, 25));
jRadioButton2.addActionListener(new Frame2_jRadioButton2_actionAdapter(this));
jLabel2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 15));
jLabel2.setToolTipText("");
jLabel2.setText("性别:");
jLabel2.setBounds(new Rectangle(136, 131, 34, 16));
jButton1.setBounds(new Rectangle(182, 218, 71, 25));
jButton1.setText("确定");
jButton1.addActionListener(new Frame2_jButton1_actionAdapter(this));
jButton2.setBounds(new Rectangle(264, 218, 71, 25));
jButton2.setText("取消");
jButton2.addActionListener(new Frame2_jButton2_actionAdapter(this));
contentPane.add(jLabel1);
contentPane.add(jTextField1);
contentPane.add(jRadioButton1);
contentPane.add(jRadioButton2);
contentPane.add(jLabel2);
contentPane.add(jButton2);
contentPane.add(jButton1);
buttonGroup1.add(jRadioButton1);
buttonGroup1.add(jRadioButton2);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
setVisible(true);
}
public void setClection(Socket soc,DataInputStream din,DataOutputStream dout ){
socket=soc;
in=din;
out=dout;
}
public boolean getYesOrNo(){
return bool;
}
public String getName(){
return name;
}
public String getSex(){
return sex;
}
public void jRadioButton1_actionPerformed(ActionEvent e) {
sex="男";
}
public void jRadioButton2_actionPerformed(ActionEvent e) {
sex="女";
}
public void jButton1_actionPerformed(ActionEvent e) {
if(jTextField1.getText().equals("")){
jTextField1.setText("请输入昵称");
return;
}
if(jTextField1.getText().equals(" ")){
jTextField1.setText("请输入昵称");
return;
}
if(jTextField1.getText().equals(" ")){
jTextField1.setText("请输入昵称");
return;
}
if(jTextField1.getText().equals("请输入昵称")){
jTextField1.setText("请输入昵称");
return;
}
name=jTextField1.getText();
try{
out.writeUTF("新的朋友:" + name +"/"+ sex);
}
catch(Exception ee){
return;
}
}
public void jButton2_actionPerformed(ActionEvent e) {
jTextField1.setText("");
}
public void jTextField1_keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER ){
if(jTextField1.getText().equals("")){
jTextField1.setText("请输入昵称");
return;
}
if(jTextField1.getText().equals(" ")){
jTextField1.setText("请输入昵称");
return;
}
if(jTextField1.getText().equals(" ")){
jTextField1.setText("请输入昵称");
return;
}
if(jTextField1.getText().equals("请输入昵称")){
jTextField1.setText("请输入昵称");
return;
}
name=jTextField1.getText();
try{
out.writeUTF("新的朋友:" + name +"/"+ sex);
if(in.readUTF().startsWith("可以") ){
bool=true;
}
else{
jTextField1.setText("请重新输入昵称") ;
return;
}
}
catch(Exception ee){
return;
}
setVisible(false);
}
}
} (未完待续)
- 上篇文章:Java文件操作详解
- 下篇文章:一个c/s的聊天室(2)