当前位置:  技术问答>java相关

the super developer for Swing/AWT/JFC , come in please

    来源: 互联网  发布时间:2015-05-10

    本文导语:  I have a very simple program to test use method requestFocus() my program is below: //////////////////////////////////////////////////// import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class TextTestFrame extends JFram...

I have a very simple program to test use method requestFocus()
my program is below:
////////////////////////////////////////////////////
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class TextTestFrame extends JFrame  implements  FocusListener
{  public TextTestFrame()
   {  setTitle("TextTest");
      setSize(300, 200);
      addWindowListener(new WindowAdapter()
      {  public void windowClosing(WindowEvent e)
         {  System.exit(0);
         }
      }); 

  //addFocusEvent(this);
addFocusListener(this); 
  

      Container contentPane = getContentPane();

      JPanel p = new JPanel();
      hourField = new JTextField("12", 3);
      p.add(hourField);

      minuteField = new JTextField("00", 3);
      //minuteField.requestFocus();
      p.add(minuteField);

      contentPane.add(p, "South");
   }

   public void focusLost(FocusEvent evt)
   {

   }
   public void focusGained(FocusEvent evt)
   {
       minuteField.requestFocus();
   }
   private JTextField hourField;
   private JTextField minuteField;

}

public class TextTest
{  public static void main(String[] args)
   {  JFrame frame = new TextTestFrame();
      frame.show();
   }
}

//////////////////////////////
I only want when I run this program , the focus at minuteField . but it always focus at hourField .
What’s wrong with me ? how to do it ?
Please answer my question detailly!
Thanx!

|
You can try it :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class TextTestFrame extends JFrame  
{  
   private JTextField hourField;
   private JTextField minuteField;
   private NmFocusListener aFocusListener = new NmFocusListener() ;
   public TextTestFrame()
   {  
      setTitle("TextTest");
      setSize(300, 200);
      addWindowListener(new WindowAdapter()
      {  public void windowClosing(WindowEvent e)
         {  System.exit(0);
         }
      }); 
      Container contentPane = getContentPane();

      JPanel p = new JPanel();
      hourField = new JTextField("12", 3);
      hourField.addFocusListener(aFocusListener);
      p.add(hourField);

      minuteField = new JTextField("00", 3);
      minuteField.addFocusListener(aFocusListener) ;
      //minuteField.requestFocus();
      p.add(minuteField);

      contentPane.add(p, "South");
      toFront();
      
   }

    class NmFocusListener extends FocusAdapter
    {    
        public void focusGained(FocusEvent event)
        {
            minuteField.requestFocus();
        }
    }// of inner class NmFoucsListener
}

public class TextTest
{  public static void main(String[] args)
   {  TextTestFrame frame = new TextTestFrame();
      frame.show();
   }
}

|
The following code should meet ur request,pls try it:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class TextTestFrame extends JFrame  
{  
   private JTextField hourField;
   private JTextField minuteField;
   private NmFocusListener aFocusListener = new NmFocusListener() ;
   private static int x = 0 ;
   
   public TextTestFrame()
   {  
      setTitle("TextTest");
      setSize(300, 200);
      addWindowListener(new WindowAdapter()
      {  public void windowClosing(WindowEvent e)
         {  System.exit(0);
         }
      }); 
      Container contentPane = getContentPane();
      JPanel p = new JPanel();
      hourField = new JTextField("12", 3);
      hourField.addFocusListener(aFocusListener);
      p.add(hourField);
      minuteField = new JTextField("00", 3);
      minuteField.addFocusListener(aFocusListener) ;
      p.add(minuteField);
      contentPane.add(p, "South");
      toFront();
   }

    class NmFocusListener extends FocusAdapter
    {
        public void focusGained(FocusEvent event)
        {
            if(x == 0)
            {
                minuteField.requestFocus();
                x++ ;
            }
        }
    }
}

public class TextTest
{  public static void main(String[] args)
   {  TextTestFrame frame = new TextTestFrame();
      frame.show();
   }
}

|
刚才看了一下awt,默认的焦点遍历顺序就是你的component的加入顺序
呵呵, 再try一下看看哈

    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • C++ Double Ended Queues 成员 resize():change the size of the dequeue
  • If I know the function used in the program,how can I get the library which provides the implemation of the BEING USED function
  • C++ Double Ended Queues 成员 assign():set the values of the dequeue
  • 哪位解释一下这句话constructor code executes from the current class up the hierarchy to the ancestor class
  • C++ Double Ended Queues 成员 size():return the number of elements in the dequeue
  • the origin about the Grapics class? SOS
  • C++ Double Ended Queues 成员 end():returns an iterator to the end of the queue
  • 驱动加载时出现“couldn't find the kernel version the module was compiled for”
  • C++ Double Ended Queues 成员 rbegin():returns a reverse iterator to the end of the dequeue
  • How can the linux kernel get the value of a environment variable
  • C++ Double Ended Queues 成员 rend():returns a reverse iterator to the beginning of the dequeue
  • couldn 't find the kernel version the module was compiled for
  • C++ Double Ended Queues 成员 max_size():returns the maximum elements that the dequeue can hold
  • "The '$GRIL' did well" 为什么是The 'girl' di well.
  • C++ Double Ended Queues 成员 push_front():add an element to the front of the dequeue
  • what's the meaning of the following shell command?
  • C++ Double Ended Queues 成员 push_back():add an element to the end of the dequeue
  • insmod hello.o:couldn't find the kernel version the module was compiled for
  • C++ Double Ended Queues 成员 front():returns the first element
  • The kernel module can't be loaded correctly during the system booting
  • C++ Double Ended Queues 成员 back():returns the last element
  • help me to explain the falls in the text. thinks!


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3