1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| import java.awt.event.ActionEvent;
import java.util.Vector;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class Test extends AbstractAction{
private Vector<Integer> vi;
private JTextField num;
private JButton add;
private JList jl;
private JFrame jf;
private JPanel jp;
public Test()
{
vi = new Vector<Integer>();
vi.add(2);
jf = new JFrame();
jp = new JPanel();
jl = new JList(vi);
JScrollPane jsp = new JScrollPane(jl);
jp.add(jsp);
jp.add(num = new JTextField(1));
jp.add(add=new JButton("Add"));
add.setAction(this);
jf.add(jp);
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
try{
vi.add(Integer.parseInt(num.getText()));
System.out.println(vi);
}
catch(Exception ex)
{
}
}
public static void main(String[] args) {
new Test();
}
} |
Partager