是否可以选择重新绘制SWT中的所有窗口?我有900个组合框的shell,我必须在循环(100x)中重新绘制此窗口的所有内容,并计算它是多少微秒。
如何创建循环以及重绘的计数时间并不重要。
我尝试了 redraw(),setRedraw(true),布局以及来自少数论坛的类似建议,但我认为它不起作用。
当我使用 window.invalidate()时,我需要像在Swing中一样的功能; window.validate(); window.repaint();
public class SwtCombobox {
private static final int numberOFTestingLoops = 100;
protected Shell shell;
private Combo[] comboboxes;
private List<Long> resultOfAnalysisRepaint = new ArrayList<Long>();
private List<Double> resultOfAnalysisMemory = new ArrayList<Double>();
private int counterOfAnalysis = 0;
public static void main(String[] args) {
try {
SwtCombobox window = new SwtCombobox();
window.open(window);
} catch (Exception e) {
e.printStackTrace();
}
}
public void open(SwtCombobox window) {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
shell.addPaintListener(new MyListener(window));
while (!shell.isDisposed()) {
if (counterOfAnalysis < numberOFTestingLoops) {
counterOfAnalysis++;
shell.redraw();
} else if (counterOfAnalysis == numberOFTestingLoops) {
createOutputFile();
shell.setRedraw(false);
MessageBox messageBox = new MessageBox(shell,
SWT.ICON_INFORMATION);
messageBox.setMessage("Analysis file created.");
messageBox.open();
System.exit(0);
}
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
protected void createContents() {
shell = new Shell();
shell.setSize(1550, 670);
comboboxes = new Combo[900];
int counter = 0;
for (int i = 0; i < 30; i++) {
for (int y = 0; y < 30; y++) {
counter++;
String options[] = { Integer.toString(counter), "test1",
"test2" };
comboboxes[i] = new Combo(shell, SWT.BORDER);
comboboxes[i].setItems(options);
comboboxes[i].select(0);
comboboxes[i].setBounds(y * 51, i * 21, 51, 21);
}
}
}
public void repaint() {
long solStart = System.currentTimeMillis();
long solTime = System.currentTimeMillis() - solStart;
resultOfAnalysisRepaint.add(solTime);
double currentMemory = ((double) ((double) (Runtime.getRuntime().totalMemory() / 1024) / 1024)) - ((double) ((double) (Runtime.getRuntime().freeMemory() / 1024) / 1024));
resultOfAnalysisMemory.add(currentMemory);
}
private void createOutputFile() {
String currentDir = System.getProperty("user.dir");
generateCsvFile(currentDir + "\\outputSwtCombobox.csv",
resultOfAnalysisRepaint, resultOfAnalysisMemory);
}
private static void generateCsvFile(String sFileName,
List<Long> resultsRepaint, List<Double> resultsMemory) {
try {
FileWriter writer = new FileWriter(sFileName);
Long averageRepaint = 0L;
Double averageMemory = 0.0;
writer.append("RepaintTime");
writer.append(';');
writer.append("MemoryUsage");
writer.append('\n');
for (int i = 0; i < resultsRepaint.size(); i++) {
writer.append(resultsRepaint.get(i).toString());
writer.append(';');
writer.append(resultsMemory.get(i).toString());
writer.append('\n');
averageRepaint = averageRepaint + resultsRepaint.get(i);
averageMemory = averageMemory + resultsMemory.get(i);
}
writer.append('\n');
writer.append("Average (microseconds):");
writer.append(';');
writer.append("Average (MB):");
writer.append('\n');
writer.append(String
.valueOf((averageRepaint / numberOFTestingLoops)));
writer.append(';');
writer.append(String
.valueOf((averageMemory / numberOFTestingLoops)));
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class MyListener实现了PaintListener {
private SwtCombobox swtCombobox;
public MyListener(SwtCombobox swtCombobox) {
super();
this.swtCombobox = swtCombobox;
}
public void paintControl(PaintEvent e) {
swtCombobox.repaint();
}
}