Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何失去jtextfield onClick的焦点_Java_Swing_Focus_Jtextfield - Fatal编程技术网

Java 如何失去jtextfield onClick的焦点

Java 如何失去jtextfield onClick的焦点,java,swing,focus,jtextfield,Java,Swing,Focus,Jtextfield,如何在单击日历窗口时失去JFormattedTextFeild的焦点 private void dateTxtFocusGained(java.awt.event.FocusEvent evt) { // TODO add your handling code here: PickDate ex=new PickDate(); // dateTxt.setText(Helper.pickDate.toString()); } ... private cl

如何在单击日历窗口时失去JFormattedTextFeild的焦点

private void dateTxtFocusGained(java.awt.event.FocusEvent evt) {
    // TODO add your handling code here:
    PickDate ex=new PickDate();       
    // dateTxt.setText(Helper.pickDate.toString());
}

...

private class MyDateListener implements DateListener
{
    public void dateChanged(DateEvent e)
    {
        Calendar c = e.getSelectedDate();
        if (c != null) {        
            formatTxt.setText(c.getTime()); 
            PickDate.this.dispose(); // pickdate is nothing but JFrame in which calender shows 
        }
        else {
            System.out.println("No time selected.");
        }
    }
}

我过去使用的一个技巧是切换组件的焦点。在onClick事件中:

//we just want to lose current caret focus
//but still have textfield be focusable
dateTxt.setFocusable(false);
dateTxt.setFocusable(true);

您可以请求对任何其他组件进行聚焦,因为正如您所知,窗口一次可以包含一个聚焦元素。任何其他SwingComponent.requestFocusInWindow();对不起,我听不懂