我有一個使用Eclipse構建的Java SWT GUI。我正在使用while循環來引用文本文件。 while循環遍歷文本文件的每一行,併爲每行上的特定項目構建一系列組合或文本框。每行代表GUI中的一個可視列,並且根據文本文件中有多少項,GUI將向右移動。爲了簡單起見,我只包含了我想要弄清楚的代碼。例如,假設我有三行在GUI中創建六個組合框(三列兩行)。我想在第二列的第一行上進行更改,以在最底一行(也在第二列)上執行一個監聽器。然而,現在聽衆循環了所有的組合,並對所有三個組合進行了更改,而不僅僅是我想要的組合。我無法弄清楚如何使這項工作。請參閱下面的代碼。我很感激幫助。用於Eclipse環狀組合的Java監聽器SWT
private void buildMultipleSatPulldowns() {
try {
FileReader fr = new FileReader("MultipleSatellites.txt");
BufferedReader br = new BufferedReader(fr);
String line = null;
String[] tempS;
String constellation = null;
String satellite = null;
while ((line = br.readLine()) != null) {
tempS = line.split("~");
constellation = tempS[4];
satellite = tempS[6];
constNameCombo = new Combo(satellitesComposite2, SWT.NONE);
constNameCombo.setToolTipText("Pulldown constellation name");
constNameCombo.setBounds(startX + x2, 71, 125, 28);
constNameCombo.setItems(constNameArray);
constNameCombo.setText(constellation);
constNameCombos.add(constNameCombo);
constNameCombo.addModifyListener(new ModifyListener() { // captures changed combo values
public void modifyText(ModifyEvent arg0) {
setConstellationPD();
}
});
sPullDown(constellation); // builds the satellite array for the constellation and populates each pulldown
satNameCombo = new Combo(satellitesComposite2, SWT.NONE);
satNameCombo.setToolTipText("Pulldown satellite name");
satNameCombo.setBounds(startX + x2, 106, 125, 28);
satNameCombo.setItems(satNameArray);
satNameCombo.setText(satellite);
satNameCombos.add(satNameCombo);
startX = startX + nextX;
}
br.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void setConstellationPD() {
int constellations = 0;
for (Combo constNameCombo : constNameCombos) {
// What do I do here so that only the desired satNameCombo changes to reflect the desired pull down?
setSatellitesPD(constellations, constNameCombo)
constellations++;
}
}
private void setSatellitesPD(int c, String cN) {
int satellites = 0;
for (Combo satNameCombo : satNameCombos) {
if (c == satellites) {
satNameCombo.setText(satNameCombos.get(satellites).toString());
satNameCombo.removeAll();
sPullDown(cN);
satNameCombo.setText("select Satellite");
}
satellites++;
}
}
private void sPullDown(String cName) {
// sPullDown takes the constellation name and returns a String Array of all objects in the constellation. This code works correctly when called.
}
作爲一個方面說明,你應該看看使用[佈局](https://www.eclipse.org/articles/Article-Understanding-Layouts/Understanding-Layouts.htm)。如果您在具有不同字體大小的不同平臺上運行代碼,則使用setBounds會導致問題。 –
我會檢查一下。謝謝。 –