gpt4 book ai didi

java - 线程输出到 GUI 文本字段

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:43:39 25 4
gpt4 key购买 nike

我试图在 GUI 的 TextField 中输出,但我得到的只是线程信息。这只是完整代码的一小部分,但完整版本也有同样的问题。完整版有 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()来自线程,所以我对你看到的只是垃圾并不感到惊讶。我的建议:

  • 创建 SwingWorker<Void, String>在你的 GUI 里面
  • 从 SwingWorker 的 doInBackground 中运行长时间运行的代码
  • 通过调用 publish(...) 发布 GUI 需要的任何字符串,传入字符串。
  • 使用 SwingWorker 的 process(...) 在 GUI 中显示它们方法。
  • 不要使用静态变量作为线程间通信的障碍。这是一个非常容易破解的非解决方案。
  • 避免调用 setBounds()在 Swing GUI 中。而空布局和 setBounds()对于 Swing 新手来说,这似乎是创建复杂 GUI 的最简单和最好的方法,您创建的 Swing GUI 越多,使用它们时就会遇到越严重的困难。当 GUI 调整大小时,它们不会调整组件的大小,它们是增强或维护的皇家女巫,当放置在滚动 Pane 中时它们会完全失败,当在所有平台或与原始屏幕分辨率不同的屏幕分辨率上查看时,它们看起来很糟糕.而是学习和使用布局管理器。
  • 查看:Tutorial: Concurrency in Swing .

例如类似的东西,

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 - 线程输出到 GUI 文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30204521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com