Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 线程输出到GUI文本字段_Java_Multithreading_Swing_User Interface - Fatal编程技术网

Java 线程输出到GUI文本字段

Java 线程输出到GUI文本字段,java,multithreading,swing,user-interface,Java,Multithreading,Swing,User Interface,我试图在GUI的文本字段中输出,但我得到的只是线程信息。这只是完整代码的一小部分,但是完整版本也有同样的问题。完整版本有5个不同的线程同时运行。任何帮助或建议都将不胜感激 public class O21 implements Runnable { @Override public void run() { try { Scanner O1 = new Scanner(new File("O21.txt")); O1.useDelimiter(",");

我试图在GUI的文本字段中输出,但我得到的只是线程信息。这只是完整代码的一小部分,但是完整版本也有同样的问题。完整版本有5个不同的线程同时运行。任何帮助或建议都将不胜感激

public class O21 implements Runnable {
@Override
public void run() {

    try {
        Scanner O1 = new Scanner(new File("O21.txt"));
        O1.useDelimiter(",");
        while (O1.hasNext()) {
            String a = O1.next();
            int aa = Integer.parseInt(a);
            Thread.sleep(500); // Time delay to sync output
            if (a.trim().isEmpty()) {
                continue;
            }
            System.out.println(a);
        }
    } catch (Exception f) {
        f.printStackTrace();
    }}}
这是主要问题

public class Window {
    private JFrame frmTest;
    private JTextField txtTank1;
    private JTextField textField_4;
    static String o1;

/**
 * Launch the application.
 */
public static void main(String[] args) throws Exception {

    Thread a = new Thread(new O21());
    a.start();

    o1= a.toString();

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Window window = new Window();
                window.frmTest.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    });
}

/**
 * Create the application.
 */
public Window() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frmTest = new JFrame();
    frmTest.setAlwaysOnTop(true);
    frmTest.setResizable(false);
    frmTest.setBounds(100, 100, 350, 400);
    frmTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmTest.getContentPane().setLayout(null);

    txtTank1 = new JTextField();
    txtTank1.setText("Tank1");
    txtTank1.setFont(new Font("Tahoma", Font.PLAIN, 20));
    txtTank1.setEditable(false);
    txtTank1.setColumns(10);
    txtTank1.setBounds(10, 60, 150, 50);
    frmTest.getContentPane().add(txtTank1);

    textField_4 = new JTextField();
    textField_4.setEditable(true);
    textField_4.setText(o1);
    textField_4.setFont(new Font("Tahoma", Font.PLAIN, 20));
    textField_4.setColumns(10);
    textField_4.setBounds(170, 60, 150, 50);
    frmTest.getContentPane().add(textField_4);
}}

您正在向o1写入一次,并且只从线程中获取默认的
toString()
,因此,您看到的除了垃圾之外什么都没有,这并不奇怪。我的建议是:

  • 在GUI中创建一个
    SwingWorker
  • 在SwingWorker的后台运行长时间运行的代码
  • 通过调用
    Publish(…)
    ,传入字符串,发布GUI需要的任何字符串
  • 使用SwingWorker的
    过程(…)
    方法在GUI中显示它们
  • 不要将静态变量用作线程间通信的乱码。这是一个非常容易打破的非解决方案
  • 避免在Swing GUI中调用
    setBounds()
    。虽然空布局和
    setBounds()
    可能会像创建复杂GUI的最简单和最好的方式一样吸引新手,但您创建的GUI越多,在使用它们时遇到的困难就越严重。当GUI调整大小时,它们不会调整您的组件的大小,它们是一个需要增强或维护的皇家女巫,当它们放置在滚动窗格中时会完全失败,当在所有平台或屏幕分辨率与原始分辨率不同的情况下查看时,它们看起来非常糟糕。而是学习和使用布局管理器
  • 看看:

e、 g.类似于

