我正在尝试将输出放在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);
}}
答案 0 :(得分:3)
您已经写过o1一次,并且只从线程中获取默认toString()
,所以我不会感到惊讶您除了垃圾之外什么都没看到。我的建议是:
SwingWorker<Void, String>
publish(...)
,传入字符串,发布GUI所需的任何字符串。process(...)
方法在GUI中显示它们。setBounds()
。虽然null布局和setBounds()
似乎是Swing新手,比如创建复杂GUI的最简单和最好的方法,但是你创建的Swing GUI越多,你在使用它们时会遇到更严重的困难。 。当GUI调整大小时,他们不会调整组件的大小,他们是增强或维护的皇室女巫,当他们放置在滚动窗格中时,他们完全失败,当他们在所有平台或屏幕分辨率不同时看起来很糟糕原来的。而是学习并使用布局managaers。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);
}