import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Count extends JFrame implements ItemListener {
private JComboBox box;
private static String[] num = {"5", "6", "7", "8", "9", "10"};
private static int size, i;
public Count() {
super("Count");
setLayout(new FlowLayout());
box = new JComboBox(num);
box.addItemListener(this);
add(box);
}
@Override
public void itemStateChanged(ItemEvent e) {
size = Integer.parseInt((String)box.getSelectedItem());
for (i = 1; i <= size; i++) {
System.out.print(" " + i);
}
System.out.println();
}
public static void main(String[] args) {
Count a = new Count();
a.setSize(200, 150);
a.setVisible(true);
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
this code print from 1 to selected item
EX: if you select number 8 ,will print
1 2 3 4 5 6 7 8
BUT there is wrong
EX: when select number 8 ,will print
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
print twice, why?