我試圖編寫一個很短的類,將JPanel「綁定」到JComboBox。我覺得我有邏輯下來,但是當我選擇使用的JComboBox新的東西什麼也沒有發生......這是(或多或少)我的代碼:Janel不會刷新與JComboBox的選擇
private DisplayPanel currentDisplay; //a displaypanel is simply an extended JPanel with an id field, and an overriden .equals() method
private JComboBox selector;
private List<DisplayPanel> displays;
public SelectionPanel(DisplayPanel panel){
displays = new ArrayList<DisplayPanel>();
selector = new JComboBox(new String[]{panel.id});
currentDisplay = panel;
selector.addActionListener(this);
this.add(selector);
this.add(currentDisplay);
this.displays.add(panel);
}
public void addNewSelection(DisplayPanel panel){
displays.add(panel);
selector.addItem(panel.id);
}
@Override
public void actionPerformed(ActionEvent e) {
JComboBox source = (JComboBox) e.getSource();
String id = (String) source.getSelectedItem();
for(DisplayPanel display: displays)
if(id.equals(display.id))
currentDisplay = display;
this.validate();
}
我假設我需要重寫重繪()功能,但我真的不確定最好的方式來做到這一點。
我不是100%確定,但我認爲你不應該每次都完全取代該面板。 (雖然我不是100%確定) – jjnguy