使用SwingGUI的java简单ClientServer程序
我正在制作一个简单的,无线程客户端-服务器程序,其中GUI在服务器和客户端上都有一个按钮。当客户端按下按钮时,它将按钮上的文本更改为“C”,并发送到服务器“C”字符串,因此服务器侧的按钮将文本更改为“C”服务器的工作原理与客户端类似,但发送的“S”而不是的“C”。它们轮流工作:客户端的轮到时,服务器的按钮被锁定,他无法更改按钮客户端始终首先启动
当客户端按下按钮时,它工作正常,但当服务器按下按钮时,它会在服务器侧将按钮更改为“S”,而不是在客户端侧。我知道我做错了什么
服务器代码:
public class Serv implements ActionListener
{
private JButton button;
private boolean myTurn;
private ServerSocket sock;
private Socket s;
private BufferedReader input;
private PrintStream output;
public Serv() throws UnknownHostException, IOException
{
button = new JButton();
myTurn = false;
sock = new ServerSocket(9001);
s = null;
button = new JButton();
}
public void createGUI()
{
JFrame frame = new JFrame("TicTacToe - Server");
JPanel mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(100, 100));
button = new JButton("");
button.setPreferredSize(new Dimension(100, 100));
button.setFont(new Font(button.getFont().getName(), button.getFont().getStyle(), 70));
button.setActionCommand("1");
button.addActionListener(this);
mainPanel.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void startMyGame() throws IOException
{
createGUI();
s = sock.accept();
input = new BufferedReader(new InputStreamReader(s.getInputStream()));
output = new PrintStream(s.getOutputStream(), true);
while(true)
{
if(myTurn == false)
{
myTurn = true;
String out = input.readLine();
button.setText(out);
}
}
}
public static void main(String args[])
{
Serv tc = null;
try
{
tc = new Serv();
tc.startMyGame();
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
tc.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
private void close() throws IOException
{
this.sock.close();
this.input.close();
this.output.close();
}
@Override
public void actionPerformed(ActionEvent e)
{
if(myTurn == true)
{
if(e.getActionCommand().equals("1"))
{
JButton b = (JButton) e.getSource();
b.setText("S");
output.println("S");
myTurn = false;
}
}
}
}
客户端代码:
public class Cli implements ActionListener
{
private JButton button;
private boolean myTurn;
private Socket sock;
private BufferedReader input;
private PrintStream output;
public Cli() throws UnknownHostException, IOException
{
button = new JButton();
myTurn = true;
sock = new Socket("127.0.0.1", 9001);
input = new BufferedReader(new InputStreamReader(sock.getInputStream()));
output = new PrintStream(sock.getOutputStream(), true);
}
public void createGUI()
{
JFrame frame = new JFrame("TicTacToe - Client");
JPanel mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(100, 100));
button = new JButton("");
button.setPreferredSize(new Dimension(100, 100));
button.setFont(new Font(button.getFont().getName(), button.getFont().getStyle(), 70));
button.setActionCommand("1");
button.addActionListener(this);
mainPanel.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void startMyGame() throws IOException
{
createGUI();
while(true)
{
if(myTurn == false)
{
myTurn = true;
String out = input.readLine();
button.setText(out);
}
}
}
private void close() throws IOException
{
this.sock.close();
this.input.close();
this.output.close();
}
public static void main(String args[])
{
Cli tc = null;
try
{
tc = new Cli();
tc.startMyGame();
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
tc.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
@Override
public void actionPerformed(ActionEvent e)
{
if(myTurn == true)
{
if(e.getActionCommand().equals("1"))
{
JButton b = (JButton) e.getSource();
if(!b.getText().equals("X") || !b.getText().equals("O"))
{
b.setText("C");
output.println("C");
myTurn = false;
}
}
}
}
}
我已经删除了导入,因此代码会更短
# 1 楼答案
代码当前存在的问题:
正在制作一个更干净的示例
例如: