2017年8月9日 星期三

(Java) 井字遊戲_連線

Server :
  1. package tic.tac.toe;  
  2.   
  3. import java.awt.event.ActionEvent;  
  4. import java.awt.event.ActionListener;  
  5. import java.io.*;  
  6. import java.net.*;  
  7. import java.util.logging.Level;  
  8. import java.util.logging.Logger;  
  9. import javax.swing.*;  
  10. public class TicTacToe_Server extends javax.swing.JFrame {  
  11. ServerSocket ss  ;  
  12. Socket s ;  
  13. BufferedReader in ;  
  14. PrintWriter out ;  
  15. boolean stop = true ; //用來安排是否可按按鈕  
  16. int i = 0 ;//時間  
  17. Timer t = new Timer(800,new ActionListener(){  
  18.     @Override  
  19.     public void actionPerformed(ActionEvent e) {  
  20.         jLabel2.setText(Integer.toString(i++));  
  21.     }  
  22.       
  23. });  
  24.     public TicTacToe_Server() {  
  25.         initComponents();  
  26.     }  
  27.     public void game()  {  
  28.        String  mess ;  
  29.        try{  
  30.      while((mess = in.readLine()) != null){ //互相傳達消息 來驅動工作  
  31.          //假設對方贏或輸 會傳送消息給對方 來進行同步  
  32.           if(mess.equals("You Lose")){  
  33.               Clear() ;  
  34.               JOptionPane.showMessageDialog(null"You Lose" );  
  35.               continue ;  
  36.          }  
  37.          if(mess.equals("You Win")){  
  38.              Clear() ;  
  39.               JOptionPane.showMessageDialog(null"You Win" );  
  40.               continue ;  
  41.          }  
  42.          //用來判斷按了什麼按鈕  
  43.          if(mess.equals("1")){  
  44.             jButton1.setText("X");  
  45.             jButton1.setEnabled(false);  
  46.          }else if(mess.equals("2")){  
  47.             jButton2.setText("X");  
  48.             jButton2.setEnabled(false);  
  49.          }else if(mess.equals("3")){  
  50.             jButton3.setText("X");  
  51.             jButton3.setEnabled(false);  
  52.          }else if(mess.equals("4")){  
  53.             jButton4.setText("X");  
  54.             jButton4.setEnabled(false);  
  55.          }else if(mess.equals("5")){  
  56.             jButton5.setText("X");  
  57.             jButton5.setEnabled(false);  
  58.          }else if(mess.equals("6")){  
  59.             jButton6.setText("X");  
  60.             jButton6.setEnabled(false);  
  61.          }else if(mess.equals("7")){  
  62.             jButton7.setText("X");  
  63.             jButton7.setEnabled(false);  
  64.          }else if(mess.equals("8")){  
  65.             jButton8.setText("X");  
  66.             jButton8.setEnabled(false);  
  67.          }else if(mess.equals("9")){  
  68.             jButton9.setText("X");  
  69.             jButton9.setEnabled(false);  
  70.          }  
  71.          stop = false ;    
  72.          jLabel3.setText("您先 ");//收到訊息已經代表你可以按按鈕  
  73.           win() ;  
  74.           lose();  
  75.           isFull();  
  76.      }  
  77.        }catch(IOException e){  
  78.            JOptionPane.showMessageDialog(null"對方已退出");  
  79.            try {  
  80.                s.close();  
  81.                System.exit(0);  
  82.            } catch (IOException ex) {  
  83.                JOptionPane.showMessageDialog(null"關閉錯誤....");  
  84.                System.exit(-1);  
  85.            }  
  86.        }  
  87.     }  
  88.      public void Clear(){//清空重來  
  89.          i = 0;//時間從0開始  
  90.          jButton1.setText("");jButton4.setText("");jButton7.setText("");  
  91.          jButton2.setText("");jButton5.setText("");jButton8.setText("");  
  92.          jButton3.setText("");jButton6.setText("");jButton9.setText("");  
  93.          jButton1.setEnabled(true); jButton2.setEnabled(true); jButton3.setEnabled(true);  
  94.          jButton4.setEnabled(true); jButton5.setEnabled(true); jButton6.setEnabled(true);  
  95.          jButton7.setEnabled(true); jButton8.setEnabled(true); jButton9.setEnabled(true);  
  96.          // 產生隨機數 來安排誰先攻  
  97.         int num = (int)(Math.random()*2+1) ;  
  98.         if(num == 1){   
  99.             stop = false ;    
  100.             jLabel3.setText("您先");  
  101.             out.println("stop");  
  102.         }else{  
  103.             stop = true ;   
  104.             jLabel3.setText("對方先");   
  105.              out.println();  
  106.         }   
  107.      }  
  108.     public void win(){//贏比賽條件 Buttin顯示值為O  
  109.         if((jButton1.getText().equals("O")  && jButton2.getText() == "O" && jButton3.getText() == "O")||  
  110.            (jButton4.getText().equals("O")  && jButton5.getText() == "O" && jButton6.getText() == "O")||   
  111.            (jButton7.getText().equals("O")  && jButton8.getText() == "O" && jButton9.getText() == "O")||  
  112.            (jButton1.getText().equals("O")  && jButton4.getText() == "O" && jButton7.getText() == "O")||  
  113.            (jButton2.getText().equals("O")  && jButton5.getText() == "O" && jButton8.getText() == "O")||  
  114.            (jButton3.getText().equals("O")  && jButton6.getText() == "O" && jButton9.getText() == "O")  ){  
  115.         out.println("You Lose");  
  116.         Clear();  
  117.         JOptionPane.showMessageDialog(null"You Win" );  
  118.         }  
  119.     }  
  120.     public void isFull(){//檢查是否滿  
  121.         if(!jButton1.getText().isEmpty() && !jButton2.getText().isEmpty()  && !jButton3.getText().isEmpty()  
  122.            && !jButton4.getText().isEmpty() && !jButton5.getText().isEmpty()  && !jButton6.getText().isEmpty()  
  123.            && !jButton7.getText().isEmpty()  && !jButton8.getText().isEmpty()  && !jButton9.getText().isEmpty() ){  
  124.              Clear() ; out.println("You ");  
  125.              JOptionPane.showMessageDialog(null"平手" );  
  126.         }  
  127.     }  
  128.     public void lose(){//輸比賽條件 Buttin顯示值為X  
  129.        if((jButton1.getText().equals("X")  && jButton2.getText() == "X" && jButton3.getText() == "X")||  
  130.            (jButton4.getText().equals("X")  && jButton5.getText() == "X" && jButton6.getText() == "X")||   
  131.            (jButton7.getText().equals("X")  && jButton8.getText() == "X" && jButton9.getText() == "X")||  
  132.            (jButton1.getText().equals("X")  && jButton4.getText() == "X" && jButton7.getText() == "X")||  
  133.            (jButton2.getText().equals("X")  && jButton5.getText() == "X" && jButton8.getText() == "X")||  
  134.            (jButton3.getText().equals("X")  && jButton6.getText() == "X" && jButton9.getText() == "X")  ){  
  135.         out.println("You Win");  
  136.         Clear() ;  
  137.         JOptionPane.showMessageDialog(null"You Lose" );  
  138.        }  
  139.     }  
  140.     public void Start(){  
  141.       new Thread(new Runnable(){  
  142.         @Override  
  143.         public void run() {  
  144.         try {  
  145.         ss = new ServerSocket(20097) ;  
  146.         JOptionPane.showMessageDialog(null"建立成功....");  
  147.         s = ss.accept();   
  148.         in = new BufferedReader(new InputStreamReader(s.getInputStream()));  
  149.         out = new PrintWriter(s.getOutputStream() , true);  
  150.         JOptionPane.showMessageDialog(null"連線成功....");  
  151.         jButton10.setEnabled(false);//設定不可頂選  
  152.           // 產生隨機數 來安排誰先攻  
  153.         int num = (int)(Math.random()*2+1) ;  
  154.         if(num == 1){   
  155.             stop = false ;    
  156.             jLabel3.setText("您先");  
  157.             out.println("stop");  
  158.         }else{  
  159.             stop = true ;   
  160.             jLabel3.setText("對方先");   
  161.              out.println();  
  162.         }   
  163.         t.start();  
  164.         game();  
  165.         } catch (IOException ex) {  
  166.           JOptionPane.showMessageDialog(null"建立失敗....");  
  167.           jButton10.setEnabled(true);//設定不可頂選  
  168.          }  
  169.         }  
  170.           
  171.     }).start();      
  172.      
  173.     }  
  174.     @SuppressWarnings("unchecked")  
  175.     // <editor-fold defaultstate="collapsed" desc="Generated Code">     
  176. //建構式窗的區塊                       
  177.     private void initComponents() {  
  178.   
  179.         jPanel1 = new javax.swing.JPanel();  
  180.         jButton1 = new javax.swing.JButton();  
  181.         jButton2 = new javax.swing.JButton();  
  182.         jButton3 = new javax.swing.JButton();  
  183.         jButton4 = new javax.swing.JButton();  
  184.         jButton5 = new javax.swing.JButton();  
  185.         jButton6 = new javax.swing.JButton();  
  186.         jButton7 = new javax.swing.JButton();  
  187.         jButton8 = new javax.swing.JButton();  
  188.         jButton9 = new javax.swing.JButton();  
  189.         jButton10 = new javax.swing.JButton();  
  190.         jButton11 = new javax.swing.JButton();  
  191.         jLabel1 = new javax.swing.JLabel();  
  192.         jLabel2 = new javax.swing.JLabel();  
  193.         jLabel3 = new javax.swing.JLabel();  
  194.         jLabel4 = new javax.swing.JLabel();  
  195.         jLabel5 = new javax.swing.JLabel();  
  196.         jLabel6 = new javax.swing.JLabel();  
  197.         jLabel7 = new javax.swing.JLabel();  
  198.   
  199.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);  
  200.         setTitle("井字遊戲");  
  201.         setResizable(false);  
  202.   
  203.         jPanel1.setPreferredSize(new java.awt.Dimension(430430));  
  204.         jPanel1.setLayout(new java.awt.GridLayout(33));  
  205.   
  206.         jButton1.setFont(new java.awt.Font("Arial"148)); // NOI18N  
  207.         jButton1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 3));  
  208.         jButton1.addActionListener(new java.awt.event.ActionListener() {  
  209.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  210.                 jButton1ActionPerformed(evt);  
  211.             }  
  212.         });  
  213.         jPanel1.add(jButton1);  
  214.   
  215.         jButton2.setFont(new java.awt.Font("Arial"148)); // NOI18N  
  216.         jButton2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 3));  
  217.         jButton2.addActionListener(new java.awt.event.ActionListener() {  
  218.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  219.                 jButton2ActionPerformed(evt);  
  220.             }  
  221.         });  
  222.         jPanel1.add(jButton2);  
  223.   
  224.         jButton3.setFont(new java.awt.Font("Arial"148)); // NOI18N  
  225.         jButton3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 3));  
  226.         jButton3.addActionListener(new java.awt.event.ActionListener() {  
  227.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  228.                 jButton3ActionPerformed(evt);  
  229.             }  
  230.         });  
  231.         jPanel1.add(jButton3);  
  232.   
  233.         jButton4.setFont(new java.awt.Font("Arial"148)); // NOI18N  
  234.         jButton4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 3));  
  235.         jButton4.addActionListener(new java.awt.event.ActionListener() {  
  236.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  237.                 jButton4ActionPerformed(evt);  
  238.             }  
  239.         });  
  240.         jPanel1.add(jButton4);  
  241.   
  242.         jButton5.setFont(new java.awt.Font("Arial"148)); // NOI18N  
  243.         jButton5.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 3));  
  244.         jButton5.addActionListener(new java.awt.event.ActionListener() {  
  245.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  246.                 jButton5ActionPerformed(evt);  
  247.             }  
  248.         });  
  249.         jPanel1.add(jButton5);  
  250.   
  251.         jButton6.setFont(new java.awt.Font("Arial"148)); // NOI18N  
  252.         jButton6.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 3));  
  253.         jButton6.addActionListener(new java.awt.event.ActionListener() {  
  254.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  255.                 jButton6ActionPerformed(evt);  
  256.             }  
  257.         });  
  258.         jPanel1.add(jButton6);  
  259.   
  260.         jButton7.setFont(new java.awt.Font("Arial"148)); // NOI18N  
  261.         jButton7.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 3));  
  262.         jButton7.addActionListener(new java.awt.event.ActionListener() {  
  263.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  264.                 jButton7ActionPerformed(evt);  
  265.             }  
  266.         });  
  267.         jPanel1.add(jButton7);  
  268.   
  269.         jButton8.setFont(new java.awt.Font("Arial"148)); // NOI18N  
  270.         jButton8.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 3));  
  271.         jButton8.addActionListener(new java.awt.event.ActionListener() {  
  272.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  273.                 jButton8ActionPerformed(evt);  
  274.             }  
  275.         });  
  276.         jPanel1.add(jButton8);  
  277.   
  278.         jButton9.setFont(new java.awt.Font("Arial"148)); // NOI18N  
  279.         jButton9.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 3));  
  280.         jButton9.addActionListener(new java.awt.event.ActionListener() {  
  281.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  282.                 jButton9ActionPerformed(evt);  
  283.             }  
  284.         });  
  285.         jPanel1.add(jButton9);  
  286.   
  287.         jButton10.setText("連線");  
  288.         jButton10.addActionListener(new java.awt.event.ActionListener() {  
  289.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  290.                 jButton10ActionPerformed(evt);  
  291.             }  
  292.         });  
  293.   
  294.         jButton11.setText("離開");  
  295.         jButton11.addActionListener(new java.awt.event.ActionListener() {  
  296.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  297.                 jButton11ActionPerformed(evt);  
  298.             }  
  299.         });  
  300.   
  301.         jLabel1.setFont(new java.awt.Font("微軟正黑體"118)); // NOI18N  
  302.         jLabel1.setText("Time : ");  
  303.   
  304.         jLabel2.setFont(new java.awt.Font("微軟正黑體"118)); // NOI18N  
  305.         jLabel2.setText("00");  
  306.   
  307.         jLabel3.setText("XXXX");  
  308.   
  309.         jLabel4.setFont(new java.awt.Font("微軟正黑體"124)); // NOI18N  
  310.         jLabel4.setText("井");  
  311.         jLabel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 2));  
  312.   
  313.         jLabel5.setFont(new java.awt.Font("微軟正黑體"124)); // NOI18N  
  314.         jLabel5.setText("字");  
  315.         jLabel5.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 2));  
  316.   
  317.         jLabel6.setFont(new java.awt.Font("微軟正黑體"124)); // NOI18N  
  318.         jLabel6.setText("遊");  
  319.         jLabel6.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 2));  
  320.   
  321.         jLabel7.setFont(new java.awt.Font("微軟正黑體"124)); // NOI18N  
  322.         jLabel7.setText("戲");  
  323.         jLabel7.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 2));  
  324.   
  325.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());  
  326.         getContentPane().setLayout(layout);  
  327.         layout.setHorizontalGroup(  
  328.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
  329.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()  
  330.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
  331.                     .addGroup(layout.createSequentialGroup()  
  332.                         .addGap(313131)  
  333.                         .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)  
  334.                         .addGap(353535)  
  335.                         .addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)  
  336.                         .addGap(737373)  
  337.                         .addComponent(jLabel1)  
  338.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)  
  339.                         .addComponent(jLabel2))  
  340.                     .addGroup(layout.createSequentialGroup()  
  341.                         .addContainerGap()  
  342.                         .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))  
  343.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)  
  344.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
  345.                     .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)  
  346.                     .addGroup(layout.createSequentialGroup()  
  347.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, javax.swing.GroupLayout.PREFERRED_SIZE)  
  348.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
  349.                             .addComponent(jLabel4)  
  350.                             .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)))  
  351.                     .addComponent(jLabel5)  
  352.                     .addComponent(jLabel7))  
  353.                 .addGap(181818))  
  354.         );  
  355.         layout.setVerticalGroup(  
  356.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
  357.             .addGroup(layout.createSequentialGroup()  
  358.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
  359.                     .addGroup(layout.createSequentialGroup()  
  360.                         .addContainerGap()  
  361.                         .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))  
  362.                     .addGroup(layout.createSequentialGroup()  
  363.                         .addGap(444444)  
  364.                         .addComponent(jLabel4)  
  365.                         .addGap(373737)  
  366.                         .addComponent(jLabel5)  
  367.                         .addGap(383838)  
  368.                         .addComponent(jLabel6)  
  369.                         .addGap(414141)  
  370.                         .addComponent(jLabel7)))  
  371.                 .addGap(181818)  
  372.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)  
  373.                     .addComponent(jButton10)  
  374.                     .addComponent(jButton11)  
  375.                     .addComponent(jLabel1)  
  376.                     .addComponent(jLabel2)  
  377.                     .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))  
  378.                 .addContainerGap(13, Short.MAX_VALUE))  
  379.         );  
  380.   
  381.         pack();  
  382.     }// </editor-fold>                          
  383.   
  384.     private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {                                            
  385.            Start() ;         
  386.     }                                           
  387.   
  388.     private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {                                            
  389.        System.exit(0);  
  390.     }                                           
  391.   
  392.     private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  393.         if(stop != true){  
  394.        jButton5.setText("O");  
  395.        out.println("5");  
  396.        jButton5.setEnabled(false);  
  397.        stop = true ;  
  398.        jLabel3.setText("對方先");    
  399.        }  
  400.     }                                          
  401.    
  402.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  403.        if(stop != true){  
  404.        jButton1.setText("O");  
  405.        out.println("1");  
  406.        jButton1.setEnabled(false);  
  407.        stop = true ;  
  408.        jLabel3.setText("對方先");    
  409.        }  
  410.     }                                          
  411.   
  412.     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  413.         if(stop != true){  
  414.        jButton2.setText("O");  
  415.        out.println("2");  
  416.        jButton2.setEnabled(false);  
  417.        stop = true ;  
  418.          jLabel3.setText("對方先");    
  419.        }  
  420.     }                                          
  421.   
  422.     private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  423.          if(stop != true){  
  424.        jButton3.setText("O");  
  425.        out.println("3");  
  426.        jButton3.setEnabled(false);  
  427.        stop = true ;  
  428.          jLabel3.setText("對方先");    
  429.        }  
  430.     }                                          
  431.   
  432.     private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  433.        if(stop != true){  
  434.        jButton7.setText("O");  
  435.        out.println("7");  
  436.        jButton7.setEnabled(false);  
  437.        stop = true ;  
  438.          jLabel3.setText("對方先");    
  439.        }  
  440.     }                                          
  441.   
  442.     private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  443.          if(stop != true){  
  444.        jButton4.setText("O");  
  445.        out.println("4");  
  446.        jButton4.setEnabled(false);  
  447.        stop = true ;  
  448.          jLabel3.setText("對方先");    
  449.        }  
  450.     }                                          
  451.   
  452.     private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  453.         if(stop != true){  
  454.        jButton6.setText("O");  
  455.        out.println("6");  
  456.        jButton6.setEnabled(false);  
  457.        stop = true ;  
  458.          jLabel3.setText("對方先");    
  459.        }  
  460.     }                                          
  461.   
  462.     private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  463.        if(stop != true){  
  464.        jButton8.setText("O");  
  465.        out.println("8");  
  466.        jButton8.setEnabled(false);  
  467.        stop = true ;  
  468.          jLabel3.setText("對方先");    
  469.        }  
  470.     }                                          
  471.   
  472.     private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  473.          if(stop != true){  
  474.        jButton9.setText("O");  
  475.        out.println("9");  
  476.        jButton9.setEnabled(false);  
  477.        stop = true ;  
  478.          jLabel3.setText("對方先");    
  479.        }  
  480.     }                                          
  481.   
  482.     public static void main(String args[]) {  
  483.         /* Set the Nimbus look and feel */  
  484.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">  
  485.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
  486.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html  
  487.          */  
  488.         try {  
  489.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {  
  490.                 if ("Nimbus".equals(info.getName())) {  
  491.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());  
  492.                     break;  
  493.                 }  
  494.             }  
  495.         } catch (ClassNotFoundException ex) {  
  496.             java.util.logging.Logger.getLogger(TicTacToe_Server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
  497.         } catch (InstantiationException ex) {  
  498.             java.util.logging.Logger.getLogger(TicTacToe_Server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
  499.         } catch (IllegalAccessException ex) {  
  500.             java.util.logging.Logger.getLogger(TicTacToe_Server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
  501.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {  
  502.             java.util.logging.Logger.getLogger(TicTacToe_Server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
  503.         }  
  504.         //</editor-fold>  
  505.   
  506.         /* Create and display the form */  
  507.         java.awt.EventQueue.invokeLater(new Runnable() {  
  508.             public void run() {  
  509.                 new TicTacToe_Server().setVisible(true);  
  510.             }  
  511.         });  
  512.     }  
  513.   
  514.     // Variables declaration - do not modify                       
  515.     private javax.swing.JButton jButton1;  
  516.     private javax.swing.JButton jButton10;  
  517.     private javax.swing.JButton jButton11;  
  518.     private javax.swing.JButton jButton2;  
  519.     private javax.swing.JButton jButton3;  
  520.     private javax.swing.JButton jButton4;  
  521.     private javax.swing.JButton jButton5;  
  522.     private javax.swing.JButton jButton6;  
  523.     private javax.swing.JButton jButton7;  
  524.     private javax.swing.JButton jButton8;  
  525.     private javax.swing.JButton jButton9;  
  526.     private javax.swing.JLabel jLabel1;  
  527.     private javax.swing.JLabel jLabel2;  
  528.     private javax.swing.JLabel jLabel3;  
  529.     private javax.swing.JLabel jLabel4;  
  530.     private javax.swing.JLabel jLabel5;  
  531.     private javax.swing.JLabel jLabel6;  
  532.     private javax.swing.JLabel jLabel7;  
  533.     private javax.swing.JPanel jPanel1;  
  534.     // End of variables declaration                     
  535. }  
clinet : 
  1. package tic.tac;  
  2.   
  3. import java.awt.event.ActionEvent;  
  4. import java.awt.event.ActionListener;  
  5. import java.io.BufferedReader;  
  6. import java.io.IOException;  
  7. import java.io.InputStreamReader;  
  8. import java.io.PrintWriter;  
  9. import java.net.ServerSocket;  
  10. import java.net.Socket;  
  11. import javax.swing.JOptionPane;  
  12. import javax.swing.Timer;  
  13.   
  14.   
  15. public class toe_Clinet extends javax.swing.JFrame {  
  16. Socket s ;  
  17. BufferedReader in ;  
  18. PrintWriter out ;  
  19. boolean stop = true;//用來安排是否可按按鈕  
  20. int i = 0 ;//時間  
  21. Timer t = new Timer(800,new ActionListener(){  
  22.     @Override  
  23.     public void actionPerformed(ActionEvent e) {  
  24.         jLabel2.setText(Integer.toString(i++));  
  25.     }  
  26.       
  27. });  
  28.     public toe_Clinet() {  
  29.         initComponents();  
  30.     }  
  31.     @SuppressWarnings("unchecked")  
  32.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                            
  33.     private void initComponents() {  //建構式窗區塊
  34.   
  35.         jPanel1 = new javax.swing.JPanel();  
  36.         jButton1 = new javax.swing.JButton();  
  37.         jButton2 = new javax.swing.JButton();  
  38.         jButton3 = new javax.swing.JButton();  
  39.         jButton4 = new javax.swing.JButton();  
  40.         jButton5 = new javax.swing.JButton();  
  41.         jButton6 = new javax.swing.JButton();  
  42.         jButton7 = new javax.swing.JButton();  
  43.         jButton8 = new javax.swing.JButton();  
  44.         jButton9 = new javax.swing.JButton();  
  45.         jButton10 = new javax.swing.JButton();  
  46.         jButton11 = new javax.swing.JButton();  
  47.         jLabel1 = new javax.swing.JLabel();  
  48.         jLabel2 = new javax.swing.JLabel();  
  49.         jLabel3 = new javax.swing.JLabel();  
  50.         jLabel4 = new javax.swing.JLabel();  
  51.         jLabel5 = new javax.swing.JLabel();  
  52.         jLabel6 = new javax.swing.JLabel();  
  53.         jLabel7 = new javax.swing.JLabel();  
  54.   
  55.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);  
  56.         setTitle("井字遊戲");  
  57.         setResizable(false);  
  58.   
  59.         jPanel1.setPreferredSize(new java.awt.Dimension(430430));  
  60.         jPanel1.setLayout(new java.awt.GridLayout(33));  
  61.   
  62.         jButton1.setFont(new java.awt.Font("Arial"148)); // NOI18N  
  63.         jButton1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 3));  
  64.         jButton1.addActionListener(new java.awt.event.ActionListener() {  
  65.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  66.                 jButton1ActionPerformed(evt);  
  67.             }  
  68.         });  
  69.         jPanel1.add(jButton1);  
  70.   
  71.         jButton2.setFont(new java.awt.Font("Arial"148)); // NOI18N  
  72.         jButton2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 3));  
  73.         jButton2.addActionListener(new java.awt.event.ActionListener() {  
  74.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  75.                 jButton2ActionPerformed(evt);  
  76.             }  
  77.         });  
  78.         jPanel1.add(jButton2);  
  79.   
  80.         jButton3.setFont(new java.awt.Font("Arial"148)); // NOI18N  
  81.         jButton3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 3));  
  82.         jButton3.addActionListener(new java.awt.event.ActionListener() {  
  83.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  84.                 jButton3ActionPerformed(evt);  
  85.             }  
  86.         });  
  87.         jPanel1.add(jButton3);  
  88.   
  89.         jButton4.setFont(new java.awt.Font("Arial"148)); // NOI18N  
  90.         jButton4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 3));  
  91.         jButton4.addActionListener(new java.awt.event.ActionListener() {  
  92.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  93.                 jButton4ActionPerformed(evt);  
  94.             }  
  95.         });  
  96.         jPanel1.add(jButton4);  
  97.   
  98.         jButton5.setFont(new java.awt.Font("Arial"148)); // NOI18N  
  99.         jButton5.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 3));  
  100.         jButton5.addActionListener(new java.awt.event.ActionListener() {  
  101.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  102.                 jButton5ActionPerformed(evt);  
  103.             }  
  104.         });  
  105.         jPanel1.add(jButton5);  
  106.   
  107.         jButton6.setFont(new java.awt.Font("Arial"148)); // NOI18N  
  108.         jButton6.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 3));  
  109.         jButton6.addActionListener(new java.awt.event.ActionListener() {  
  110.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  111.                 jButton6ActionPerformed(evt);  
  112.             }  
  113.         });  
  114.         jPanel1.add(jButton6);  
  115.   
  116.         jButton7.setFont(new java.awt.Font("Arial"148)); // NOI18N  
  117.         jButton7.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 3));  
  118.         jButton7.addActionListener(new java.awt.event.ActionListener() {  
  119.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  120.                 jButton7ActionPerformed(evt);  
  121.             }  
  122.         });  
  123.         jPanel1.add(jButton7);  
  124.   
  125.         jButton8.setFont(new java.awt.Font("Arial"148)); // NOI18N  
  126.         jButton8.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 3));  
  127.         jButton8.addActionListener(new java.awt.event.ActionListener() {  
  128.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  129.                 jButton8ActionPerformed(evt);  
  130.             }  
  131.         });  
  132.         jPanel1.add(jButton8);  
  133.   
  134.         jButton9.setFont(new java.awt.Font("Arial"148)); // NOI18N  
  135.         jButton9.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 3));  
  136.         jButton9.addActionListener(new java.awt.event.ActionListener() {  
  137.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  138.                 jButton9ActionPerformed(evt);  
  139.             }  
  140.         });  
  141.         jPanel1.add(jButton9);  
  142.   
  143.         jButton10.setText("連線");  
  144.         jButton10.addActionListener(new java.awt.event.ActionListener() {  
  145.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  146.                 jButton10ActionPerformed(evt);  
  147.             }  
  148.         });  
  149.   
  150.         jButton11.setText("離開");  
  151.         jButton11.addActionListener(new java.awt.event.ActionListener() {  
  152.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  153.                 jButton11ActionPerformed(evt);  
  154.             }  
  155.         });  
  156.   
  157.         jLabel1.setFont(new java.awt.Font("微軟正黑體"118)); // NOI18N  
  158.         jLabel1.setText("Time : ");  
  159.   
  160.         jLabel2.setFont(new java.awt.Font("微軟正黑體"118)); // NOI18N  
  161.         jLabel2.setText("00");  
  162.   
  163.         jLabel3.setText("XXXX");  
  164.   
  165.         jLabel4.setFont(new java.awt.Font("微軟正黑體"124)); // NOI18N  
  166.         jLabel4.setText("井");  
  167.         jLabel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 2));  
  168.   
  169.         jLabel5.setFont(new java.awt.Font("微軟正黑體"124)); // NOI18N  
  170.         jLabel5.setText("字");  
  171.         jLabel5.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 2));  
  172.   
  173.         jLabel6.setFont(new java.awt.Font("微軟正黑體"124)); // NOI18N  
  174.         jLabel6.setText("遊");  
  175.         jLabel6.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 2));  
  176.   
  177.         jLabel7.setFont(new java.awt.Font("微軟正黑體"124)); // NOI18N  
  178.         jLabel7.setText("戲");  
  179.         jLabel7.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(000), 2));  
  180.   
  181.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());  
  182.         getContentPane().setLayout(layout);  
  183.         layout.setHorizontalGroup(  
  184.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
  185.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()  
  186.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
  187.                     .addGroup(layout.createSequentialGroup()  
  188.                         .addGap(313131)  
  189.                         .addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)  
  190.                         .addGap(353535)  
  191.                         .addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)  
  192.                         .addGap(737373)  
  193.                         .addComponent(jLabel1)  
  194.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)  
  195.                         .addComponent(jLabel2))  
  196.                     .addGroup(layout.createSequentialGroup()  
  197.                         .addContainerGap()  
  198.                         .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))  
  199.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)  
  200.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
  201.                     .addGroup(layout.createSequentialGroup()  
  202.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, javax.swing.GroupLayout.PREFERRED_SIZE)  
  203.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
  204.                             .addComponent(jLabel4)  
  205.                             .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)))  
  206.                     .addComponent(jLabel5)  
  207.                     .addComponent(jLabel7)  
  208.                     .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))  
  209.                 .addGap(181818))  
  210.         );  
  211.         layout.setVerticalGroup(  
  212.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
  213.             .addGroup(layout.createSequentialGroup()  
  214.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
  215.                     .addGroup(layout.createSequentialGroup()  
  216.                         .addContainerGap()  
  217.                         .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))  
  218.                     .addGroup(layout.createSequentialGroup()  
  219.                         .addGap(717171)  
  220.                         .addComponent(jLabel4)  
  221.                         .addGap(373737)  
  222.                         .addComponent(jLabel5)  
  223.                         .addGap(383838)  
  224.                         .addComponent(jLabel6)  
  225.                         .addGap(414141)  
  226.                         .addComponent(jLabel7)))  
  227.                 .addGap(181818)  
  228.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)  
  229.                     .addComponent(jButton10)  
  230.                     .addComponent(jButton11)  
  231.                     .addComponent(jLabel1)  
  232.                     .addComponent(jLabel2)  
  233.                     .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))  
  234.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))  
  235.         );  
  236.   
  237.         pack();  
  238.     }// </editor-fold>                          
  239.  public void game()  {  
  240.        String  mess ;  
  241.        try{  
  242.             
  243.      while((mess = in.readLine()) != null){  
  244.           //互相傳達消息 來驅動工作  
  245.          //假設對方贏或輸 會傳送消息給對方 來進行同步  
  246.          if(mess.equals("stop")){  
  247.              stop = true ;  
  248.              jLabel3.setText("對方先 ");  
  249.              continue ;  
  250.          }  
  251.          if(mess.equals("You Lose")){  
  252.               JOptionPane.showMessageDialog(null"You Lose" );  
  253.               Clear() ;  
  254.               continue ;  
  255.          }  
  256.          if(mess.equals("You Win")){  
  257.               JOptionPane.showMessageDialog(null"You Win" );  
  258.               Clear() ;  
  259.               continue ;  
  260.          }  
  261.           //用來判斷按了什麼按鈕  
  262.          if(mess.equals("1")){  
  263.             jButton1.setText("X");  
  264.             jButton1.setEnabled(false);  
  265.          }else if(mess.equals("2")){  
  266.             jButton2.setText("X");  
  267.             jButton2.setEnabled(false);  
  268.          }else if(mess.equals("3")){  
  269.             jButton3.setText("X");  
  270.             jButton3.setEnabled(false);  
  271.          }else if(mess.equals("4")){  
  272.             jButton4.setText("X");  
  273.             jButton4.setEnabled(false);  
  274.          }else if(mess.equals("5")){  
  275.             jButton5.setText("X");  
  276.             jButton5.setEnabled(false);  
  277.          }else if(mess.equals("6")){  
  278.             jButton6.setText("X");  
  279.             jButton6.setEnabled(false);  
  280.          }else if(mess.equals("7")){  
  281.             jButton7.setText("X");  
  282.             jButton7.setEnabled(false);  
  283.          }else if(mess.equals("8")){  
  284.             jButton8.setText("X");  
  285.             jButton8.setEnabled(false);  
  286.          }else if(mess.equals("9")){  
  287.             jButton9.setText("X");  
  288.             jButton9.setEnabled(false);  
  289.          }  
  290.          stop = false ;       
  291.          jLabel3.setText("您先 ");//收到訊息已經代表你可以按按鈕  
  292.           win() ;  
  293.           lose();  
  294.           isFull();  
  295.      }  
  296.        }catch(IOException e){  
  297.            JOptionPane.showMessageDialog(null"對方已退出");  
  298.            try {  
  299.                s.close();  
  300.                 System.exit(-1);  
  301.            } catch (IOException ex) {  
  302.                JOptionPane.showMessageDialog(null"關閉錯誤....");  
  303.                System.exit(-1);  
  304.            }  
  305.        }  
  306.     }  
  307.   public void win(){//贏比賽條件 Buttin顯示值為O  
  308.          if((jButton1.getText().equals("X")  && jButton2.getText() == "X" && jButton3.getText() == "X")||  
  309.            (jButton4.getText().equals("X")  && jButton5.getText() == "X" && jButton6.getText() == "X")||   
  310.            (jButton7.getText().equals("X")  && jButton8.getText() == "X" && jButton9.getText() == "X")||  
  311.            (jButton1.getText().equals("X")  && jButton4.getText() == "X" && jButton7.getText() == "X")||  
  312.            (jButton2.getText().equals("X")  && jButton5.getText() == "X" && jButton8.getText() == "X")||  
  313.            (jButton3.getText().equals("X")  && jButton6.getText() == "X" && jButton9.getText() == "X")  ){  
  314.         out.println("You Lose");  
  315.         Clear() ;  
  316.          JOptionPane.showMessageDialog(null"You Win" );  
  317.         }  
  318.     }  
  319.      public void Clear(){//清空重來  
  320.          i = 0;  
  321.          jButton1.setText("");jButton4.setText("");jButton7.setText("");  
  322.          jButton2.setText("");jButton5.setText("");jButton8.setText("");  
  323.          jButton3.setText("");jButton6.setText("");jButton9.setText("");  
  324.          jButton1.setEnabled(true); jButton2.setEnabled(true); jButton3.setEnabled(true);  
  325.          jButton4.setEnabled(true); jButton5.setEnabled(true); jButton6.setEnabled(true);  
  326.          jButton7.setEnabled(true); jButton8.setEnabled(true); jButton9.setEnabled(true);  
  327.      }  
  328.   public void lose(){//輸比賽條件 Buttin顯示值為X  
  329.         if((jButton1.getText().equals("O")  && jButton2.getText() == "O" && jButton3.getText() == "O")||  
  330.            (jButton4.getText().equals("O")  && jButton5.getText() == "O" && jButton6.getText() == "O")||   
  331.            (jButton7.getText().equals("O")  && jButton8.getText() == "O" && jButton9.getText() == "O")||  
  332.            (jButton1.getText().equals("O")  && jButton4.getText() == "O" && jButton7.getText() == "O")||  
  333.            (jButton2.getText().equals("O")  && jButton5.getText() == "O" && jButton8.getText() == "O")||  
  334.            (jButton3.getText().equals("O")  && jButton6.getText() == "O" && jButton9.getText() == "O")  ){  
  335.          out.println("You Win");  
  336.          Clear() ;  
  337.           JOptionPane.showMessageDialog(null"You Lose" );  
  338.        }  
  339.         
  340.   }  
  341.      public void Start(){  
  342.       new Thread(new Runnable(){  
  343.         @Override  
  344.         public void run() {  
  345.         try {  
  346.         s = new Socket("127.0.0.1" , 20097) ;  
  347.         in = new BufferedReader(new InputStreamReader(s.getInputStream()));  
  348.         out = new PrintWriter(s.getOutputStream(), true);  
  349.         JOptionPane.showMessageDialog(null"連線成功....");  
  350.         jButton10.setEnabled(false);//設定不可頂選  
  351.         t.start();  
  352.         game();  
  353.         } catch (IOException ex) {  
  354.           JOptionPane.showMessageDialog(null,  "沒連接到伺服器 再試一次.... \n");  
  355.           jButton10.setEnabled(true);//設定不可頂選  
  356.          }  
  357.         }  
  358.     }).start();      
  359.      
  360.     }  
  361.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  362.         if(stop != true){  
  363.             jButton1.setText("O");  
  364.             out.println("1");  
  365.             jButton1.setEnabled(false);  
  366.             stop = true ;  
  367.               jLabel3.setText("對方先");    
  368.         }  
  369.     }                                          
  370.   
  371.     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  372.         if(stop != true){  
  373.             jButton2.setText("O");  
  374.             out.println("2");  
  375.             jButton2.setEnabled(false);  
  376.             stop = true ;  
  377.               jLabel3.setText("對方先");    
  378.         }  
  379.     }                                          
  380.   
  381.     private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  382.         if(stop != true){  
  383.             jButton3.setText("O");  
  384.             out.println("3");  
  385.             jButton3.setEnabled(false);  
  386.             stop = true ;  
  387.              jLabel3.setText("對方先");    
  388.         }  
  389.     }                                          
  390.   public void isFull(){//檢查是否滿  
  391.         if(!jButton1.getText().isEmpty() && !jButton2.getText().isEmpty()  && !jButton3.getText().isEmpty()  
  392.            && !jButton4.getText().isEmpty() && !jButton5.getText().isEmpty()  && !jButton6.getText().isEmpty()  
  393.            && !jButton7.getText().isEmpty()  && !jButton8.getText().isEmpty()  && !jButton9.getText().isEmpty() ){  
  394.              Clear() ;  
  395.              out.println("888");  
  396.              JOptionPane.showMessageDialog(null"平手" );  
  397.         }  
  398.     }  
  399.     private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  400.         if(stop != true){  
  401.             jButton4.setText("O");  
  402.             out.println("4");  
  403.             jButton4.setEnabled(false);  
  404.             stop = true ;  
  405.             jLabel3.setText("對方先");    
  406.         }  
  407.     }                                          
  408.   
  409.     private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  410.         if(stop != true){  
  411.             jButton5.setText("O");  
  412.             out.println("5");  
  413.             jButton5.setEnabled(false);  
  414.             stop = true ;  
  415.             jLabel3.setText("對方先");    
  416.         }  
  417.     }                                          
  418.   
  419.     private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  420.         if(stop != true){  
  421.             jButton6.setText("O");  
  422.             out.println("6");  
  423.             jButton6.setEnabled(false);  
  424.             stop = true ;  
  425.             jLabel3.setText("對方先");    
  426.         }  
  427.     }                                          
  428.   
  429.     private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  430.         if(stop != true){  
  431.             jButton7.setText("O");  
  432.             out.println("7");  
  433.             jButton7.setEnabled(false);  
  434.             stop = true ;  
  435.             jLabel3.setText("對方先");    
  436.         }  
  437.     }                                          
  438.   
  439.     private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  440.         if(stop != true){  
  441.             jButton8.setText("O");  
  442.             out.println("8");  
  443.             jButton8.setEnabled(false);  
  444.             stop = true ;  
  445.             jLabel3.setText("對方先");    
  446.         }  
  447.     }                                          
  448.   
  449.     private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  450.         if(stop != true){  
  451.             jButton9.setText("O");  
  452.             out.println("9");  
  453.             jButton9.setEnabled(false);  
  454.             stop = true ;  
  455.             jLabel3.setText("對方先");    
  456.         }  
  457.     }                                          
  458.   
  459.     private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {                                            
  460.       Start() ;  
  461.     }                                           
  462.   
  463.     private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {                                            
  464.         System.exit(0);  
  465.     }                                           
  466.   
  467.     /** 
  468.      * @param args the command line arguments 
  469.      */  
  470.     public static void main(String args[]) {  
  471.         /* Set the Nimbus look and feel */  
  472.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">  
  473.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
  474.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html  
  475.          */  
  476.         try {  
  477.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {  
  478.                 if ("Nimbus".equals(info.getName())) {  
  479.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());  
  480.                     break;  
  481.                 }  
  482.             }  
  483.         } catch (ClassNotFoundException ex) {  
  484.             java.util.logging.Logger.getLogger(toe_Clinet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
  485.         } catch (InstantiationException ex) {  
  486.             java.util.logging.Logger.getLogger(toe_Clinet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
  487.         } catch (IllegalAccessException ex) {  
  488.             java.util.logging.Logger.getLogger(toe_Clinet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
  489.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {  
  490.             java.util.logging.Logger.getLogger(toe_Clinet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
  491.         }  
  492.         //</editor-fold>  
  493.   
  494.         /* Create and display the form */  
  495.         java.awt.EventQueue.invokeLater(new Runnable() {  
  496.             public void run() {  
  497.                 new toe_Clinet().setVisible(true);  
  498.             }  
  499.         });  
  500.     }  
  501.   
  502.     // Variables declaration - do not modify                       
  503.     private javax.swing.JButton jButton1;  
  504.     private javax.swing.JButton jButton10;  
  505.     private javax.swing.JButton jButton11;  
  506.     private javax.swing.JButton jButton2;  
  507.     private javax.swing.JButton jButton3;  
  508.     private javax.swing.JButton jButton4;  
  509.     private javax.swing.JButton jButton5;  
  510.     private javax.swing.JButton jButton6;  
  511.     private javax.swing.JButton jButton7;  
  512.     private javax.swing.JButton jButton8;  
  513.     private javax.swing.JButton jButton9;  
  514.     private javax.swing.JLabel jLabel1;  
  515.     private javax.swing.JLabel jLabel2;  
  516.     private javax.swing.JLabel jLabel3;  
  517.     private javax.swing.JLabel jLabel4;  
  518.     private javax.swing.JLabel jLabel5;  
  519.     private javax.swing.JLabel jLabel6;  
  520.     private javax.swing.JLabel jLabel7;  
  521.     private javax.swing.JPanel jPanel1;  
  522.     // End of variables declaration                     
  523. }  

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。