Java Jbutton未在单击时注册(服务器客户端设置)

Java Jbutton未在单击时注册(服务器客户端设置),java,server,client,Java,Server,Client,因此,在制作这个小游戏时,我重播了两次,第一次是为了设置服务器,第二次是为了从接口类中提取常量并将其放入客户端,我认为这是可以的我的主要问题是当播放器1(目前为atm实现)尝试单击“垃圾邮件赢取”时,当两个客户端都启动时,代码不会触发。因此,所有按钮都不起任何作用。为什么他们不会触发。我正确地调用了actionListener,甚至尝试过将按钮设置为静态和非静态 这是一个尚未完成的服务器,但它执行的任务是当前将两个人连接到一个服务器 import java.io.*; import java.n

因此,在制作这个小游戏时,我重播了两次,第一次是为了设置服务器,第二次是为了从接口类中提取常量并将其放入客户端,我认为这是可以的我的主要问题是当播放器1(目前为atm实现)尝试单击“垃圾邮件赢取”时,当两个客户端都启动时,代码不会触发。因此,所有按钮都不起任何作用。为什么他们不会触发。我正确地调用了actionListener,甚至尝试过将按钮设置为静态和非静态

这是一个尚未完成的服务器,但它执行的任务是当前将两个人连接到一个服务器

import java.io.*;
import java.net.*;

import javax.swing.*;

import java.awt.*;
import java.util.Date;
import java.util.Scanner;

public class Server extends JFrame
{
    public static Socket player1;
    public static Scanner player1Input;
    public static PrintWriter player1Output;
    public static Socket player2;
    public static Scanner player2Input;
    public static PrintWriter player2Output;
    public static ServerSocket server;

public static void main(String[] args) throws IOException 
{
    final int SBAP_PORT = 8888;
    server = new ServerSocket(SBAP_PORT);
    //Frame
    Server frame = new Server();
    System.out.println("Waiting for clients to connect...");

    //Player1
    player1 = server.accept();
    System.out.println("Player 1 has joined."); 

    //input/Output for player 1
    player1Input = new Scanner (player1.getInputStream());
    player1Output = new PrintWriter (player1.getOutputStream());

    //Label player 1
    player1Output.println(1);
    player1Output.flush();
    System.out.println("Waiting for player 2 to connect...");

    //Player1
    player2 = server.accept();
    System.out.println("Player 2 has joined."); 

    //input/Output for player 1
    player2Input = new Scanner (player2.getInputStream());
    player2Output = new PrintWriter (player2.getOutputStream());

    //Label player 1
    player2Output.println(2);
    player2Output.flush();
    System.out.println("Both players have connected.");
}

public Server()
{
    //Frame
    JTextArea jtaLog = new JTextArea();   

    // Create a scroll pane to hold text area
    JScrollPane scrollPane = new JScrollPane(jtaLog);

    // Add the scroll pane to the frame
    add(scrollPane, BorderLayout.CENTER);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(450, 300);
    setTitle("Server");
    setVisible(true);
}

public void run() 
{
while (true) 
  {
      if(isWon() == 1)
      {

      }
      else if(isWon() == 2)
      {

      }
      else 
      {

      }
  }
}

/** Total clicks to win the game */
private int isWon() 
{
    if(player1Input.equals("WinP1"))
    {
        return 1;
    }
    if(player2Input.equals("WinP2"))
    {
        return 2;
    }
    return 3;
}
}
这是我为两个玩家生成的标准客户端,它有标题栏、状态栏、劣势按钮、垃圾邮件按钮和优势按钮

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.io.*;
import java.net.*;
import java.util.Random;
import java.util.Scanner;

public class Client extends JApplet implements Runnable
{
    private static final JButton MainClick = new JButton("Spam to win!");
    private static final JButton Advantage = new JButton("Spend 25 " + "clicks for " + "advantages");
    private static final JButton Disadvantage = new JButton("Spend 25 " + "clicks to " + "sabotage " + "your " + "opponent");

    // Starting Value of clicks for player 1
    public int player1Amount = 0;
    // Starting Value of clicks for player 2
    public int player2Amount = 0;
    // Multi-click for Player 1
    public boolean multiclicksP1 = false;
    // Multi-click for Player 2
    public boolean multiclicksP2 = false;
    // 2for1 for Player 1
    public boolean player1TwoForOne = false;
    // 2for1 count for Player 1
    public int player1TwoForOneCount = 0;
    // 2for1 timer
    public Thread timer = new Thread();
    // Enable Countdown for timer
    public boolean countDown = false;
    // Time
    public int time;

    // Creates a random number between 1-4
    Random randomGenerator = new Random();
    int randomNum1 = randomGenerator.nextInt(4) + 1;
    int randomNum2 = randomGenerator.nextInt(4) + 1;
    int ranNUmOfClicks = randomGenerator.nextInt(50) + 25;

