简体   繁体   English

带套接字的Java GUI多线程

[英]java GUI multi-threading with socket

I am trying to make a GUI program which get data from TCP socket. 我正在尝试制作一个从TCP套接字获取数据的GUI程序。 The problem is when I set up the socket, and then start it, GUI program freeze. 问题是当我设置套接字,然后启动它时,GUI程序冻结。 I want add one more function that stop thread by using click event. 我想添加一个通过使用click事件停止线程的功能。

As I know I have to use EDT, I tried but doesn't work. 据我所知,我必须使用EDT,但我尝试了但没用。 Is there anyone who can help me? 有谁可以帮助我吗?

Here's my code. 这是我的代码。

  1. main 主要

     public class ImageSplit { static ImageViewerFrame program; public static void main(String[] args) throws IOException { JPanel[] displayArray = new JPanel[100]; program = new ImageViewerFrame(); EventQueue.invokeLater(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub program.setVisible(true); } }); } 
  2. frame

     void createMouseMenu() { final PopupMenu menu = new PopupMenu(); MenuItem grid = new MenuItem("Grid"); MenuItem start = new MenuItem("Start"); MenuItem stop = new MenuItem("Stop"); menu.add(grid); menu.add(start); menu.add(stop); add(menu); mouseMenuHandler mousemenu = new mouseMenuHandler(); grid.addActionListener(mousemenu); start.addActionListener(mousemenu); stop.addActionListener(mousemenu); addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { if (e.getModifiers() == InputEvent.BUTTON3_MASK) menu.show(e.getComponent(), e.getX(), e.getY()); } }); } class mouseMenuHandler implements ActionListener { getInfo info; public void actionPerformed(ActionEvent e) {// open grid setting if (e.getActionCommand().equals("Grid")) { gridSettingUI = new GridSetting(img); gridSettingUI.setVisible(true); } else if (e.getActionCommand().equals("Start")) { try { info = new getInfo(40000);//open socket info.run(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else if (e.getActionCommand().equals("Stop")) { System.out.println("stop"); info.stop(); } } } 
  3. socket Receiving 套接字接收

     import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class getInfo implements Runnable{ ServerSocket serverSocket; private boolean stopped=false; public getInfo(int port_num) throws IOException{ serverSocket = new ServerSocket(port_num); } @Override public void run() { // TODO Auto-generated method stub while(!stopped){ try { Socket socket = serverSocket.accept(); System.out.println(socket.getInetAddress()+ "connected"); InputStream is = socket.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); if(br.ready()) { String line = br.readLine(); System.out.println(line+"\\n"); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void stop(){ stopped = true; } 

} }

info = new getInfo(40000);//open socket
info.run();

In the lines above, you are NOT starting a thread, instead, you are just executing the run() method in the same thread (as it was any other method call). 在上面的几行中,您不是在启动线程,而是在同一线程中执行run()方法(就像其他任何方法调用一样)。

Extend from Thread , then call info.start() . Thread扩展,然后调用info.start()

See: Java Puzzlers: Traps, Pitfalls, and Corner Cases by Joshua Bloch and Neal Gafter, Puzzle #76 . 请参阅: Java拼图游戏:陷阱,陷阱和极端案例 ,作者:Joshua Bloch和Neal Gafter, 难题#76

I wrote a library called Jexxus which lets you use sockets more easily. 我写了一个叫Jexxus的库,它使您可以更轻松地使用套接字。 It will automatically do things on a different thread so your UI won't freeze. 它将自动在其他线程上执行操作,因此您的UI不会冻结。 There's a little tutorial at the bottom of the page here: https://github.com/mirraj2/Jexxus/tree/master 在页面底部,这里有一个小教程: https : //github.com/mirraj2/Jexxus/tree/master

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM