解决方案 »

  1.   

     
    public class ListPanelUI {
        private JPanel listJPanel; // 较低层面板-此面板用来承装滚动面板和搜索框
        private JTextField textFieldSearch;// 用来搜索滴
        private JScrollPane listJScrollPane; // 滚动面板-此面板承装列表
        private JList IdNameList;
        private DefaultListModel model = new DefaultListModel();// 列表模型
        private static ListPanelUI singlistpJPanel = null;// 单例调用
        static String initStr = "快速查找“学号”“姓名”";
        public static ListPanelUI getInstance() {
    if(null==singlistpJPanel){
    singlistpJPanel=new ListPanelUI();
    }
            return singlistpJPanel;
        }
     
        private ListPanelUI() {
            InitlistJPanel();
        }
     
        /*
         * 获取整合后的面板
         */
        public JPanel getlistJPanel() {
            return listJPanel;
        }
     
        /*
         * 初始化承装滚动面板和搜索框的底层面板
         */
        public void InitlistJPanel() {
            listJPanel = new JPanel();
            listJPanel.setLayout(null);
            listJPanel.add(InitlistJScrollPane());
            listJPanel.add(InittextFieldSearch());
        }
     
        /*
         * 初始化搜索输入框
         */
        public JTextField InittextFieldSearch() {
            textFieldSearch = new JTextField(initStr);
            textFieldSearch.setForeground(Color.LIGHT_GRAY);
            textFieldSearch.setBounds(10, 590, 161, 27);
            textFieldSearch.addFocusListener(new FocusListener() {
               public void focusGained(FocusEvent e) {
                   JTextField src = (JTextField) e.getSource();
                   src.setForeground(Color.blue);
                   if (src.getText().equals(initStr)){
                       src.setText(null);
                   }
               }
               public void focusLost(FocusEvent e) {
                   JTextField src = (JTextField) e.getSource();
                   if (src.getText().trim().isEmpty()) {
                       src.setText(initStr);
                       src.setForeground(Color.LIGHT_GRAY);
                   }
               }
            });
            
            textFieldSearch.addKeyListener(new KeyListener() {
                @Override
                public void keyTyped(KeyEvent e) {
                }
                 
                @Override
                public void keyReleased(KeyEvent e) {
                }
                 
                @Override
                public void keyPressed(KeyEvent e) {                 
                }
            });
             
            return textFieldSearch;
        }
         
        public void FindTheItem() {
            String key = textFieldSearch.getText();
            if ((key == null) || (key.trim().isEmpty())) {
                // 处理为空格的时候
                textFieldSearch.setText("");// 当为空格的时候直接删除
            }
            if (model.contains(key)) {
                int index = model.indexOf(key);
                IdNameList.setSelectedIndex(index);
            }else {
                IdNameList.clearSelection();
                System.out.println("没有元素");
            }
        }
     
        /*
         * 初始化承装列表的滚动面板
         */
        public JScrollPane InitlistJScrollPane() {
            listJScrollPane = new JScrollPane();
            listJScrollPane.setBounds(10, 10, 161, 570);
            listJScrollPane.setColumnHeaderView(new JLabel("学生列表",
                    SwingConstants.CENTER));
            listJScrollPane.setViewportView(InitJList());
            return listJScrollPane;
        }
     
        /*
         * 初始化Jlist
         */
        public JList InitJList() {
            System.out.println("Jlist初始化");
            // 初始化一个List
            IdNameList = new JList();
            //下面作为测试,注释掉先
            //// 设置列表的选择模式为单选
            //IdNameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            //final StuEbi ebi = StuEbiFactory.getStuEbi();
            //List<StudentModel> list = ebi.getAll();
            //
            //IdNameList.setModel(model);// 将model设置给列表
            //IdNameList.setListData(list.toArray());
            //IdNameList.setSelectedIndex(0);
            //// 监听选择
            //IdNameList.addListSelectionListener(new ListSelectionListener() {
            //    @Override
            //    public void valueChanged(ListSelectionEvent e) {
            //        if (e.getValueIsAdjusting()){//通过此处判断来过滤多余的事件
            //        }else {
            //            StudentModel selectedStuModel = (StudentModel) IdNameList.getSelectedValue();
            //            String nameString = selectedStuModel.getName();
            //            Speaker temp = Speaker.getInstance();
            //            temp.SpeakWords(nameString);
            //
            //            SpeakNameFace.lblShowNumLabel.setText(selectedStuModel.getId());
            //            SpeakNameFace.lblNowShowLabel.setText(nameString);
            //        }
            //    }
            //});
            return IdNameList;
        }
         
        /**
         * 获取列表当前选择的下一个
         * 若当前项的索引小于最大索引值 则选择到下个索引值
         * 否则弹出消息框
         * @return
         */
        public boolean SetNextselected(){
            //获取当前选中的索引值
            int selectedindex = IdNameList.getSelectedIndex();
            System.out.println("当前索引:"+selectedindex+"最大索引:"+IdNameList.getLastVisibleIndex());
            if(selectedindex<IdNameList.getLastVisibleIndex()){
                //TODO 此处还需完善 列表向上滚 还没有做完善的处理
                IdNameList.setSelectedIndex(selectedindex+1);
                IdNameList.scrollRectToVisible(IdNameList.getCellBounds(selectedindex, selectedindex+2));
                return true;
            }else {
                JOptionPane.showConfirmDialog(null, "已到最后!");
                return false;
            }
        }
        public boolean SetBeforeselected(){
            int selectedindex = IdNameList.getSelectedIndex();
            System.out.println("当前索引:"+selectedindex+"最小索引:"+IdNameList.getFirstVisibleIndex());
            if(selectedindex>0){
                IdNameList.setSelectedIndex(selectedindex-1);
                IdNameList.scrollRectToVisible(IdNameList.getCellBounds(selectedindex-1, selectedindex));
                return true;
            }else {
                JOptionPane.showConfirmDialog(null, "已到最前!");
                return false;
            }
        }
    public static void main(String args[]){
    JFrame jframe=new JFrame("ListPanelUI");
    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jframe.setBounds(10, 10, 300, 700);
    jframe.add(ListPanelUI.getInstance().getlistJPanel());
    jframe.setVisible(true);
    }
    }
    显示正常。
      

  2.   


    private static ListPanelUI singlistpJPanel = new ListPanelUI();// 单例调用
    static String initStr = "快速查找“学号”“姓名”";
    对换即可 其实就是静态变量初始化顺序的问题
    搞定