为什么这个JButton在被按下后没有响应?

时间:2016-06-04 04:26:50

标签: java swing awt

我处于通过套接字处理服务器和客户端连接的早期阶段。在我的“ServerMain”类中,我给服务器一个端口来通过JTextField运行,JTextField通过JButton上的ActionListener输入它。只要按下按钮,它就会变灰,并且它下面的JTextArea会停止附加文本。我需要进行哪些更改才能使按钮和textarea在服务器启动后继续工作?

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);
  }
}

1 个答案:

答案 0 :(得分:1)

在按钮的操作中,您基本上打开服务器套接字并等待某些通信。当您在UI所在的同一个线程中执行此操作时,您将阻止进度,并且无法与UI进行进一步的交互。

您需要启动一个不同的线程并进入UI线程之间的线程间通信(在Java中称为AWT事件处理程序线程,如果您想查找它)以及您为通信启动的任何线程