好站連結:程式設計工藝大師
剪刀石頭布
//Client
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client extends JFrame implements ActionListener {
private JTextField JexT = new JTextField();
private JTextArea JtaR = new JTextArea();
DataOutputStream osTS;
DataInputStream iFrS;
public static void main(String[] args) {
new Client();
}
public Client() {
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(new JLabel("Enter Random Number"), BorderLayout.WEST);
p.add(JexT, BorderLayout.CENTER);
JexT.setHorizontalAlignment(JTextField.RIGHT);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p, BorderLayout.SOUTH);
getContentPane().add(new JScrollPane(JtaR), BorderLayout.CENTER);
JexT.addActionListener(this);
setSize(500,300);
setTitle("Client");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
try {
Socket connectToServer = new Socket("localhost", 7000);
iFrS = new DataInputStream(
connectToServer.getInputStream());
osTS =
new DataOutputStream(connectToServer.getOutputStream());
}
catch (IOException ex) {
JtaR.append(ex.toString() + '\n');
}
}
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
if (e.getSource() instanceof JTextField) {
try {
double radius = Double.parseDouble(JexT.getText().trim());
osTS.writeDouble(radius);
osTS.flush();
double area = iFrS.readDouble();
JtaR.append("Number is " + radius + "\n");
JtaR.append("Random Number received from the server is "
+ area + '\n');
if(area==1.0 && radius==1)
JtaR.append("Computer piece " + '\n');
if(area==2.0 && radius==2)
JtaR.append("Computer piece " + '\n');
if(area==3.0 && radius==3)
JtaR.append("Computer piece " + '\n');
if(area==1.0 && radius==2)
JtaR.append("Computer Lose " + '\n');
if(area==2.0 && radius==3)
JtaR.append("Computer Lose " + '\n');
if(area==3.0 && radius==1)
JtaR.append("Computer Lose " + '\n');
if(area==1.0 && radius==3)
JtaR.append("Computer Win " + '\n');
if(area==2.0 && radius==1)
JtaR.append("Computer Win " + '\n');
if(area==3.0 && radius==2)
JtaR.append("Computer Win " + '\n');
}
catch (IOException ex) {
System.err.println(ex);
}
}
}
}
---------------------------------------------------------------------------
執行結果
留言列表