java中如何用Tab键改变文本框的焦点?
1个回答
展开全部
import java.awt.FlowLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.*;
public class Example6_13
{
public static void main(String[] args)
{
new FocusTest();
}
}
class FocusTest extends JFrame
{
JTextField txt1,txt2;
public FocusTest()
{
setTitle("焦点事件示例");
setBounds(300,200,350,200);
setVisible(true);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
txt1=new JTextField("",10);
txt2=new JTextField("",10);
txt1.addFocusListener(new txtFocus());
txt2.addFocusListener(new txtFocus());
add(txt1);
add(txt2);
add(new JButton("确定"));
add(new JLabel("使用Tab键改变组件的焦点"));
validate();
}
class txtFocus implements FocusListener
{
public void focusGained(FocusEvent e)
{
if(e.getSource()==txt1)
txt1.setText("得到焦点");
else
txt2.setText("得到焦点");
}
public void focusLost(FocusEvent e)
{
if(e.getSource()==txt1)
txt1.setText("失去焦点");
else
txt2.setText("失去焦点");
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询