我想回來一個數組的每個第二個元素(所以在這種情況下2.和4.)。JComboBox ActionListener不起作用
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class tester2 extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
JFrame f = new JFrame();
String [] corps = {"IBM", "Apple", "Oracle", "Google"};
JComboBox <String> cb = new JComboBox <>();
JLabel l1 = new JLabel();
public tester2() {
f.setSize(500, 300);
f.setLayout(new BorderLayout());
f.setVisible(true);
f.setTitle("Tester 2");
f.setLocationRelativeTo(null);
f.add(cb, BorderLayout.NORTH);
f.add(l1);
// Combo
cb.removeAllItems();
cb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 1; i < corps.length; i=+2) {
String tempRes = corps[i];
System.out.println(tempRes);
cb.addItem(tempRes);
}
}
});
}
public static void main(String[] args) {
new tester2();
}
}
更多細節,更多詳情,更多詳情,更多詳情,更多詳情,更多詳情,更多詳情,更多詳情,更多詳情,更多詳情,更多詳情,更多詳情,更多詳情,更多的細節,更細節,更多細節。
感謝
在0無法啓動循環在1 – XtremeBaumer
*「我想拿回數組中的每個第二部件(所以在這種情況下,2和4)。」 *我想一飛排出冰淇淋的小馬。現在我們都討論了我們想要的。你有**問題**我們可以**回答**? –
爲什麼這段代碼沒有得到任何結果?例如System.out.println(tempRes); - >不打印任何東西 – fucorogu