簡體   English   中英

為什么按下此JButton后沒有響應?

[英]Why is this JButton not responding after being pressed?

我處於通過套接字處理服務器和客戶端連接的早期階段。 在我的“ ServerMain”類中,我為服務器提供了一個端口,該端口可通過JTextField運行,該端口通過JButton上的ActionListener進入該端口。 一旦按下按鈕,它就會變灰,並且它下面的JTextArea會停止添加文本。 我需要進行哪些更改,以便服務器啟動后按鈕和文本區域都能繼續工作?

ServerMain:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class ServerMain extends JFrame{

  GraphicPanel m;
  final static String newLine = "\n";
  public static void main(String[] args) {

    ServerMain s = new ServerMain("Testing Server");
    s.setUpUI();
    s.pack();
    s.setVisible(true);
  }

  private void setUpUI() {
      m = new GraphicPanel(this);
      m.initialize();
      this.add(m);
      this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
  }
  public void hostPort(String port){
    System.out.println("Running hostPort method");
    int p=-1;
    try{
         p= Integer.parseInt(port);
    }
    catch(Exception e){
        JOptionPane.showInternalMessageDialog(this, "Must enter a number(0 - 65500) for the port");

    }
    m.print(""+p);
    m.print("Running Try.");
    try {
        System.out.println("About to create socket");
        @SuppressWarnings("resource")

        ServerSocket serverSocket = new ServerSocket(p);
        m.print("got the socket."+serverSocket);
        Socket[] clientSocket = new Socket[2];
        clientSocket[0] = serverSocket.accept();
        clientSocket[1] = serverSocket.accept();
        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket[0].getInputStream()));
        m.print("Server Socket Initialized.");
        System.out.println("S");
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            m.print(inputLine);
            m.print("Client: "+in.readLine());
        }
    }
    catch (IOException e) {
        m.print("Exception caught when trying to listen on port "
                + port + " or listening for a connection "+newLine+e+newLine);
        m.print(e.getMessage());
    }

}

  public ServerMain(String s){
      super(s);
  }

}

最后是GraphicPanel:

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class GraphicPanel extends JPanel{
  private String ip = "";
  private String port = "";
  private JTextField txt_port = new javax.swing.JTextField(20);
  private JTextArea txa_info;
  private JButton but_conDC;
  private ServerMain s;
  final static String newLine = "\n";
  public GraphicPanel(ServerMain serv){
      this.s = serv;
      this.setPreferredSize(new Dimension(800,200));
  }

  public void initialize(){

      txa_info = new JTextArea(10,70);
      but_conDC = new JButton("Connect");
      txt_port.setLocation(100, 100);

      add(txt_port);
      but_conDC.addActionListener(new ActionListener(){

          @Override
          public void actionPerformed(ActionEvent e) {
              port = txt_port.getText();
              System.out.println(port + " was in the text box");
              if(port.equals("")){
                  emptyPort();
                  return;
              }
              but_conDC.setText("Disconnect");
              repaint();
              txa_info.setText("Hosting on port "+port);
              s.hostPort(port);
          }
      });
      but_conDC.setSize(5, 5);
      add(but_conDC);
      txa_info.setSize(800, 100);
      txa_info.setEditable(false);

      add(txa_info);
  }

  public void print(String s){
      txa_info.append(s);
  }

  public void emptyPort() {
      txa_info.append("ERROR: Attempted to host with no port! Try entering something like 4444"+newLine);
  }
}

在操作按鈕時,基本上是打開服務器套接字並等待一些通信。 當您在與UI相同的線程中執行此操作時,您將踩到踏板,並無法與UI進行進一步的交互。

您將需要啟動一個不同的線程,並進入UI線程(在Java中稱為AWT事件處理程序線程,以防您要查找它)與為通信而啟動的任何線程之間進行線程間通信。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM