免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1538 | 回复: 0
打印 上一主题 下一主题

这个小程序怎么实现与服务器互联? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-12-27 20:47 |只看该作者 |倒序浏览
本帖最后由 PrtScreen 于 2012-12-27 20:57 编辑

这个小程序怎么能两个人玩,就是再建一个,两个人可以联机对战。图直接放在D盘。本人新手,求解



import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.*;


public class E1301 extends JFrame implements ActionListener{

    /**
         *
         */
        private static final long serialVersionUID = 2028547680042899648L;
        JButton b1,b2,b3,b4,b5,b6,b7,b8,b9;
    JButton but1,but2,but3;
    JPanel p1,p2,p3;
    JLabel L1,L2;
    JLabel jg;
    Icon iconObj= new ImageIcon("d:\\quan1.jpg";
    Icon iconObj2= new ImageIcon("d:\\cha1.jpg";
        int i=0;
        int pos1=0,pos2=0,pos3=0,pos4=0,pos5=0;
        int pos6=0,pos7=0,pos8=0,pos9=0;
        int ks=0;
        JTextField t1;
        String xm;
        ServerSocket server=null;
        Socket socket=null;
        PrintWriter os=null;
    BufferedReader is=null;
    String strMessage;
       
        String driver="sun.jdbc.odbc.JdbcOdbcDriver";
    String url="jdbcdbc:first";
    String user="";
    String password="";
   
       
    E1301(){
        super("欢迎使用#字棋";
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container c=getContentPane();
        c.setLayout(null);
        GridLayout gl=new GridLayout(3,3);
        L1=new JLabel("xxxxxx             ";
        L2=new JLabel("xxxx";
        jg=new JLabel("";

        p1=new JPanel(new FlowLayout());
        p2=new JPanel(new FlowLayout());
        p3=new JPanel(new FlowLayout());

        p1.setBackground(Color.RED);
        p1.setBounds(0, 0, 400, 50);
        but1=new JButton("开始";
        
        p2.setBackground(Color.blue);
        p2.setLayout(gl);
        p2.setBounds(0,50,400,400);
        b1=new JButton("";
        b2=new JButton("";
        b3=new JButton("";
        b4=new JButton("");
        b5=new JButton("");
        b6=new JButton("");
        b7=new JButton("");
        b8=new JButton("");
        b9=new JButton("");
        
        
        p3.setBackground(Color.green);
        p3.setBounds(0, 450, 400, 50);
        t1=new JTextField(10);
        but2=new JButton("保存");
        but3=new JButton("加载");

        p2.add(b1);
        p2.add(b2);
        p2.add(b3);
        p2.add(b4);
        p2.add(b5);
        p2.add(b6);
        p2.add(b7);
        p2.add(b;
        p2.add(b9);
        p1.add(but1);
        p3.add(but2);
        p3.add(but3);
        p3.add(t1);
        
        b1.setActionCommand("a1");
        b2.setActionCommand("a2");
        b3.setActionCommand("a3");
        b4.setActionCommand("a4");
        b5.setActionCommand("a5");
        b6.setActionCommand("a6");
        b7.setActionCommand("a7");
        b8.setActionCommand("a8");
        b9.setActionCommand("a9");
        but1.setActionCommand("but1");
        but2.setActionCommand("but2");
        but3.setActionCommand("but3");
        
        
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
        b5.addActionListener(this);
        b6.addActionListener(this);
        b7.addActionListener(this);
        b8.addActionListener(this);
        b9.addActionListener(this);
        but1.addActionListener(this);
        but2.addActionListener(this);
        but3.addActionListener(this);

        c.add(p1);
        c.add(p2);
        c.add(p3);
        p1.add(L1);
        p1.add(L2);
        p3.add(jg);
       // p1.setBorder(BorderFactory.createTitledBorder(""));
        
    }
    public static void main(String args[]){
        E1301 ex=new E1301();
        ex.setSize(400,520);
        ex.setVisible(true);
        
    }
    public void actionPerformed(ActionEvent e){
   
            if(e.getActionCommand().equals("but1")){
                    ks=1;
                    but1.setText("游戏中");
                    try {
                                server=new ServerSocket(4700);
                                System.out.println("123");
                        } catch (IOException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                        }
                   
            }
            if(e.getActionCommand().equals("but2")){
                    if(ks==0)
                    JOptionPane.showMessageDialog(null, "请先点击开始按钮!");
                   
            }
            if(e.getActionCommand().equals("but3")){
                    if(ks==0)
                    JOptionPane.showMessageDialog(null, "请先点击开始按钮!");
            }

      if(ks==1){
           
            if(e.getActionCommand().equals("a1")){
                    if(pos1==0){
                    if (i%2==0){
                            b1.setIcon(iconObj);
                            i++;
                            pos1=1;
                    }
                    else{
                            b1.setIcon(iconObj2);
                            i++;
                            pos1=2;
                    }
            }
                   
      }
            else if(e.getActionCommand().equals("a2")){
                    if(pos2==0){
                    if(i%2==0){
                            b2.setIcon(iconObj);
                            i++;
                            pos2=1;
                    }
                    else{
                            b2.setIcon(iconObj2);
                            i++;
                            pos2=2;
                    }
        }
            }
            else if(e.getActionCommand().equals("a3")){
                    if(pos3==0){
                            if(i%2==0){
                                    b3.setIcon(iconObj);
                                    i++;
                                    pos3=1;
                            }
                            else{
                                    b3.setIcon(iconObj2);
                                    i++;
                                    pos3=2;
                            }
                    }
            }
            else if(e.getActionCommand().equals("a4")){
                    if(pos4==0){
                    if(i%2==0){
                            b4.setIcon(iconObj);
                            i++;
                            pos4=1;
                    }
                    else{
                            b4.setIcon(iconObj2);
                            i++;
                            pos4=2;
                    }
        }
            }
            else if(e.getActionCommand().equals("a5")){
                    if(pos5==0){
                    if(i%2==0){
                            b5.setIcon(iconObj);
                            i++;
                            pos5=1;
                    }
                    else{
                            b5.setIcon(iconObj2);
                            i++;
                            pos5=2;
                    }
        }
            }
            else if(e.getActionCommand().equals("a6")){
                    if(pos6==0){
                    if(i%2==0){
                            b6.setIcon(iconObj);
                            i++;
                            pos6=1;
                    }
                    else{
                            b6.setIcon(iconObj2);
                            i++;
                            pos6=2;
                    }
        }
            }
            else if(e.getActionCommand().equals("a7")){
                    if(pos7==0){
                    if(i%2==0){
                            b7.setIcon(iconObj);
                            i++;
                            pos7=1;
                    }
                    else{
                            b7.setIcon(iconObj2);
                            i++;
                            pos7=2;
                    }
        }
            }
            else if(e.getActionCommand().equals("a8")){
                    if(pos8==0){
                    if(i%2==0){
                            b8.setIcon(iconObj);
                            i++;
                            pos8=1;
                    }
                    else{
                            b8.setIcon(iconObj2);
                            i++;
                            pos8=2;
                    }
        }
            }
            else if(e.getActionCommand().equals("a9")){
                    if(pos9==0){
                    if(i%2==0){
                            b9.setIcon(iconObj);
                            i++;
                            pos9=1;
                    }
                    else{
                            b9.setIcon(iconObj2);
                            i++;
                            pos9=2;
                    }
        }
            }
                       if((pos1==1&&pos2==1&&pos3==1)
                            ||(pos4==1&&pos5==1&&pos6==1)
                            ||(pos7==1&&pos8==1&&pos9==1)
                            ||(pos1==1&&pos4==1&&pos7==1)
                            ||(pos2==1&&pos5==1&&pos8==1)
                            ||(pos3==1&&pos6==1&&pos9==1)
                            ||(pos1==1&&pos5==1&&pos9==1)
                            ||(pos3==1&&pos5==1&&pos7==1)){
                    jg.setText("红方胜!");
                    ks=0;
                    but1.setText("重新开始");

            }
            else if((pos1==2&&pos2==2&&pos3==2)
                            ||(pos4==2&&pos5==2&&pos6==2)
                            ||(pos7==2&&pos8==2&&pos9==2)
                            ||(pos1==2&&pos4==2&&pos7==2)
                            ||(pos2==2&&pos5==2&&pos8==2)
                            ||(pos3==2&&pos6==2&&pos9==2)
                            ||(pos1==2&&pos5==2&&pos9==2)
                            ||(pos3==2&&pos5==2&&pos7==2)){
                    jg.setText("黑方胜!");
                    ks=0;
                    but1.setText("重新开始");
            }
           
            else if(pos1!=0&&pos2!=0&&pos3!=0
                            &&pos4!=0&&pos5!=0&&pos6!=0
                            &&pos7!=0&&pos8!=0&&pos9!=0){
                    jg.setText("无获胜方!平局!");
                    ks=0;
                    but1.setText("重新开始");
            }
                       if(e.getActionCommand().equals("but2")){
                              
                                xm=t1.getText();
                               if(xm.length()==0){
                                        JOptionPane.showMessageDialog(null, "请输入姓名!");
                               }
                              if(xm.length()!=0){
                        try{
                                Class.forName(driver);
                            }
                            catch(Exception c){
                                System.out.println("无法加载驱动程序:");
                            }
                            try{
                                Connection con=DriverManager.getConnection(url,user,password);
                                if(!con.isClosed())
                                    System.out.println("数据库连接成功!");
                               
                                PreparedStatement psmt=con.prepareStatement("UPDATE bb set pos1=? ,pos2=? ,pos3=? ,pos4=? ,pos5=? ,pos6=? ,pos7=? ,pos8=? ,pos9=? ,i=? ,ks=? ,xm=?");
                               //UPDATE bb set pos1=? ,pos2=? ,pos3=? ,pos4=? ,pos5=? ,pos6=? ,pos7=? ,pos8=? ,pos9=? ,i=? ,ks=? ,xm=? where xm=xm
                               
                                psmt.setInt(1,pos1);
                                psmt.setInt(2,pos2);
                                psmt.setInt(3,pos3);
                                psmt.setInt(4,pos4);
                                psmt.setInt(5,pos5);
                                psmt.setInt(6,pos6);
                                psmt.setInt(7,pos7);
                                psmt.setInt(8,pos;
                                psmt.setInt(9,pos9);
                                psmt.setInt(10,i);
                                psmt.setInt(11, ks);
                                psmt.setString(12, xm);
                                /*System.out.println(pos1);
                                System.out.println(pos2);
                                System.out.println(pos3);
                                System.out.println(pos4);
                                System.out.println(pos5);
                                System.out.println(pos6);
                                System.out.println(pos7);
                                System.out.println(pos;
                                System.out.println(pos9);*/
                                int n;
                           
                                n=psmt.executeUpdate();
                                if(n>0)
                                System.out.println("更新记录完毕!");
                                JOptionPane.showMessageDialog(null, "保存成功!");
                                psmt.close();
                                con.close();
                            }
                            catch(SQLException ee){
                                System.out.println("数据库连接失败!");
                            }
                         }
                       }//存储过程;
            if(ks==0){
                if(e.getActionCommand().equals("but1")){
                       pos1=0;
                       pos2=0;
                       pos3=0;
                       pos4=0;
                       pos5=0;
                       pos6=0;
                       pos7=0;
                       pos8=0;
                       pos9=0;

                        i=0;
                        
                        ks=0;
                      but1.setText("开始");
                      b1.setIcon(null);
                      b2.setIcon(null);
                      b3.setIcon(null);
                      b4.setIcon(null);
                      b5.setIcon(null);
                      b6.setIcon(null);
                      b7.setIcon(null);
                      b8.setIcon(null);
                      b9.setIcon(null);
                      jg.setText("");
                      t1.setText(null);
                }
        }
            if(e.getActionCommand().equals("but3")){
                    Statement st;
                    ResultSet rs;
                    String strSQL;
                    xm=t1.getText();
                    if(xm.length()==0){
                            JOptionPane.showMessageDialog(null, "请输入姓名!");
                   }
                 
                    if(xm.length()!=0){
                    
                  try{
                    Class.forName(driver);
                }
                catch(Exception c){
                    System.out.println("无法加载驱动程序:");
                }
                try{
                    Connection con=DriverManager.getConnection(url,user,password);
                    if(!con.isClosed())
                        System.out.println("数据库连接成功!");
                        st=con.createStatement();
                        strSQL="select * from bb ";
                        rs=st.executeQuery(strSQL);
                      
                       if(rs.next()){
                               //pos1=1;
                              // pos2=2;
                               pos1=rs.getInt("pos1");//加""代表字符串,pos1本身现在没有值,需得到数据库中对应列表的值;
                               pos2=rs.getInt("pos2");//将数据库表中类型改为整形,不是长整形(不过没变化);
                               pos3=rs.getInt("pos3");
                               pos4=rs.getInt("pos4");
                               pos5=rs.getInt("pos5");
                               pos6=rs.getInt("pos6");
                               pos7=rs.getInt("pos7");
                               pos8=rs.getInt("pos8");
                               pos9=rs.getInt("pos9");
                               i=rs.getInt("i");
                               ks=rs.getInt("ks");
                               b1.setIcon(iconObj2);
                               t1.setText(rs.getString("xm"));
                       }
                        con.close();
                }
                catch(SQLException ee){
                    System.out.println("数据库连接失败!");
                }
                if (pos1==0){
                        b1.setIcon(null);
                }else if(pos1==1){
                        b1.setIcon(iconObj);
                }else if(pos1==2){
                        b1.setIcon(iconObj2);
                }
                
                if (pos2==0){
                        b2.setIcon(null);
                }else if(pos2==1){
                        b2.setIcon(iconObj);
                }else if(pos2==2){
                        b2.setIcon(iconObj2);
                }
                
                if (pos3==0){
                        b3.setIcon(null);
                }else if(pos3==1){
                        b3.setIcon(iconObj);
                }else if(pos3==2){
                        b3.setIcon(iconObj2);
                }
                
                if (pos4==0){
                        b4.setIcon(null);
                }else if(pos4==1){
                        b4.setIcon(iconObj);
                }else if(pos4==2){
                        b4.setIcon(iconObj2);
                }
                
                if (pos5==0){
                        b5.setIcon(null);
                }else if(pos5==1){
                        b5.setIcon(iconObj);
                }else if(pos5==2){
                        b5.setIcon(iconObj2);
                }
                
                if (pos6==0){
                        b6.setIcon(null);
                }else if(pos6==1){
                        b6.setIcon(iconObj);
                }else if(pos6==2){
                        b6.setIcon(iconObj2);
                }
                
                if (pos7==0){
                        b7.setIcon(null);
                }else if(pos7==1){
                        b7.setIcon(iconObj);
                }else if(pos7==2){
                        b7.setIcon(iconObj2);
                }
                
                if (pos8==0){
                        b8.setIcon(null);
                }else if(pos8==1){
                        b8.setIcon(iconObj);
                }else if(pos8==2){
                        b8.setIcon(iconObj2);
                }
                
                if (pos9==0){
                        b9.setIcon(null);
                }else if(pos9==1){
                        b9.setIcon(iconObj);
                }else if(pos9==2){
                        b9.setIcon(iconObj2);
                }
                
                
                /*if(e.getSource()==b0){
                                m=1;
                                b0.setIcon(g9);
                        }*/
                
                if((pos1==1&&pos2==1&&pos3==1)
                                    ||(pos4==1&&pos5==1&&pos6==1)
                                    ||(pos7==1&&pos8==1&&pos9==1)
                                    ||(pos1==1&&pos4==1&&pos7==1)
                                    ||(pos2==1&&pos5==1&&pos8==1)
                                    ||(pos3==1&&pos6==1&&pos9==1)
                                    ||(pos1==1&&pos5==1&&pos9==1)
                                    ||(pos3==1&&pos5==1&&pos7==1)){
                            jg.setText("红方胜!");
                            ks=0;
                            but1.setText("重新开始");

                    }
                    else if((pos1==2&&pos2==2&&pos3==2)
                                    ||(pos4==2&&pos5==2&&pos6==2)
                                    ||(pos7==2&&pos8==2&&pos9==2)
                                    ||(pos1==2&&pos4==2&&pos7==2)
                                    ||(pos2==2&&pos5==2&&pos8==2)
                                    ||(pos3==2&&pos6==2&&pos9==2)
                                    ||(pos1==2&&pos5==2&&pos9==2)
                                    ||(pos3==2&&pos5==2&&pos7==2)){
                            jg.setText("黑方胜!");
                            ks=0;
                            but1.setText("重新开始");
                    }
                   
                    else if(pos1!=0&&pos2!=0&&pos3!=0
                                    &&pos4!=0&&pos5!=0&&pos6!=0
                                    &&pos7!=0&&pos8!=0&&pos9!=0){
                            jg.setText("无获胜方!平局!");
                            ks=0;
                            but1.setText("重新开始");
                    }
                    }
         }//加载过程;
            }
            
            }
   
        }
      
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP