zoukankan      html  css  js  c++  java
  • 动态数组保存学生姓名

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    
    import javax.swing.AbstractListModel;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextField;
    import javax.swing.UIManager;
    import javax.swing.border.EmptyBorder;
    
    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("u7528u52A8u6001u6570u7EC4u4FDDu5B58u5B66u751Fu59D3u540D");
            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("u5B66u751Fu59D3u540DuFF1A");
            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("u6DFBu52A0u5B66u751F");
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    do_button_actionPerformed(e);
                }
            });
            panel.add(button);
    
            JButton button_1 = new JButton("u5220u9664u5B66u751F");
            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() {
                public int getSize() {// 获取数组大小
                    return arraylist.size();
                }
    
                public Object getElementAt(int index) {// 获取指定索引元素
                    return arraylist.get(index);
                }
            });
        }
    }

    关键:

      ArrayList的增删操作。

  • 相关阅读:
    【pytest】(六) pytest中fixture的使用
    【pytest】(七) pytest的一些你爱不释手的插件
    【pytest】(五) pytest中的断言
    【pytest】(四) pytest的一些其他的运行用法
    本两周学习总结
    本两周学习总结
    本两周总结
    本两周学习总结
    本周学习总结(原生)
    本周学习总结(原生)
  • 原文地址:https://www.cnblogs.com/mr-wuxiansheng/p/12257438.html
Copyright © 2011-2022 走看看