我正在制作一个GUI应用程序,在点击按钮时我们读取二进制文件并在TextArea中显示数据(数据为327680字节)。
现在,如果我使用textArea.setLine(true)
,那么它会减慢执行速度,执行和释放值需要大约1分钟。
但如果我不使用textArea.setLine(true)
,那么代码会在不到1秒的时间内执行。
更新后的代码。
public class App{
private static final String INPUT_FILE = "E:\\arun\\files\\Capture_Mod1.BIN";
private static final String OUTPUT_FILE = "E:\\arun\\files\\ISO_Write.BIN";
private static final String IMAGE_FILE = "E:\\arun\\files\\Image.jpg";
private JFrame frame;
private JTextArea textArea;
private JButton btnNewButton;
private ISOImageDataWorker worker;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
App window = new App();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public App() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0};
gridBagLayout.rowHeights = new int[]{0, 0, 0, 0};
gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
frame.getContentPane().setLayout(gridBagLayout);
textArea = new JTextArea(5, 20); // **HERE IS THE PROBLEM**
textArea.setLineWrap(true);
textArea.setWrapStyleWord( true );
JScrollPane scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
GridBagConstraints gbc_textArea = new GridBagConstraints();
gbc_textArea.insets = new Insets(0, 0, 5, 0);
gbc_textArea.gridx = 0;
gbc_textArea.gridy = 1;
frame.getContentPane().add(scrollPane, gbc_textArea);
btnNewButton = new JButton("ISO 19794 Data");
GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
gbc_btnNewButton.gridx = 0;
gbc_btnNewButton.gridy = 2;
frame.getContentPane().add(btnNewButton, gbc_btnNewButton);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
worker = new ISOImageDataWorker();
worker.execute();
}
});
}
private class ISOImageDataWorker extends SwingWorker<String , Object>{
@Override
protected String doInBackground() throws Exception {
String str = processData();
return str;
}
@Override
protected void done(){
try {
System.out.println("cm1");
textArea.setText(get());
System.out.println("cm2");
} catch (Exception e) {
e.printStackTrace();
}
}
}
public String processData(){
DataReadWrite data = new DataReadWrite();
//Read data
byte[] imageData = data.readData(INPUT_FILE);
System.out.println("Image data length is : "+ imageData.length);
// Generate Finger Image Data General Header
FingerImageDataGeneralHeader generateHeader = new FingerImageDataGeneralHeader();
byte[] resultData = generateHeader.createGeneralHeader(imageData);
// Write data to binary file
data.writeData(resultData, OUTPUT_FILE);
// Create Image from binary data
CreateTiff createTiff = new CreateTiff();
createTiff.create(resultData, IMAGE_FILE);
String str = bytesToHex(resultData);
return str;
}
public static String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02X ", b));
}
return sb.toString();
}
}
我不知道我做错了什么。请帮助。
答案 0 :(得分:3)
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
>> processData(); <<
textArea.setText(str);
}
});
不要在GUI线程(EDT)上执行耗时的代码。使用SwingWorker。