www.pudn.com > WuZiQi_src.rar > NetDialog.java


package game; 
 
import java.awt.Frame; 
import javax.swing.JDialog; 
import com.borland.jbcl.layout.XYLayout; 
import com.borland.jbcl.layout.*; 
import javax.swing.JLabel; 
import javax.swing.JTextField; 
import javax.swing.JButton; 
import javax.swing.JTextArea; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.*; 
import javax.swing.ButtonGroup; 
import javax.swing.JRadioButton; 
 
/** 
 * 

Title: 五子棋

* *

Description: 五子棋游戏

* *

Copyright: Copyright (c) 2005

* *

Company: David Company

* * @author David * @version 1.0 */ public class NetDialog extends JDialog { NetGame netGame=new NetGame(); //NetInfo netinfo; ReadThread rr=new ReadThread(); int role=-1; //1 Server 0 Client public NetDialog(Frame owner, String title, boolean modal,NetGame ng) { super(owner, title, modal); this.netGame=ng; try { setDefaultCloseOperation(DISPOSE_ON_CLOSE); jbInit(); pack(); } catch (Exception exception) { exception.printStackTrace(); } } public int getRole(){ return role; } private void jbInit() throws Exception { this.getContentPane().setLayout(xYLayout1); jServer.setFont(new java.awt.Font("Dialog", Font.PLAIN, 15)); jServer.setText("服务器"); jServer.addActionListener(new NetDialog_jServer_actionAdapter(this)); jLabel1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 13)); jLabel1.setToolTipText(""); jLabel1.setText("端口号:"); jClient.setFont(new java.awt.Font("Dialog", Font.PLAIN, 15)); jClient.setText("客户端"); jClient.addActionListener(new NetDialog_jClient_actionAdapter(this)); jLabel2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 13)); jLabel2.setText("服务器名:"); jServerName.setText("127.0.0.1"); jPort.setText("2000"); jTextInfo.setText("jTextArea1"); xYLayout1.setWidth(422); xYLayout1.setHeight(314); jBtnServer.setEnabled(false); jBtnServer.setFont(new java.awt.Font("Dialog", Font.PLAIN, 13)); jBtnServer.setText("启动服务器监听"); jBtnServer.addActionListener(new NetDialog_jBtnServer_actionAdapter(this)); jBtnClient.setEnabled(false); jBtnClient.setFont(new java.awt.Font("Dialog", Font.PLAIN, 13)); jBtnClient.setText("连接服务器"); jBtnClient.addActionListener(new NetDialog_jBtnClient_actionAdapter(this)); jButton3.setFont(new java.awt.Font("Dialog", Font.PLAIN, 13)); jButton3.setText("退出"); jButton3.addActionListener(new NetDialog_jButton3_actionAdapter(this)); this.getContentPane().add(jLabel2, new XYConstraints(31, 66, 70, 24)); this.getContentPane().add(jClient, new XYConstraints(222, 22, 103, -1)); this.getContentPane().add(jLabel1, new XYConstraints(30, 113, 61, 25)); this.getContentPane().add(jServerName, new XYConstraints(102, 66, 285, 27)); this.getContentPane().add(jPort, new XYConstraints(101, 110, 285, 27)); this.getContentPane().add(jTextInfo, new XYConstraints(27, 150, 360, 111)); this.getContentPane().add(jBtnServer, new XYConstraints(29, 273, 128, 27)); this.getContentPane().add(jBtnClient, new XYConstraints(168, 274, 122, 27)); this.getContentPane().add(jButton3, new XYConstraints(303, 275, 84, 26)); this.getContentPane().add(jServer, new XYConstraints(73, 23, -1, -1)); jTextInfo.setText(""); } XYLayout xYLayout1 = new XYLayout(); ButtonGroup buttonGroup1 = new ButtonGroup(); ButtonGroup buttonGroup2 = new ButtonGroup(); JRadioButton jServer = new JRadioButton(); JRadioButton jClient = new JRadioButton(); JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); JTextField jServerName = new JTextField(); JTextField jPort = new JTextField(); JTextArea jTextInfo = new JTextArea(); JButton jBtnServer = new JButton(); JButton jBtnClient = new JButton(); JButton jButton3 = new JButton(); //退出 public void jButton3_actionPerformed(ActionEvent e) { if (jServer.isSelected()) role=1; if (jClient.isSelected()) role=0; this.setVisible(false); } //选择“服务器” public void jServer_actionPerformed(ActionEvent e) { jClient.setSelected(false); jServerName.setEnabled(false); jPort.setEnabled(true); jBtnServer.setEnabled(true); jBtnClient.setEnabled(false); } //选择“客户端” public void jClient_actionPerformed(ActionEvent e) { jServer.setSelected(false); jServerName.setEnabled(true); jPort.setEnabled(true); jBtnServer.setEnabled(false); jBtnClient.setEnabled(true); } //服务器Listen线程 class ListenThread implements Runnable{ public void run(){ jTextInfo.append("Server is listening...\n"); netGame.serverListen(Integer.parseInt(jPort.getText())); jTextInfo.append(netGame.getStates()+"\n"); if (netGame.serverAccept()) { jTextInfo.append(netGame.getStates()+"\n"); jTextInfo.append("已经成功连接到客户端,退出该窗口选择New Game.\n"); netGame.setSelectNet(1); } } } //点击 SERVER public void jBtnServer_actionPerformed(ActionEvent e) { ListenThread lr=new ListenThread(); Thread lt=new Thread(lr); lt.start(); } //客户端Link线程 class ClientThread implements Runnable{ public void run(){ jTextInfo.append("Searing the host....please wait.\n"); if ( netGame.clientLink(jServerName.getText(),Integer.parseInt(jPort.getText()))) { jTextInfo.append(netGame.getStates()+"\n"); jTextInfo.append("已经成功连接服务器,退出该窗口选择New Game.\n"); netGame.setSelectNet(2); } } } //点击 Client public void jBtnClient_actionPerformed(ActionEvent e) { ClientThread cr=new ClientThread(); Thread ct=new Thread(cr); ct.start(); } /*实验:server发数据 public void jButton1_actionPerformed(ActionEvent e) { System.out.println("Sending....."); this.startWriteThread("Hello"); //netGame.send("Hello"); System.out.println("Sending Finish!"); } //实验:client收数据 public void jButton2_actionPerformed(ActionEvent e) { System.out.println("Receive....."); this.startReadThread(); //System.out.println("Receive = " + netGame.receive()); try{ Thread.currentThread().sleep(5000); System.out.println("**Receive=" + this.getStr()); /////////////////////// System.out.println("Receive Finish!"); }catch( Exception ie){ System.out.println("Wrong....from sleep"); } }s */ class ReadThread implements Runnable{ String str=new String(); public ReadThread(){ str=""; } public void run() { this.str=netGame.receive(); } public String getStr(){ return str; } public void setStr(String str){ this.str=""; } } public void startReadThread(){ //rr=new ReadThread(); Thread tr=new Thread(rr); tr.start(); try{ tr.join(); }catch(InterruptedException iie){ System.out.println("join wrong"); } return ; } public String getStr(){ return rr.getStr(); } public void setStr(String str){ rr.setStr(str); } public void startWriteThread(String str){ netGame.send(str); return ; } } class NetDialog_jBtnClient_actionAdapter implements ActionListener { private NetDialog adaptee; NetDialog_jBtnClient_actionAdapter(NetDialog adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jBtnClient_actionPerformed(e); } } class NetDialog_jBtnServer_actionAdapter implements ActionListener { private NetDialog adaptee; NetDialog_jBtnServer_actionAdapter(NetDialog adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jBtnServer_actionPerformed(e); } } class NetDialog_jClient_actionAdapter implements ActionListener { private NetDialog adaptee; NetDialog_jClient_actionAdapter(NetDialog adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jClient_actionPerformed(e); } } class NetDialog_jButton3_actionAdapter implements ActionListener { private NetDialog adaptee; NetDialog_jButton3_actionAdapter(NetDialog adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton3_actionPerformed(e); } } class NetDialog_jServer_actionAdapter implements ActionListener { private NetDialog adaptee; NetDialog_jServer_actionAdapter(NetDialog adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jServer_actionPerformed(e); } }