当前位置: 技术问答>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!
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();
}
}
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();
}
}
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一下看看哈
呵呵, 再try一下看看哈