日期:2014-05-20 浏览次数:20656 次
import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JTextField; public class TestText extends JFrame implements FocusListener { private JTextField[] txts = new JTextField[3]; public TestText() { getContentPane().setLayout(new FlowLayout()); for (int i = 0; i < txts.length; i++) { txts[i] = new JTextField("Test" + i); txts[i].setName("我是Text:" + i); txts[i].setPreferredSize(new Dimension(70, 20)); txts[i].addFocusListener(this); getContentPane().add(txts[i]); } setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(300, 200); } public void focusGained(FocusEvent e) { System.out.println(((JComponent) e.getSource()).getName()); } public void focusLost(FocusEvent e) { } public static void main(String[] args) { TestText frame = new TestText(); frame.setVisible(true); } }
------解决方案--------------------