根据您的确切上下文,您可能考虑使用自定义TextAction:其方法
getTextComponent(ActionEvent)返回最新的焦点文本组件。一个代码片段:
Action logSelected = new TextAction("log selected") { @Override public void actionPerformed(ActionEvent e) { JTextComponent text = getTextComponent(e); System.out.println("selected: " + text.getSelectedText()); } }; JComponent content = new JPanel(); content.add(new JTextField("sometext", 20)); content.add(new JTextField("other content", 20)); content.add(new JCheckBox("just some focusable comp")); content.add(new JButton(logSelected));