    // Create and initialize a title label and status label
    private JLabel jlblTitle = new JLabel();
    private JLabel jlblStatus = new JLabel();

    // Input and output streams from/to server
    private Scanner fromServer;
    private PrintWriter toServer;

    // Continue to play?
    private boolean continueToPlay = true;

    // Indicate if it runs as application
    private boolean isStandAlone = true;    

    // Host name or ip
    private String host = "localhost";

    /** Initialize UI */
    public void init() 
    {
        // Panel p to hold game
        JPanel p = new JPanel();
        // Set properties for labels and borders for labels and panel
        p.setBorder(new LineBorder(Color.black, 1));
        jlblTitle.setHorizontalAlignment(JLabel.CENTER);
        jlblTitle.setFont(new Font("SansSerif", Font.BOLD, 16));
        jlblTitle.setBorder(new LineBorder(Color.black, 1));
        jlblStatus.setBorder(new LineBorder(Color.black, 1));
        // Place the panel and the labels to the applet
        add(jlblTitle, BorderLayout.NORTH);
        add(jlblStatus, BorderLayout.SOUTH);
        add(MainClick, BorderLayout.CENTER);
        add(Advantage, BorderLayout.EAST);
        add(Disadvantage, BorderLayout.WEST);
        MainClick.setPreferredSize(new Dimension(100,100));
        Advantage.setPreferredSize(new Dimension(300,100));
        Disadvantage.setPreferredSize(new Dimension(300,100));

        // Connect to the server
        connectToServer();
    }
    private void connectToServer() 
    {
        try 
        {
          // Create a socket to connect to the server
          Socket socket;
          if (isStandAlone)
            socket = new Socket(host, 8888);
          else
            socket = new Socket(getCodeBase().getHost(), 8000);

          // Create an input stream to receive data from the server
          fromServer = new Scanner(socket.getInputStream());

          // Create an output stream to send data to the server
          toServer = new PrintWriter(socket.getOutputStream());
        }
        catch (Exception ex) 
        {
          System.err.println(ex);
        }

        // Control the game on a separate thread
        Thread thread = new Thread(this);
        thread.start();
    }

