首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

JTextField 初步提示文字不能显示,需获焦再失焦后才能显示 求解

2013-11-23 
JTextField 初始提示文字不能显示,需获焦再失焦后才能显示 求解package com.vtars.ttsdemo.panelsimport

JTextField 初始提示文字不能显示,需获焦再失焦后才能显示 求解

package com.vtars.ttsdemo.panels;

import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JLabel;
/*
 * 此类主要定义列表类
 * 将列表以单独的类实现并将列表生成在滚动面板上返回供底层面板调用
 */
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.apache.poi.hslf.record.Document;

import com.vtars.ttsdemo.business.Speaker;
import com.vtars.ttsdemo.business.ebi.StuEbi;
import com.vtars.ttsdemo.business.factory.StuEbiFactory;
import com.vtars.ttsdemo.dao.txtRead;
import com.vtars.ttsdemo.dao.dao.StudentDao;
import com.vtars.ttsdemo.dao.factory.StudentDaoFactoty;
import com.vtars.ttsdemo.framework.io.ReadExcel;
import com.vtars.ttsdemo.vo.StudentModel;

public class ListPanelUI {
private JPanel listJPanel; // 较低层面板-此面板用来承装滚动面板和搜索框
private static JTextField textFieldSearch;// 用来搜索滴
private static JScrollPane listJScrollPane; // 滚动面板-此面板承装列表
private static JList IdNameList;
private static DefaultListModel model = new DefaultListModel();// 列表模型
private static ListPanelUI singlistpJPanel = new ListPanelUI();// 单例调用
static String initStr = "快速查找“学号”“姓名”";
public static ListPanelUI getInstance() {
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() {
JTextField 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) {
// TODO Auto-generated method stub

}

@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub

}

@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub

}
});

return textFieldSearch;
}

public void FindTheItem() {
// TODO 自动生成的方法存根
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) {
// TODO 自动生成的方法存根
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;
}
}


}



JTextField 初步提示文字不能显示,需获焦再失焦后才能显示 求解 java? JTextField?
[解决办法]
 
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);
}
}




显示正常。
[解决办法]
使用SwingX中的JXTextField

热点排行