add FocusListener to the JTextArea components
focusGained(focusEvent e){
   //a JtextArea gained focus, insert str}

解决方案 »

  1.   

    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();
      }
    }<------ 树欲静而风不止 ------>
      

  2.   

    不好意思,上面的Button事件内写成textTemp.insert(temp,textArea1.getText().length());<------ 树欲静而风不止 ------>
      

  3.   

    又写错了,哎~~~~~~textTemp.insert(temp,textTemp.getText().length());<------ 树欲静而风不止 ------>
      

  4.   

    >>>>>mq612(理想)
        非常感谢你的帮助,我得问题在你的帮助下已经完成了,主要就是动态得到组件名字的问题。我发现在这儿交流着对提高是有很大帮助的,你学习java多长时间了。
      

  5.   

    〉〉〉〉wobelisk() 也谢谢你的帮助,你们的帮助就是我学习的动力。同时也谢谢参与地同志们。
      

  6.   

    呵呵~~~~别客气,一起学习嘛,Java如海呀。<------ 树欲静而风不止 ------>