就是判断当前焦点在哪个组件上?感谢每个回复者

解决方案 »

  1.   

     答:用KeyboardFocusManager对象中的getFocusOwner()方法或getGlobalFocusOwner()方法,或许能满足楼主的要求.
      

  2.   


    package com.sysdynamic.panel;import java.awt.Component;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JOptionPane;
    import javax.swing.SwingUtilities;
    import javax.swing.WindowConstants;/**
     * 
     * @author Andy
     *
     */
    public class AndyJFrame extends javax.swing.JFrame {
    private JButton btnName;
    private JButton btnAge;
    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    AndyJFrame inst = new AndyJFrame();
    inst.setLocationRelativeTo(null);
    inst.setVisible(true);
    }
    });
    } public AndyJFrame() {
    super();
    initGUI();
    } private void initGUI() {
    try {
    FlowLayout thisLayout = new FlowLayout();
    getContentPane().setLayout(thisLayout);
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    {
    btnName = new JButton();
    getContentPane().add(btnName);
    btnName.setText("Name");
    btnName.addActionListener(new MyactionListener()); }
    {
    btnAge = new JButton();
    getContentPane().add(btnAge);
    btnAge.setText("Age");
    btnAge.addActionListener(new MyactionListener()); }
    pack();
    setSize(400, 300);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }}class MyactionListener implements ActionListener { Component component; public MyactionListener() { } public MyactionListener(Component component) {
    this.component = component;
    } public void actionPerformed(ActionEvent e) { if (((JButton) e.getSource()).getText().toString().equals("Name")){
    JOptionPane.showMessageDialog(null, "这是Name JButton!");
    }
    else{
    JOptionPane.showMessageDialog(null, "这是Age JButton!");
    } }}注册一个相同的事件,然后去if.
      

  3.   

    比如说面板上有四个文本域,怎么判断光标在哪个文本域中?为什么我用isFocusOwner()不成功呢