📄 wuziqiclientframe.java~75~
字号:
package wuziqiclient;import java.awt.*;import java.awt.event.*;import javax.swing.*;import com.borland.jbcl.layout.*;import java.io.*;import java.net.*;import java.util.*;public class wuziqiClientFrame extends JFrame{ JPanel contentPane; XYLayout xYLayout1 = new XYLayout(); JSplitPane jSplitPane1 = new JSplitPane(); JPanel jPanel1 = new JPanel(); XYLayout xYLayout2 = new XYLayout(); JScrollPane jScrollPane1 = new JScrollPane(); JTextArea chatTextArea = new JTextArea(); JLabel IPLabel = new JLabel(); JTextField InputIp = new JTextField(); JButton ConnectGame = new JButton(); JButton CreateGame = new JButton(); JButton JoinGame = new JButton(); JButton GiveUpGame = new JButton(); JButton CloseGame = new JButton(); JComboBox userChoice = new JComboBox(); JTextField userWords = new JTextField(); JScrollPane jScrollPane2 = new JScrollPane(); JList userList = new JList(); chessPanel chesspanel=new chessPanel(); //增加成员变量,用于通信; Socket chatSocket; //用户基本聊天信息和指令 DataInputStream in;//输入流 DataOutputStream out;//输出流 String chessClientName = null;//游戏客户名 String host = null; //服务器名 int port = 4331; //服务器连接端口 boolean isOnChat = false; //在聊天? boolean isOnChess = false; //在下棋? boolean isGameConnected = false; //下棋的客户端连接? boolean isServer = false; //如果是下棋的主机 boolean isClient = false; //如果是下棋的客户端 Vector ListL=new Vector();//用于存放用户列表信息 Vector ListChoice=new Vector();// //构造函数 public wuziqiClientFrame(){ enableEvents(AWTEvent.WINDOW_EVENT_MASK); try{ //图形用户界面 jbInit(); } catch(Exception e){ e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception{ //各个部件的初始化 for(int i = 0; i < 50; i++){ ListL.add(i + "." + "没有用户"); } //用户列表的初始化 userChoice=new JComboBox(ListChoice); userList = new JList(ListL); contentPane = (JPanel) this.getContentPane(); jSplitPane1.setDebugGraphicsOptions(DebugGraphics.BUFFERED_OPTION); jSplitPane1.setContinuousLayout(false); jSplitPane1.setDividerSize(5); contentPane.setLayout(xYLayout1); this.getContentPane().setBackground(SystemColor.inactiveCaptionText); this.setSize(new Dimension(643, 580)); this.setTitle("五子棋客户端"); jPanel1.setLayout(xYLayout2); chatTextArea.setToolTipText(""); chatTextArea.setText(""); jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); IPLabel.setText("IP"); InputIp.setText("localhost"); ConnectGame.setFont(new java.awt.Font("Dialog", 0, 12)); ConnectGame.setToolTipText(""); ConnectGame.setText("连接主机"); ConnectGame.addActionListener(new wuziqiClientFrame_ConnectGame_actionAdapter(this)); CreateGame.setFont(new java.awt.Font("Dialog", 0, 12)); CreateGame.setText("创建游戏"); CreateGame.addActionListener(new wuziqiClientFrame_CreateGame_actionAdapter(this)); JoinGame.setFont(new java.awt.Font("Dialog", 0, 12)); JoinGame.setText("加入游戏"); JoinGame.addActionListener(new wuziqiClientFrame_JoinGame_actionAdapter(this)); GiveUpGame.setFont(new java.awt.Font("Dialog", 0, 12)); GiveUpGame.setText("放弃游戏"); GiveUpGame.addActionListener(new wuziqiClientFrame_GiveUpGame_actionAdapter(this)); CloseGame.setFont(new java.awt.Font("Dialog", 0, 12)); CloseGame.setText("关闭程序"); CloseGame.addActionListener(new wuziqiClientFrame_CloseGame_actionAdapter(this)); userWords.setText(""); userWords.addKeyListener(new wuziqiClientFrame_userWords_keyAdapter(this)); contentPane.setBackground(SystemColor.inactiveCaption); userChoice.setFont(new java.awt.Font("SansSerif", 0, 13)); userList.setFont(new java.awt.Font("SansSerif", 0, 13)); contentPane.add(jSplitPane1, new XYConstraints(-1, 0, 639, 490)); jSplitPane1.add(jPanel1, JSplitPane.LEFT); jPanel1.add(jScrollPane1, new XYConstraints(3, 139, 177, 325)); jPanel1.add(jScrollPane2, new XYConstraints(3, 5, 172, 128)); jSplitPane1.add(chesspanel, JSplitPane.RIGHT); jScrollPane2.getViewport().add(userList, null); jScrollPane1.getViewport().add(chatTextArea, null); // jSplitPane1.remove(jSplitPane1.getRightComponent()); contentPane.add(userWords, new XYConstraints(154, 520, 431, 28)); contentPane.add(InputIp, new XYConstraints(43, 494, 101, 23)); contentPane.add(IPLabel, new XYConstraints(20, 497, 23, 19)); contentPane.add(CloseGame, new XYConstraints(542, 492, 90, -1)); contentPane.add(GiveUpGame, new XYConstraints(447, 492, 90, -1)); contentPane.add(JoinGame, new XYConstraints(348, 492, 90, -1)); contentPane.add(ConnectGame, new XYConstraints(155, 492, 90, -1)); contentPane.add(CreateGame, new XYConstraints(255, 492, 90, -1)); contentPane.add(userChoice, new XYConstraints(41, 522, 103, -1)); jSplitPane1.setDividerLocation(180); } //Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e){ super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING){ System.exit(0); } } //连接到服务器的指定端口,并返回其输入和输出流,并启动客户线程负责和服务器的连接; public boolean connectServer(String serverIP, int serverPort)throws Exception{ try{ chatSocket = new Socket(serverIP, serverPort); System.out.println("客户端的IP地址:"+chatSocket); in = new DataInputStream(chatSocket.getInputStream()); out = new DataOutputStream(chatSocket.getOutputStream()); clientThread clientthread = new clientThread(this); clientthread.start(); isOnChat = true; return true; } catch (IOException ex){ chatTextArea.setText("chessClient:connectServer:无法连接,建议重新启动程序 \n"); } return false; } void userWords_keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER){ if (userChoice.getSelectedItem().equals("所有人")) { try { out.writeUTF(userWords.getText()); userWords.setText(""); } catch (Exception ea) { this.chatTextArea.setText("chessClient:KeyPressed无法连接,建议重新连接 \n"); userList.removeAll(); userChoice.removeAll(); userWords.setText(""); this.ConnectGame.setEnabled(true); } } else { try { out.writeUTF("/" + userChoice.getSelectedItem() + " " + userWords.getText()); userWords.setText(""); } catch (Exception ea) { this.chatTextArea.setText("chessClient:KeyPressed无法连接,建议重新连接 \n"); userList.removeAll(); userChoice.removeAll(); userWords.setText(""); this.ConnectGame.setEnabled(true); } } } } void ConnectGame_actionPerformed(ActionEvent e) { host = chesspanel.host = InputIp.getText(); try { if (connectServer(host, port)){ this.chatTextArea.setText(""); this.ConnectGame.setEnabled(false); this.CreateGame.setEnabled(true); this.JoinGame.setEnabled(true); chesspanel.jTextField1.setText("连接成功,请创建游戏或加入游戏"); } } catch (Exception ei){ this.chatTextArea.setText( "controlpad.connectButton:无法连接,建议重新启动程序 \n"); } } void CloseGame_actionPerformed(ActionEvent e) { if (isOnChat) { try { chatSocket.close(); } catch (Exception ed) { } } if (isOnChess || isGameConnected) { try { chesspanel.chessSocket.close(); } catch (Exception ee) { } } System.exit(0); } void JoinGame_actionPerformed(ActionEvent e) { String selectedUser = (String)userList.getSelectedValue(); if (selectedUser == null || selectedUser.startsWith("[inchess]") || selectedUser.equals(chessClientName)){ chesspanel.jTextField1.setText("必须先选定一个有效用户"); } else{ try { if (!isGameConnected){ if (chesspanel.connectServer(chesspanel.host, chesspanel.port)){ isGameConnected = true; isOnChess = true; isClient = true; CreateGame.setEnabled(false); JoinGame.setEnabled(false); GiveUpGame.setEnabled(true); chesspanel.chessthread.sendMessage("/joingame " + selectedUser + " " + chessClientName); } } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -