请问有什么方法可以返回当前焦点所在的组件吗? 就是判断当前焦点在哪个组件上?感谢每个回复者 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 答:用KeyboardFocusManager对象中的getFocusOwner()方法或getGlobalFocusOwner()方法,或许能满足楼主的要求. 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. 比如说面板上有四个文本域,怎么判断光标在哪个文本域中?为什么我用isFocusOwner()不成功呢 JFileChooser fileChooser = new JFileChooser();这句出现空指针异常 急切请人帮忙、 关于一个小程序、 关于重载的问题询问 今天考试的一道题目,帮忙看下 怎么不显示??? Socket 怎样传输 新生成的图片 java鼠标事件的问题 问个问题,估计谁都知道的简单问题 菜鸟求java程序!!!急!!!! 关于井字游戏问题 编程实现对一个按钮的点击 编译javat程序,出错提示为软件包system不存在
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.