Gui在循环中冻结了

时间:2011-11-13 09:38:29

标签: java swing user-interface network-programming concurrency

server.java

好的,所以问题是:当我点击开始按钮时,gui“冻结”,但是服务器正在工作,我在控制台中有一个输出。我想我应该使用线程,但我不知道如何实现它= /

void Runer() throws Exception
{
    ServerSocket welcomeSocket = new ServerSocket(6789);
    System.out.println("welcome into server");
    while(true)
    {

        Socket connectionSocket = welcomeSocket.accept();
        if (connectionSocket != null)
        {
            System.out.println(connectionSocket);
            Client client = new Client(connectionSocket);
            client.start();
        }
    }
}

}

class Client extends Thread
{
private Socket connectionSocket;
private String pozX;
private String ans;
private String pozY;
private String capitalizedSentence;
private BufferedReader inFromClient;
private DataOutputStream outToClient;

public Client(Socket c) throws IOException
{
    connectionSocket = c;
}

public void run() 
{
    try
    {    
        inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
        outToClient = new DataOutputStream(connectionSocket.getOutputStream());

        pozX = inFromClient.readLine();
        pozY = inFromClient.readLine();
        System.out.println(pozX);
        System.out.println(pozY);
        capitalizedSentence = pozX.toUpperCase() + '\n';
          BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));

       outToClient.writeBytes(capitalizedSentence);

    }
    catch(IOException e)
    {
        System.out.println("Errore: " + e);
    }
}

gui.java

JPanel GuiServera(){
    JPanel panel = new JPanel();
    area = new JTextArea();
    area.setColumns(20);
    area.setRows(10);
    area.setText("The log is logging injoy");
    panel.setLayout(null);
    start = new JButton("Start");
    start.addActionListener(list);
    panel.add(start);
    stop = new JButton("Stop");
    stop.addActionListener(list);
    pane = new JButton("Admin Panel");
    panel.add(stop);

    panel.add(pane);
     scrolled = new JScrollPane(area);
     panel.add(scrolled);
    Insets insets = panel.getInsets();
    Dimension size = start.getPreferredSize();
    start.setBounds(40 + insets.left, 20 + insets.top,
    size.width, size.height);

    size = stop.getPreferredSize();
    stop.setBounds(40 + insets.left, 60 + insets.top,
    size.width, size.height);

    size = pane.getPreferredSize();
    pane.setBounds(40 + insets.left, 100 + insets.top,
    size.width, size.height);

    size = scrolled.getPreferredSize();
    scrolled.setBounds(160 + insets.left, 20 + insets.top,
    size.width, size.height);
    return panel;
}
class Nasluchiwacz implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==start){
            System.out.println("start");
            area.append("start button\n");
            try {
                inzServer.Runer();
            } catch (Exception ex) {
                Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
        else if (e.getSource()==stop){
            area.setText(area.getText()+"\nstop\n");
        }
    }

}

2 个答案:

答案 0 :(得分:4)

你差不多正确了。 而不是跑步者扩展线程。让它实现runnable。

然后在你的guiclass。你可以要么新的Thread(inserver).start(); 或者看看Executorservice或Swingworkers。

答案 1 :(得分:0)

你应该尝试使用swingWorker。 检查此Tutorial of sun