标签:点击 dem seve weight class strong xtend factor 绿色
【FocusListener焦点监听器】
public class Demo extends JFrame { public Demo(){ setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setBounds(100,100,200,120); Container c=getContent(); JTextField sk1=new JTextField(); JTextField sk2=new JTextField(); JTextField sk3=new JTextField(); sk1.setColumns(15); sk2.setColumns(15); sk3.setColumns(15); c.add(sk1); c.add(sk2); c.add(sk3); sk1.FocusListener(new myFocusListener );//调用自定义焦点监听方法类 sk2.FocusListener(new myFocusListener ); sk3.FocusListener(new myFocusListener );
setVisible(true); //在myFocusListener 上 ,按Alt+Enter导入Java类快捷键 implements FocusListener class myFocusListener implements FocusListener{//实现焦点监听 有光标点击为绿色,无光标点击为红色。 @Override public void focusGained(FocusEvent e) { JTextField sk=(JTextField) e.getSource();//获取触发焦点事件的控件,强制转换为文本 jt.setBorder(BorderFactory.createLineBorder(Color.green)); } @Override public void focusLost(FocusEvent e) { JTextField sk=(JTextField) e.getSource();//获取焦点事件触发的控件。强制转换为文本 jt.setBorder(BorderFactory.createLineBorder(Color.RED)); } } public static void main(String[] args) { new Demo(); } }
标签:点击 dem seve weight class strong xtend factor 绿色
原文地址:https://www.cnblogs.com/mld1040871703/p/10230765.html