【发布时间】:2018-06-21 12:17:40
【问题描述】:
我尝试了许多在线提供的解决方案,但仍然无法正常工作。 我正在创建一个 JAR,希望文本可以在 JTextArea 中实时更新。 但是文本只有在程序完成后才会附加到文本区域。
我的用户界面:
package Test;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import Test.SearchForWorker;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import java.awt.SystemColor;
import java.awt.Font;
public class TestTesr {
private JFrame frame;
private JTextField textField;
private JTextArea console;
private int input;
public int getInput() {
return input;
}
public void setInput(int input) {
this.input = input;
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestTesr window = new TestTesr();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public TestTesr() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 537, 360);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 233, 338);
frame.getContentPane().add(panel);
panel.setLayout(null);
textField = new JTextField();
textField.setBounds(5, 6, 130, 26);
panel.add(textField);
textField.setColumns(10);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(233, 6, 298, 326);
frame.getContentPane().add(scrollPane);
console = new JTextArea();
console.setEditable(false);
scrollPane.setViewportView(console);
JButton btnNewButton = new JButton("Submit");
btnNewButton.setBounds(140, 5, 88, 29);
panel.add(btnNewButton);
JLabel show = new JLabel("");
show.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
show.setForeground(SystemColor.textHighlight);
show.setBounds(49, 115, 100, 53);
panel.add(show);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.print("Data Submitted");
new SearchForWorker("Data Submitted", console).execute();
;
String y = textField.getText();
int g = Integer.parseInt(y);
setInput(g);
for (int u = 1; u <= 10; u++) {
try {
Thread.sleep(3000);
} catch (Exception q) {
System.out.println("Error occur for program time break" + q.toString());
}
g++;
new SearchForWorker("Inout plus " + u + " is :" + g, console).execute();
;
}
}
});
}
}
SwingWorker:
package Test;
import java.awt.Color;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
public class SearchForWorker extends SwingWorker<String, String> {
private final String display;
private final JTextArea messageTextArea;
TestTesr test = new TestTesr();
SearchForWorker(final String output, final JTextArea area) {
// initialize
this.display = output;
this.messageTextArea = area;
}
@Override
protected String doInBackground() throws InterruptedException {
System.out.println("Inside do in background for: "+display);
publish(display);
return display;
}
protected void done() {
}
@Override
protected void process(List<String> showText){
System.out.println("ShowText is: "+showText.toString());
messageTextArea.append(showText.toString());
messageTextArea.append("\n");
}
}
【问题讨论】:
-
ActionListener中的Thread.sleep(3000);正在阻塞事件调度线程,该线程正在停止更新 UI。此功能属于您的SwingWorker的doInBackgrond方法 -
或者更好的是,根本不要调用 sleep()。这简直是在浪费时间。
-
虽然我删除了 sleep(),但 Teaxtarea 中的文本并没有实时更新。
-
删除
sleep后,messageTextArea 会在您按下btnNewButton后立即更新
标签: java swing user-interface updates swingworker