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

关于FocusAdaptor(FocusListener)

    来源: 互联网  发布时间:2015-09-13

    本文导语:  我要监视某个组件是否获得焦点,事件监视器应该加到具体组件还是加到放组件的容器上? | 当然是具体组件,例如 import java.awt.event.*; import javax.swing.*; public class FocusTest  { static JFrame fram...

我要监视某个组件是否获得焦点,事件监视器应该加到具体组件还是加到放组件的容器上?

|
当然是具体组件,例如
import java.awt.event.*;
import javax.swing.*;

public class FocusTest 
{
static JFrame frame = new JFrame();
static JButton bt1 = new JButton("Button1");
static JButton bt2 = new JButton("Button2");
static JButton bt3 = new JButton("Button3");

public static void main(String[] args) 
{
JPanel p = new JPanel();
p.add(bt1);
p.add(bt2);
p.add(bt3);
frame.getContentPane().add(p);
FocusAdapter fa = new MyFocusAdapter();
bt1.addFocusListener(fa);
bt2.addFocusListener(fa);
bt3.addFocusListener(fa);
frame.setBounds(100,100,400,400);
frame.show();
}
static class MyFocusAdapter extends FocusAdapter
{
public void focusLost(FocusEvent e)
{
if(e.getSource()==bt1)
JOptionPane.showMessageDialog(null, "Button1 Focus Lost");
else if(e.getSource()==bt2)
JOptionPane.showMessageDialog(null, "Button2 Focus Lost");
else
JOptionPane.showMessageDialog(null, "Button3 Focus Lost");
}
};

}

|
好像是不能,可是你为什么要用FocusListener?位是么不用InternalFrameListener?看看下面的代码,也可以呀,而且比FocusListener功能还多。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;


class Test extends JFrame
{
private JDesktopPane desk = new JDesktopPane();
private InternalFrameListener frameListener = new MyFrameListener();
JInternalFrame iframe1 = new JInternalFrame();
JInternalFrame iframe2 = new JInternalFrame();
Test()
{
setContentPane(desk);
desk.add(iframe1);
desk.add(iframe2);
iframe1.addInternalFrameListener(frameListener);
iframe2.addInternalFrameListener(frameListener);
iframe1.reshape(10, 10, 300, 300);
iframe2.reshape(40, 40, 300, 300);
iframe1.show();
iframe2.show();
setBounds(100,100,400,400);
show();
}
public static void main(String[] args) 
{
Test t = new Test();
}
class MyFrameListener implements InternalFrameListener
{
public void internalFrameActivated(InternalFrameEvent e) 
{System.out.println("Activated");}
public void internalFrameClosed(InternalFrameEvent e) 
{System.out.println("Closed");}
public void internalFrameClosing(InternalFrameEvent e) 
{System.out.println("Closing");}
public void internalFrameDeactivated(InternalFrameEvent e) 
{System.out.println("Deactivated");}
public void internalFrameDeiconified(InternalFrameEvent e) 
{System.out.println("Deiconified");}
public void internalFrameIconified(InternalFrameEvent e) 
{System.out.println("Iconified");}
public void internalFrameOpened(InternalFrameEvent e) 
{System.out.println("Opened");}
};
}

|
加到具体组件上。

|
你可能是拼错了吧focusGained,不是FocusGained,这样,框架就把你的FocusGained当成一个普通的函数了,而用FocusAdapter中的一个空的focusGained来处理相应。
看看我给你的例子吧,其中focusGained中的探出对话框被注释了,因为那样会不停的把焦点在对话框和JInternalFrame之间切换。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


class Test extends JFrame
{
private JDesktopPane desk = new JDesktopPane();
private JButton bt1 = new JButton("Button1");
private JButton bt2 = new JButton("Button2");
private JButton bt3 = new JButton("Button3");
private FocusAdapter focusListener = new MyFocusAdapter();
Test()
{
setContentPane(desk);
JInternalFrame iframe = new JInternalFrame();
desk.add(iframe);
JPanel p = new JPanel();
p.add(bt1);
p.add(bt2);
p.add(bt3);
iframe.getContentPane().add(p);
bt1.addFocusListener(focusListener);
bt2.addFocusListener(focusListener);
bt3.addFocusListener(focusListener);
iframe.reshape(10, 10, 300, 300);
iframe.show();
setBounds(100,100,400,400);
show();
}
public static void main(String[] args) 
{
Test t = new Test();
}
class MyFocusAdapter extends FocusAdapter
{
public void focusGained(FocusEvent e)
{
//If this is allowed, you will get this diaog always
//JOptionPane.showMessageDialog(null, "Gained");
System.out.println("focusGained");
}
public void focusLost(FocusEvent e)
{
JOptionPane.showMessageDialog(null, "Lost");
System.out.println("focusLost");
}

};
}

    
 
 

您可能感兴趣的文章:

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












  • 相关文章推荐


  • 站内导航:


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

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

    浙ICP备11055608号-3