我的問題是這樣的:我想要一個帶有組合框和確定按鈕的框架。我想從該列表中選擇一個項目,然後單擊確定,並基於所選項目有一個新項目的新組合框。如何聆聽按鈕動作添加組合框並再次聆聽動作Java
String[] locals = {"a bunch of strings"};
JComboBox<String> localsList = new JComboBox<String>(locals);
localsList.setSelectedIndex(0);
JButton okButton = new JButton("OK");
p.add(localsList);
p.add(okButton);
okButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
String value = localsList.getSelectedItem().toString();
switch(value){
case("a bunch of strings"):
JComboBox<String> concelhosList = new JComboBox<String>(concelhosL);
concelhosList.setSelectedIndex(0);
p.add(concelhosList);
}
}
});
okButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
String value = concelhosList.getSelectedItem().toString();
}
});
的問題是,第一個動作聽者內concelhosList
變量住宿等第二動作聽者不承認它,日食說我需要創建變量。
我該如何解決這個問題,或者是否有另一種方法來做到這一點?
不行的,它說,在'concelhosList =新JComboBox的(concelhosL);''那個地方在封閉範圍內定義的變量concelhosList必須是最終的或有效的最終' –
user3636673
concelhosL甚至不存在於您的代碼 – XtremeBaumer