import java.io.File;
import java.util.List;
import java.util.Scanner;

import javax.swing.*;

public class SwingThreadingEg extends JPanel implements MyAppendable {
   private JTextArea area = new JTextArea(30, 50);

   public SwingThreadingEg() {
      JScrollPane scrollPane = new JScrollPane(area);
      scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
      add(scrollPane);
   }

   @Override
   public void append(String text) {
      area.append(text);
   }

   private static void createAndShowGui() {
      SwingThreadingEg mainPanel = new SwingThreadingEg();
      MyWorker myWorker = new MyWorker(mainPanel);
      // add a Prop Change listener here to listen for 
      // DONE state then call get() on myWorker
      myWorker.execute();

      JFrame frame = new JFrame("SwingThreadingEg");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class MyWorker extends SwingWorker<Void, String> {
   private MyAppendable myAppendable;

   public MyWorker(MyAppendable myAppendable) {
      this.myAppendable = myAppendable;
   }

   @Override
   protected Void doInBackground() throws Exception {
      try (Scanner O1 = new Scanner(new File("O21.txt"))) {

         O1.useDelimiter(",");
         while (O1.hasNext()) {
            String a = O1.next();
            int aa = Integer.parseInt(a);
            Thread.sleep(500); // Time delay to sync output
            if (a.trim().isEmpty()) {
               continue;
            }
            System.out.println(a);
            publish(a);
         }
      } catch (Exception f) {
         f.printStackTrace();
      }
      return null;
   }

   @Override
   protected void process(List<String> chunks) {
      for (String text : chunks) {
         myAppendable.append(text + "\n");
      }
   }
}

interface MyAppendable {
   public void append(String text);
}
导入java.io.File;
导入java.util.List;
导入java.util.Scanner;
导入javax.swing.*;
公共类SwingThreadingEg扩展JPanel实现MyAppendable{
私人JTextArea=新JTextArea(30,50);
公共SwingThreadingEg(){
JScrollPane scrollPane=新的JScrollPane(区域);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL\u SCROLLBAR\u ALWAYS);
添加(滚动窗格);
}
@凌驾
公共void追加(字符串文本){
区域。追加(文本);
}
私有静态void createAndShowGui(){
SwingThreadingEg主面板=新SwingThreadingEg();
MyWorker MyWorker=新MyWorker(主面板);
//在此处添加道具更改侦听器以侦听
//完成状态,然后对myWorker调用get()
myWorker.execute();
JFrame=新JFrame(“SwingThreadingEg”);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(主面板);
frame.pack();
frame.setLocationByPlatform(真);
frame.setVisible(true);
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
createAndShowGui();
}
});
}
}
类MyWorker扩展了SwingWorker{
私有MyAppendable MyAppendable;
公共MyWorker(MyAppendable MyAppendable){
this.myappendeable=myappendeable;
}
@凌驾
受保护的Void doInBackground()引发异常{
尝试(Scanner O1=新的扫描仪(新文件(“O21.txt”)){
O1.使用分隔符(“,”);
而(O1.hasNext()){
字符串a=O1.next();
int aa=整数.parseInt(a);
Thread.sleep(500);//同步输出的时间延迟
如果(a.trim().isEmpty()){
继续;
}
系统输出打印项次(a);
出版(a);
}
}捕获(例外f){
f、 printStackTrace();
}
返回null;
}
@凌驾
受保护的无效进程(列表块){
for(字符串文本:块){
myappendeable.append(文本+“\n”);
}
}
}
可追加的接口{
公共void追加(字符串文本);
}

Why OP的
window.frmTest.setVisible(true)
不会抛出
NPE
?@PM77-1:不知道。当我看到他在线程上调用
toString()
时,我就放弃了详细查看他的代码!谢谢,这正是我需要的。我无法正确输出字符串。和window.frmTest.setVisible(true);由窗口生成器自动生成。请查看要回答的编辑,包括代码。