    public void run() 
      {
        try 
        {
          // Get notification from the server
          int player = fromServer.nextInt();
          // Am I player 1 or 2?
          if (player == 1) 
          {
            jlblTitle.setText("Player 1 click for your life!" + " Your Clicks: " + player1Amount);
            jlblStatus.setText("Waiting for player 2 to join.");
            // Receive startup notification from the server
            fromServer.nextInt(); // Whatever read is ignored
            // The other player has joined
            jlblStatus.setText("Player 2 has joined. Now Click!");
            MainClick.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event) 
                {
                    //String clicked = event.toString();
                    if(multiclicksP1 == true)
                    {
                        //System.out.println("I'm on!"); //Debug
                        player1Amount = player1Amount + 2;
                        jlblTitle.setText("Player 1 click for your life!" + " Your Clicks: " + player1Amount);
                    }
                    else
                    player1Amount++;
                    jlblTitle.setText("Player 1 click for your life!" + " Your Clicks: " + player1Amount);
                    //System.out.println(player1Amount); //Debug
                }
            }); 
            Advantage.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event) 
                {
                    System.out.println("Button Clicked");
                    //System.out.println("Starting random number: " + randomNum1);
                    if(player1Amount >= 25)
                        {
                            randomNum1 = randomGenerator.nextInt(4) + 1;
                            //System.out.println("New random number before choosing: " + randomNum1);
                            player1Amount = player1Amount - 25;
                            if(randomNum1 == 1)
                            {                   
                                multiclicksP1 = true;
                                jlblStatus.setText("Clicks are now worth 2 clicks!");
                                jlblTitle.setText("Player 1 click for your life!" + " Your Clicks: " + player1Amount);
                                randomNum1 = randomGenerator.nextInt(4) + 1;
                                //System.out.println("New random number after choosing: " + randomNum1);
                            }
                            if(randomNum1 == 2)
                            {
                                jlblStatus.setText(ranNUmOfClicks + " Clicks were added!");
                                jlblTitle.setText("Player 1 click for your life!" + " Your Clicks: " + player1Amount);
                                player1Amount = player1Amount + ranNUmOfClicks;
                                ranNUmOfClicks = randomGenerator.nextInt(25) + 25;
                                //System.out.println("New random number after adding: " + ranNUmOfClicks);
                            }
                            if(randomNum1 == 3)
                            {
                                jlblStatus.setText("Your opponents clicks wil now slightly help you for a limited time!");
                                player1TwoForOne = true;
                            }
                            if(randomNum1 == 4)
                            {

                            }
                        }
                        else 
                        {
                            jlblStatus.setText("You don't have 25 clicks to spend!");
                        }
                    }
            });
            Disadvantage.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent event) 
                {
                    System.out.println("Button Clicked");
                    if(player1Amount >= 25)
                    {

                    }
                    else 
                    {
                        jlblStatus.setText("You don't have 25 clicks to spend!");
                    }
                }
            }); 
            }           
          else if (player == 2) 
          {
            jlblTitle.setText("Player 2 click for your life!" + " Your Clicks: " + player2Amount);
            jlblStatus.setText("Now Click!");
            JButton MainClick = new JButton("Spam to win!");
            add(MainClick, BorderLayout.CENTER);
            MainClick.setPreferredSize(new Dimension(100,100));
            JButton Advantage = new JButton("Spend 25 " + "clicks for " + "advantages");
            add(Advantage, BorderLayout.EAST);
            Advantage.setPreferredSize(new Dimension(300,100));
            JButton Disadvantage = new JButton("Spend 25 " + "clicks to " + "sabotage " + "your " + "opponent");
            add(Disadvantage, BorderLayout.WEST);
            Disadvantage.setPreferredSize(new Dimension(300,100));
            // Receive startup notification from the server
            fromServer.nextInt(); // Whatever read is ignored
            // The other player has joined    
            /*if(countDown == true)
            {
                for(int time = 8; time >= 0; time--)
                {
                    try {
                        timer.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println(time);
                    if(time == 0)
                    {
                        player1TwoForOne = false;
                    }
                }
            }*/
            MainClick.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent event) 
                    {
                        if(player1TwoForOne = true)
                        {
                            //countDown = true;
                            player1TwoForOneCount++;
                            System.out.println(player1TwoForOneCount);
                            if(player1TwoForOneCount == 2)
                            {
                                player1Amount = player1Amount + 1;
                                player1TwoForOneCount = 0;
                            }
                            if (time == 0)
                            {
                                countDown = false;
                            }
                        }

                        if(multiclicksP2 == true)
                        {
                            //System.out.println("I'm on!"); //Debug
                            player2Amount = player2Amount + 2;
                            jlblTitle.setText("Player 2 click for your life!" + " Your Clicks: " + player2Amount);
                        }
                        else
                        player2Amount++;
                        jlblTitle.setText("Player 2 click for your life!" + " Your Clicks: " + player2Amount);
                    }
                }); 
                Advantage.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent event) 
                    {
                        //System.out.println("Starting random number: " + randomNum2);
                        if(player2Amount >= 25)
                        {
                            randomNum2 = randomGenerator.nextInt(4) + 1;
                            //System.out.println("New random number before choosing: " + randomNum2);
                            player2Amount = player2Amount - 25;
                            if(randomNum2 == 1)
                            {
                                multiclicksP2 = true;
                                jlblStatus.setText("Clicks are now worth 2 clicks!");
                                jlblTitle.setText("Player 2 click for your life!" + " Your Clicks: " + player2Amount);
                                randomNum2 = randomGenerator.nextInt(4) + 1;
                                //System.out.println("New random number after choosing: " + randomNum2);
                            }
                        }
                        else 
                        {
                            jlblStatus.setText("You don't have 25 clicks to spend!");
                        }
                    }
                }); 
                Disadvantage.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent event) 
                    {
                        if(player2Amount >= 25)
                        {

                        }
                        else 
                        {
                            jlblStatus.setText("You don't have 25 clicks to spend!");
                        }
                    }
                });             
          }

          // Continue to play
          while (continueToPlay) 
          {
            int status = fromServer.nextInt();
            continueToPlay = false;
            if (player == 1) 
            {
                if(player1Amount >= 200)
                {
                    toServer.write(1);
                }
            }
            else if (player == 2) 
            {
                if(player2Amount >= 200)
                {
                    toServer.write(2);
                }
            }
            //else if (status == PLAYER1_WON)
            {
                if(player == 1)
                {
                    jlblTitle.setText("Congrats Player 1 you've out click Player 2!");
                }
                if(player == 2)
                {
                    jlblTitle.setText("Player 1 has out clicked you Player 2!");
                }       
            }
            //else if (status == PLAYER2_WON)
            {
                if(player == 1)
                {
                    jlblTitle.setText("Player 2 has out clicked you Player 1!");
                }
                if(player == 2)
                {
                    jlblTitle.setText("Congrats Player 2 you've out click Player 1!");
                }       
            }
          }
        }
        catch (Exception ex) {}
      }
      /** This main method enables the applet to run as an application */
      public static void main(String[] args) 
      {
        // Create a frame
        JFrame frame = new JFrame("Client");

        // Create an instance of the applet
        Client applet = new Client();
        applet.isStandAlone = true;

        // Get host
        if (args.length == 1) applet.host = args[0];

        // Add the applet instance to the frame
        frame.getContentPane().add(applet, BorderLayout.CENTER);

        // Invoke init() and start()
        applet.init();
        applet.start();

        // Display the frame
        frame.setSize(800, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      }
}