怎么判断和得到当前输入光标所在的组件名字!并插字符串到光标处。 add FocusListener to the JTextArea componentsfocusGained(focusEvent e){ //a JtextArea gained focus, insert str} 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Test { JFrame frame ; JPanel pane ; JScrollPane sPane1,sPane2,sPane3 ; JTextArea textArea1,textArea2,textArea3,textTemp ; JButton button ; String temp ; public Test() { temp = "我们都有一个家,名字叫中国。" ; jbInit(); } public void jbInit(){ frame = new JFrame ("获取焦点插入") ; pane = new JPanel() ; button = new JButton ("插入") ; textArea1 = new JTextArea(3,25) ; textArea2 = new JTextArea(3,25) ; textArea3 = new JTextArea(3,25) ; sPane1 = new JScrollPane(textArea1); sPane2 = new JScrollPane(textArea2); sPane3 = new JScrollPane(textArea3); frame.getContentPane().add(pane, BorderLayout.CENTER); pane.add(sPane1, null) ; pane.add(sPane2, null) ; pane.add(sPane3, null) ; pane.add(button, null) ; frame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); textArea1.addFocusListener(new FocusListener(){ public void focusGained(FocusEvent focusEvent) { textArea(focusEvent); } public void focusLost(FocusEvent focusEvent) { } }); textArea2.addFocusListener(new FocusListener(){ public void focusGained(FocusEvent focusEvent) { textArea(focusEvent); } public void focusLost(FocusEvent focusEvent) { } }); textArea3.addFocusListener(new FocusListener(){ public void focusGained(FocusEvent focusEvent) { textArea(focusEvent); } public void focusLost(FocusEvent focusEvent) { } }); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(textTemp==textArea1){ textArea1.insert(temp,textArea1.getText().length()); } if(textTemp==textArea2){ textArea2.insert(temp,textArea2.getText().length()); } if(textTemp==textArea3){ textArea3.insert(temp,textArea3.getText().length()); } } }); frame.setSize(300,250); frame.setVisible(true); } public void textArea(FocusEvent focusEvent){ textTemp = (JTextArea) focusEvent.getComponent() ; } public static void main(String[] args) { Test test = new Test(); }}<------ 树欲静而风不止 ------> 不好意思,上面的Button事件内写成textTemp.insert(temp,textArea1.getText().length());<------ 树欲静而风不止 ------> 又写错了,哎~~~~~~textTemp.insert(temp,textTemp.getText().length());<------ 树欲静而风不止 ------> >>>>>mq612(理想) 非常感谢你的帮助,我得问题在你的帮助下已经完成了,主要就是动态得到组件名字的问题。我发现在这儿交流着对提高是有很大帮助的,你学习java多长时间了。 〉〉〉〉wobelisk() 也谢谢你的帮助,你们的帮助就是我学习的动力。同时也谢谢参与地同志们。 呵呵~~~~别客气,一起学习嘛,Java如海呀。<------ 树欲静而风不止 ------> swing中组件注册监听器的问题 java写入文本的问题 Too many open files 关于Iterator的问题???????????? 今天去面试,考官出了一正则表达式的题目,一下蒙住了,各位给看看! 小弟想学JAVA,请达人推荐几本JAVA的书籍 小弟是JAVA初学者,这儿有道题不会做,急死人啦。请大家给出源程序啊,加分~ 使用匿名类的问题…… X:\j2sdk1.4.1_06\demo\java3d\ObjLoad 谁给我这个新人看看这个问题? JAVA的系统 现在的困惑!!!
import java.awt.*;
import java.awt.event.*;public class Test {
JFrame frame ;
JPanel pane ;
JScrollPane sPane1,sPane2,sPane3 ;
JTextArea textArea1,textArea2,textArea3,textTemp ;
JButton button ;
String temp ; public Test() {
temp = "我们都有一个家,名字叫中国。" ;
jbInit();
} public void jbInit(){
frame = new JFrame ("获取焦点插入") ;
pane = new JPanel() ;
button = new JButton ("插入") ;
textArea1 = new JTextArea(3,25) ;
textArea2 = new JTextArea(3,25) ;
textArea3 = new JTextArea(3,25) ;
sPane1 = new JScrollPane(textArea1);
sPane2 = new JScrollPane(textArea2);
sPane3 = new JScrollPane(textArea3);
frame.getContentPane().add(pane, BorderLayout.CENTER);
pane.add(sPane1, null) ;
pane.add(sPane2, null) ;
pane.add(sPane3, null) ;
pane.add(button, null) ;
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
textArea1.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent focusEvent) {
textArea(focusEvent);
}
public void focusLost(FocusEvent focusEvent) {
}
});
textArea2.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent focusEvent) {
textArea(focusEvent);
}
public void focusLost(FocusEvent focusEvent) {
}
});
textArea3.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent focusEvent) {
textArea(focusEvent);
}
public void focusLost(FocusEvent focusEvent) {
}
});
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(textTemp==textArea1){
textArea1.insert(temp,textArea1.getText().length());
}
if(textTemp==textArea2){
textArea2.insert(temp,textArea2.getText().length());
}
if(textTemp==textArea3){
textArea3.insert(temp,textArea3.getText().length());
}
}
});
frame.setSize(300,250);
frame.setVisible(true);
} public void textArea(FocusEvent focusEvent){
textTemp = (JTextArea) focusEvent.getComponent() ;
} public static void main(String[] args) {
Test test = new Test();
}
}<------ 树欲静而风不止 ------>
非常感谢你的帮助,我得问题在你的帮助下已经完成了,主要就是动态得到组件名字的问题。我发现在这儿交流着对提高是有很大帮助的,你学习java多长时间了。