当然是具体组件,例如
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");
}
};}
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");
}
};}
解决方案 »
- 做一次if比较 和一次new操作 哪个需要花费的时间长一些 欢迎拍砖~~
- swing 鼠标滑过 显示提示信息
- java 图形操作 GUI
- 发布一个用SWT/JFace实现的开源小项目jZoomer放大镜/取色软件,寻求其下一步发展的建设性意见
- XLS 文件乱码
- 请问不使用isAutoIncrement()函数用DatabaseMetaData对象可以检测自动编号的字段吗?
- 请问大虾们下面是什么加密算法 是一个邮件系统easymail里面的
- package的问题,ntzls(三星堆)请进。
- 各位大侠:我有一个2M左右的只有一行的纯文本文件,要怎么读?
- 请教磁盘分区修复!!!
- 一个菜菜的问题!
- 在哪儿下java的教程
java.lang.Object
|
+--java.awt.Component
|
+--java.awt.Container
|
+--javax.swing.JComponent
|
+--javax.swing.JInternalFrame
看看我给你的例子吧,其中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");
} };
}
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");}
};
}