赞
踩
实例说明
Java中提供了各种数据集合类,这些类主要用于保存复杂结构的数据。其中,ArrayList集合可以看作为动态数组,它突破普通数组固定长度的限制,可以随时向数组中添加和移除元素,这将使数组更加灵活。本实例通过ArrayList集合类实例向程序动态添加与删除学生姓名的功能,所以数据都保存在ArrayList集合的实例中。
关键技术
add()方法可以把任意类型的元素添加到List集合的尾部。其中元素类型任意。该方法声明如下:
public boolean add(E element)
参数说明:
1.element:要添加到集合中的任意类型的元素值或对象
2.返回值:是否成功添加元素
移除元素
remove()方法可以移除集合中的指定元素。该方法声明如下:
public boolean remove(Object object)
参数说明:
1.object:要从集合中移除的对象
2.返回值:是否成功移除数据
DynamicArray.java
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
-
- import javax.swing.AbstractListModel;
- import javax.swing.DefaultListModel;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.ListModel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JScrollPane;
- import javax.swing.JList;
- import java.awt.FlowLayout;
- import javax.swing.JButton;
- import java.awt.Dimension;
- import javax.swing.JLabel;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.util.ArrayList;
- import javax.swing.JTextField;
- import javax.swing.UIManager;
- import javax.swing.JPopupMenu;
- import java.awt.Component;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import javax.swing.JComboBox;
-
- public class DynamicArray extends JFrame {
-
- private JPanel contentPane;
- private final JPanel panel = new JPanel();
-
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- try {
- UIManager
- .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
- } catch (Throwable e) {
- e.printStackTrace();
- }
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- DynamicArray frame = new DynamicArray();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
-
- /**
- * Create the frame.
- */
- public DynamicArray() {
- setTitle("\u7528\u52A8\u6001\u6570\u7EC4\u4FDD\u5B58\u5B66\u751F\u59D3\u540D");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 337, 269);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- contentPane.setLayout(new BorderLayout(0, 0));
- setContentPane(contentPane);
-
- JScrollPane scrollPane = new JScrollPane();
- contentPane.add(scrollPane, BorderLayout.CENTER);
-
- list = new JList();
- scrollPane.setViewportView(list);
-
- JPanel panel_1 = new JPanel();
- scrollPane.setColumnHeaderView(panel_1);
-
- JLabel label = new JLabel("\u5B66\u751F\u59D3\u540D\uFF1A");
- panel_1.add(label);
-
- textField = new JTextField();
- panel_1.add(textField);
- textField.setColumns(10);
- panel.setPreferredSize(new Dimension(100, 10));
- contentPane.add(panel, BorderLayout.EAST);
- panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
-
- JButton button = new JButton("\u6DFB\u52A0\u5B66\u751F");
- button.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- do_button_actionPerformed(e);
- }
- });
- panel.add(button);
-
- JButton button_1 = new JButton("\u5220\u9664\u5B66\u751F");
- button_1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- do_button_1_actionPerformed(e);
- }
- });
- panel.add(button_1);
- }
-
- private ArrayList<String> arraylist = new ArrayList<String>();
- private JTextField textField;
- private JList list;
-
- protected void do_button_actionPerformed(ActionEvent e) {
- textField.requestFocusInWindow();
- textField.selectAll();
- String text = textField.getText(); //获取用户输入的姓名
- if(text.isEmpty()) //过滤为输入姓名的情况
- return;
- arraylist.add(text); //把姓名添加到数组集合中
- replaceModel(); //把数组集合中 的内容显示到界面列表控件中
- }
-
- protected void do_button_1_actionPerformed(ActionEvent e) {
- Object value = list.getSelectedValue(); //获取列表控件的选择项
- arraylist.remove(value); //从数组集合中移除用户的选择项
- replaceModel(); //把数组集合中的内容显示到界面列表控件中
-
- }
-
- private void replaceModel() {
- //为列表控件设置数据模型,用于显示数组集合中的数据
- list.setModel(new AbstractListModel() {
- @Override
- public int getSize(){ //获取数组大小
- return arraylist.size();
- }
- @Override
- public Object getElementAt(int index){ //获取指定索引元素
- return arraylist.get(index);
- }
-
- });
-
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。