JFrame变成空白并“挂起”

时间:2016-05-20 15:22:31

标签: java swing user-interface jframe

在调用StartGameButtonListener之后调用newWaitScreen方法时,JFrame将为白色并且“挂起”,但是如果在StartGameButtonListener之前调用newWaitScreen它成功显示。有人能帮助我吗?

提前致谢并对这个烂摊子感到抱歉,这是我第一次发帖。我的代码如下:

public void serverMenu () 
{
    frame = new JFrame ("Game Server");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    String [] noOfPlayer = {"1", "2", "3"};

    JPanel panel = new JPanel (); 

    JLabel port = new JLabel ("Port");
    portNo = new JTextField (15);

    JLabel players = new JLabel ("No. of Player(s):");
    playerNo = new JComboBox (noOfPlayer);

    JButton startGame = new JButton ("Start Game Server");
    startGame.addActionListener (new StartGameButtonListener());

    panel.add (port);
    panel.add (portNo);
    panel.add (players); 
    panel.add (playerNo); 
    panel.add (startGame);

    frame.add (panel); 
    frame.setPreferredSize (new Dimension (275,200));
    frame.pack (); 
    frame.setVisible (true); 
    //newWaitScreen (444, 2);  //if newWaitScreen is called here, the screen will be shown with no issue

}

private class StartGameButtonListener implements ActionListener 
{
    public void actionPerformed (ActionEvent e) 
    {
        String portNum = portNo.getText(); 

        String playerNum = (String) playerNo.getSelectedItem(); 

        startGameCallBack (portNum, playerNum);
    }
}

public void startGameCallBack (String portNum, String playerNum)
{
    frame.setVisible (false); 

    int port ; 
    int noPlayer; 

    port = Integer.parseInt (portNum); 

    noPlayer = Integer.parseInt (playerNum);

    waitGame (port, noPlayer); 
}

public void waitGame (int portNum, int noOfPlayer)
{ 
    newWaitScreen (portNum, noOfPlayer);  //if newWaitScreen called JFrame becomes white and "hang"

    ServerSocket serverSocket = null; 

    try 
    {
        serverSocket = new ServerSocket (portNum);
    }

    catch (IOException e)
    {
        System.out.println ("Port " + portNum +" not avaliable");
        System.exit (0); 
    }

    for (int i = 1 ; i <= noOfPlayer; i++)
    {
        Socket clientSocket = null; 

        try 
        {
            System.out.println ("Waiting for client connection");
            clientSocket = serverSocket.accept(); 
            System.out.println ("Client connected");
        }

        catch (IOException e)
        {
            System.out.println ("Client unable to connect");
            System.exit (0); 
        }

        System.out.println (i);


        /*ClientHandler c = new ClientHandler (clientSocket, serverCounter); 
        serverCounter++;

        if (serverCounter >= noOfPlayer)
            break;*/
    }
}

public void newWaitScreen (int portNum, int noOfPlayer)
{
    frame = new JFrame ("Game Server");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    bigPanel = new JPanel ();
    bigPanel.setPreferredSize (new Dimension (200, 300));

    JLabel label = new JLabel ("Listening to port: " + portNum);
    JPanel portLabel = new JPanel ();
    portLabel.add (label);
    bigPanel.add (portLabel); 

    JPanel panel1 = new JPanel (); 
    panel1.setPreferredSize (new Dimension (200, 30));

    player1 = new JLabel ("Player 1");
    connection1 = new JLabel ("Waiting for connection...");
    panel1.add (player1);
    panel1.add (connection1);
    bigPanel.add (panel1); 

    if (noOfPlayer > 1)
    {
        player2 = new JLabel ("Player 2");
        connection2 = new JLabel ("Waiting for connection...");

        JPanel panel2 = new JPanel (); 
        panel2.setPreferredSize (new Dimension (200, 30));
        panel2.add (player2);
        panel2.add (connection2); 

        bigPanel.add (panel2); 
    }

    if (noOfPlayer > 2)
    {
        player3 = new JLabel ("Player 3");
        connection3 = new JLabel ("Waiting for connection...");
        JPanel panel3 = new JPanel (); 
        panel3.setPreferredSize (new Dimension (200, 30));
        panel3.add (player3);
        panel3.add (connection3); 

        bigPanel.add (panel3); 
    }

   frame.add (bigPanel);
   bigPanel.setVisible (true); 

   frame.setPreferredSize (new Dimension (275,300));
   frame.pack(); 
   frame.setVisible (true); 

   frame.repaint(); 
   frame.revalidate(); 

}

1 个答案:

答案 0 :(得分:1)

您正在使用UI线程侦听ServerSocket上的连接。您需要创建一个新的线程来处理这个