<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ![](https://box.kancloud.cn/dafb028faa1909f6b1ec91424f2cf7c9_692x295.png) XiangQi.java ~~~ import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.net.Socket; public class XiangQi extends JFrame implements ActionListener { public static final Color bgColor=new Color(245,250,160);//棋盤的背景色 public static final Color focusbg=new Color(242,242,242);//棋子選中后的背景色 public static final Color focuschar=new Color(96,95,91);//棋子選中后的字符顏色 public static final Color color1=new Color(249,183,173);//紅方的顏色 public static final Color color2=Color.white;//白方的顏色 JLabel jlHost=new JLabel("主機名");//創建提示輸入主機名的標簽 JLabel jlPort=new JLabel("端口號");////創建提示輸入端口號標簽 JLabel jlNickName=new JLabel("昵 稱");//創建提示輸入昵稱的標簽 JTextField jtfHost=new JTextField("127.0.0.1");//創建輸入主機名的文本框,默認值是"127.0.0.1" JTextField jtfPort=new JTextField("9999");//創建輸入端口號的文本框,默認值是9999 JTextField jtfNickName=new JTextField("Play1");//創建輸入昵稱的文本框,默認值是Play1 JButton jbConnect=new JButton("連 接");//創建"連接"按鈕 JButton jbDisconnect=new JButton("斷 開");//創建"斷開"按鈕 JButton jbFail=new JButton("認 輸");//創建"認輸"按鈕 JButton jbChallenge=new JButton("挑 戰");//創建"挑戰"按鈕 JComboBox jcbNickList=new JComboBox();//創建存放當前用戶的下拉列表框 JButton jbYChallenge=new JButton("接受挑戰");//創建"接受挑戰"按鈕 JButton jbNChallenge=new JButton("拒絕挑戰");//創建"拒絕挑戰"按鈕 int width=60;//設置棋盤兩線之間的距離//單位是像素 QiZi[][] qiZi=new QiZi[9][10];//創建棋子數組 //QiPan jpz=new QiPan(qiZi,width,this);//創建棋盤 JPanel jpz=new JPanel();//創建一個JPanel,暫時代替棋盤 JPanel jpy=new JPanel();//創建一個JPanel JSplitPane jsp=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jpz,jpy);//創建一個JSplitPane boolean caiPan=false;//可否走棋的標志位 int color=0;//0 代表紅棋,1代表白棋 Socket sc;//聲明Socket引用 //ClientAgentThread cat;//聲明客戶端代理線程的引用 public XiangQi() { this.initialComponent();//初始化控件 this.addListener();//為相應控件注冊事件監聽器 this.initialState();//初始化狀態 this.initialQiZi();//初始化棋子 this.initialFrame();//初始化窗體 } public void initialComponent()//初始化控件 { jpy.setLayout(null);//設為空布局 this.jlHost.setBounds(10,10,50,20);//前兩個參數是大小,后面兩個是位置 jpy.add(this.jlHost);//添加"主機名"標簽 this.jtfHost.setBounds(70,10,80,20); jpy.add(this.jtfHost);//添加用于輸入主機名的文本框 this.jlPort.setBounds(10,40,50,20); jpy.add(this.jlPort);//添加"端口號"標簽 this.jtfPort.setBounds(70,40,80,20); jpy.add(this.jtfPort);//添加用于輸入端口號的文本框 this.jlNickName.setBounds(10,70,50,20); jpy.add(this.jlNickName);//添加"昵稱"標簽 this.jtfNickName.setBounds(70,70,80,20); jpy.add(this.jtfNickName);//添加用于輸入昵稱的文本框 this.jbConnect.setBounds(10,100,80,20); jpy.add(this.jbConnect);//添加"連接"按鈕 this.jbDisconnect.setBounds(100,100,80,20); jpy.add(this.jbDisconnect);//添加"斷開"按鈕 this.jcbNickList.setBounds(20,130,130,20); jpy.add(this.jcbNickList);//添加用于顯示當前用戶的下拉列表框 this.jbChallenge.setBounds(10,160,80,20); jpy.add(this.jbChallenge);//添加"挑戰"按鈕 this.jbFail.setBounds(100,160,80,20); jpy.add(this.jbFail);//添加"認輸"按鈕 this.jbYChallenge.setBounds(5,190,86,20); jpy.add(this.jbYChallenge);//添加"接受挑戰"按鈕 this.jbNChallenge.setBounds(100,190,86,20); jpy.add(this.jbNChallenge);//添加"拒絕挑戰"按鈕 jpz.setLayout(null);//將棋盤設為空布局 jpz.setBounds(0,0,700,700);//設置大小 } public void addListener()//為相應控件注冊事件監聽器 { this.jbConnect.addActionListener(this);//為"連接"按鈕注冊事件監聽器 this.jbDisconnect.addActionListener(this);//為"斷開"按鈕注冊事件監聽器 this.jbChallenge.addActionListener(this);//為"挑戰"按鈕注冊事件監聽器 this.jbFail.addActionListener(this);//為"認輸"按鈕注冊事件監聽器 this.jbYChallenge.addActionListener(this);//為"同意挑戰"按鈕注冊事件監聽器 this.jbNChallenge.addActionListener(this);//為"拒絕挑戰"按鈕注冊事件監聽器 } public void initialState()//初始化狀態 { this.jbDisconnect.setEnabled(false);//將"斷開"按鈕設為不可用 this.jbChallenge.setEnabled(false);//將"挑戰"按鈕設為不可用 this.jbYChallenge.setEnabled(false);//將"接受挑戰"按鈕設為不可用 this.jbNChallenge.setEnabled(false);//將"拒絕挑戰"按鈕設為不可用 this.jbFail.setEnabled(false);//將"認輸"按鈕設為不可用 } public void initialQiZi()//初始化棋子 {//初始化各個棋子 qiZi[0][0]=new QiZi(color1,"車",0,0);//0行0列 qiZi[1][0]=new QiZi(color1,"馬",1,0);//0行1列 qiZi[2][0]=new QiZi(color1,"相",2,0); qiZi[3][0]=new QiZi(color1,"仕",3,0); qiZi[4][0]=new QiZi(color1,"帥",4,0); qiZi[5][0]=new QiZi(color1,"仕",5,0); qiZi[6][0]=new QiZi(color1,"相",6,0); qiZi[7][0]=new QiZi(color1,"馬",7,0); qiZi[8][0]=new QiZi(color1,"車",8,0); qiZi[1][2]=new QiZi(color1,"砲",1,2); qiZi[7][2]=new QiZi(color1,"砲",7,2); qiZi[0][3]=new QiZi(color1,"兵",0,3); qiZi[2][3]=new QiZi(color1,"兵",2,3); qiZi[4][3]=new QiZi(color1,"兵",4,3); qiZi[6][3]=new QiZi(color1,"兵",6,3); qiZi[8][3]=new QiZi(color1,"兵",8,3); qiZi[0][9]=new QiZi(color2,"車",0,9); qiZi[1][9]=new QiZi(color2,"馬",1,9); qiZi[2][9]=new QiZi(color2,"象",2,9); qiZi[3][9]=new QiZi(color2,"士",3,9); qiZi[4][9]=new QiZi(color2,"將",4,9); qiZi[5][9]=new QiZi(color2,"士",5,9); qiZi[6][9]=new QiZi(color2,"象",6,9); qiZi[7][9]=new QiZi(color2,"馬",7,9); qiZi[8][9]=new QiZi(color2,"車",8,9); qiZi[1][7]=new QiZi(color2,"炮",1,7); qiZi[7][7]=new QiZi(color2,"炮",7,7); qiZi[0][6]=new QiZi(color2,"卒",0,6); qiZi[2][6]=new QiZi(color2,"卒",2,6); qiZi[4][6]=new QiZi(color2,"卒",4,6); qiZi[6][6]=new QiZi(color2,"卒",6,6); qiZi[8][6]=new QiZi(color2,"卒",8,6); } public void initialFrame()//初始化窗體 { this.setTitle("中國象棋--客戶端");//設置窗體標題 Image image=new ImageIcon("ico.gif").getImage(); this.setIconImage(image); //設置圖標 this.add(this.jsp);//添加JSplitPane jsp.setDividerLocation(730);//設置分割線位置及寬度 jsp.setDividerSize(4); this.setBounds(30,30,930,730);//設置窗體大小 this.setVisible(true);//設置可見性 this.addWindowListener(//為窗體添加監聽器 new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0);//退出 } } ); } @Override public void actionPerformed(ActionEvent e) { if(e.getSource()==this.jbConnect) {//單擊"連接"按鈕 this.jbConnect_event(); } else if(e.getSource()==this.jbDisconnect) {//當單擊"斷開"按鈕時 this.jbDisconnect_event(); } else if(e.getSource()==this.jbChallenge) {//當單擊"挑戰"按鈕時 this.jbChallenge_event(); } else if(e.getSource()==this.jbYChallenge) {//當單擊"同意挑戰"按鈕時 this.jbYChallenge_event(); } else if(e.getSource()==this.jbNChallenge) {//當單擊"拒絕挑戰"按鈕時 this.jbNChallenge_event(); } else if(e.getSource()==this.jbFail) {//當單擊"認輸"按鈕時 this.jbFail_event(); } } public void jbConnect_event() {//對單擊"連接"按鈕事件的業務處理代碼 int port=0; try {//獲得用戶輸入的斷口號并轉化為整型 port=Integer.parseInt(this.jtfPort.getText().trim()); } catch(Exception ee) {//不是整數,給出錯誤提示 JOptionPane.showMessageDialog(this,"端口號只能是整數","錯誤", JOptionPane.ERROR_MESSAGE); return; } if(port>65535||port<0) {//端口號不合法,給出錯誤提示 JOptionPane.showMessageDialog(this,"端口號只能是0-65535的整數","錯誤", JOptionPane.ERROR_MESSAGE); return; } String name=this.jtfNickName.getText().trim();//獲得昵稱 if(name.length()==0) {//昵稱為空,給出錯誤提示信息 JOptionPane.showMessageDialog(this,"玩家姓名不能為空","錯誤", JOptionPane.ERROR_MESSAGE); return; } try { sc=new Socket(this.jtfHost.getText().trim(),port);//創建Socket對象 //cat=new ClientAgentThread(this);//創建客戶端代理線程 //cat.start();//啟動客戶端代理線程 this.jtfHost.setEnabled(false);//將用于輸入主機名的文本框設為不可用 this.jtfPort.setEnabled(false);//將用于輸入端口號的文本框設為不可用 this.jtfNickName.setEnabled(false);//將用于輸入昵稱的文本框設為不可用 this.jbConnect.setEnabled(false);//將"連接"按鈕設為不可用 this.jbDisconnect.setEnabled(true);//將"斷開"按鈕設為可用 this.jbChallenge.setEnabled(true);//將"挑戰"按鈕設為可用 this.jbYChallenge.setEnabled(false);//將"接受挑戰"按鈕設為不可用 this.jbNChallenge.setEnabled(false);//將"拒絕挑戰"按鈕設為不可用 this.jbFail.setEnabled(false);//將"認輸"按鈕設為不可用 JOptionPane.showMessageDialog(this,"已連接到服務器","提示", JOptionPane.INFORMATION_MESSAGE);//連接成功,給出提示信息 } catch(Exception ee) { JOptionPane.showMessageDialog(this,"連接服務器失敗","錯誤", JOptionPane.ERROR_MESSAGE);//連接失敗,給出提示信息 return; } } public void jbDisconnect_event() {//對單擊"斷開"按鈕事件的業務處理代碼 try { //this.cat.dout.writeUTF("<#CLIENT_LEAVE#>");//向服務器發送離開的信息 //this.cat.flag=false;//終止客戶端代理線程 //this.cat=null; this.jtfHost.setEnabled(!false);//將用于輸入主機名的文本框設為可用 this.jtfPort.setEnabled(!false);//將用于輸入端口號的文本框設為可用 this.jtfNickName.setEnabled(!false);//將用于輸入昵稱的文本框設為可用 this.jbConnect.setEnabled(!false);//將"連接"按鈕設為可用 this.jbDisconnect.setEnabled(!true);//將"斷開"按鈕設為不可用 this.jbChallenge.setEnabled(!true);//將"挑戰"按鈕設為不可用 this.jbYChallenge.setEnabled(false);//將"接受挑戰"按鈕設為不可用 this.jbNChallenge.setEnabled(false);//將"拒絕挑戰"按鈕設為不可用 this.jbFail.setEnabled(false);//將"認輸"按鈕設為不可用 } catch(Exception ee) { ee.printStackTrace(); } } public void jbChallenge_event(){ //獲得用戶選中的挑戰對象 Object o=this.jcbNickList.getSelectedItem(); if(o==null||((String)o).equals("")) {//z前面是什么都沒有,后面是有"" JOptionPane.showMessageDialog(this,"請選擇對方名字","錯誤", JOptionPane.ERROR_MESSAGE);//當未選中挑戰對象,給出錯誤提示信息 } else{ String name2=(String)this.jcbNickList.getSelectedItem();//獲得挑戰對象 try{ this.jtfHost.setEnabled(false);//將用于輸入主機名的文本框設為不可用 this.jtfPort.setEnabled(false);//將用于輸入端口號的文本框設為不可用 this.jtfNickName.setEnabled(false);//將用于輸入昵稱的文本框設為不可用 this.jbConnect.setEnabled(false);//將"連接"按鈕設為不可用 this.jbDisconnect.setEnabled(!true);//將"斷開"按鈕設為不可用 this.jbChallenge.setEnabled(!true);//將"挑戰"按鈕設為不可用 this.jbYChallenge.setEnabled(false);//將"接受挑戰"按鈕設為不可用 this.jbNChallenge.setEnabled(false);//將"拒絕挑戰"按鈕設為不可用 this.jbFail.setEnabled(false);//將"認輸"按鈕設為不可用 //this.cat.tiaoZhanZhe=name2;//設置挑戰對象 this.caiPan=true;//將caiPan設為true//z棋盤可動了 this.color=0;//將color設為0//z自己變為紅棋 //this.cat.dout.writeUTF("<#TIAO_ZHAN#>"+name2);//發送挑戰信息 JOptionPane.showMessageDialog(this,"已提出挑戰,請等待恢復...","提示", JOptionPane.INFORMATION_MESSAGE);//給出信息發出的提示信息 } catch(Exception ee){ee.printStackTrace();} } } public void jbYChallenge_event(){ try{ //發送同意挑戰的信息 //this.cat.dout.writeUTF("<#TONG_YI#>"+this.cat.tiaoZhanZhe); this.caiPan=false;//將caiPan設為false//z棋盤不可動 this.color=1;//將color設為1//z為黑棋 this.jtfHost.setEnabled(false);//將用于輸入主機名的文本框設為不可用 this.jtfPort.setEnabled(false);//將用于輸入端口號的文本框設為不可用 this.jtfNickName.setEnabled(false);//將用于輸入昵稱的文本框設為不可用 this.jbConnect.setEnabled(false);//將"連接"按鈕設為不可用 this.jbDisconnect.setEnabled(!true);//將"斷開"按鈕設為不可用 this.jbChallenge.setEnabled(!true);//將"挑戰"按鈕設為不可用 this.jbYChallenge.setEnabled(false);//將"接受挑戰"按鈕設為不可用 this.jbNChallenge.setEnabled(false);//將"拒絕挑戰"按鈕設為不可用 this.jbFail.setEnabled(!false);//將"認輸"按鈕設為可用 } catch(Exception ee){ee.printStackTrace();} } public void jbNChallenge_event(){ try{ //發送拒絕挑戰的信息 //this.cat.dout.writeUTF("<#BUTONG_YI#>"+this.cat.tiaoZhanZhe); //this.cat.tiaoZhanZhe=null;//將tiaoZhanZhe設為空 this.jtfHost.setEnabled(false);//將用于輸入主機名的文本框設為不可用 this.jtfPort.setEnabled(false);//將用于輸入端口號的文本框設為不可用 this.jtfNickName.setEnabled(false);//將用于輸入昵稱的文本框設為不可用 this.jbConnect.setEnabled(false);//將"連接"按鈕設為不可用 this.jbDisconnect.setEnabled(true);//將"斷開"按鈕設為可用 this.jbChallenge.setEnabled(true);//將"挑戰"按鈕設為可用 this.jbYChallenge.setEnabled(false);//將"接受挑戰"按鈕設為不可用 this.jbNChallenge.setEnabled(false);//將"拒絕挑戰"按鈕設為不可用 this.jbFail.setEnabled(false);//將"認輸"按鈕設為不可用 } catch(Exception ee){ee.printStackTrace();} } public void jbFail_event(){ try{ //發送認輸的信息 //this.cat.dout.writeUTF("<#RENSHU#>"+this.cat.tiaoZhanZhe); //this.cat.tiaoZhanZhe=null;//將tiaoZhanZhe設為空 this.color=0;//將color設為0 this.caiPan=false;//將caiPan設為false this.next();//初始化下一局 this.jtfHost.setEnabled(false);//將用于輸入主機名的文本框設為不可用 this.jtfPort.setEnabled(false);//將用于輸入端口號的文本框設為不可用 this.jtfNickName.setEnabled(false);//將用于輸入昵稱的文本框設為不可用 this.jbConnect.setEnabled(false);//將"連接"按鈕設為不可用 this.jbDisconnect.setEnabled(true);//將"斷開"按鈕設為可用 this.jbChallenge.setEnabled(true);//將"挑戰"按鈕設為可用 this.jbYChallenge.setEnabled(false);//將"接受挑戰"按鈕設為不可用 this.jbNChallenge.setEnabled(false);//將"拒絕挑戰"按鈕設為不可用 this.jbFail.setEnabled(false);//將"認輸"按鈕設為不可用 } catch(Exception ee){ee.printStackTrace();} } public void next(){//初始化下一局,恢復棋盤 for(int i=0;i<9;i++){//將棋子數組都置空 for(int j=0;j<10;j++){ this.qiZi[i][j]=null; } } this.caiPan=false; this.initialQiZi();//重新初始化棋子 this.repaint();//重繪 } public static void main(String args[]) { new XiangQi(); } } ~~~ 先啟動服務器,再啟動該類的結果 ![](https://box.kancloud.cn/0409b67e7dee0f3e86eef00057515625_1217x839.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看