Jframe中有个panel1 
          panel1 里有个textArea 和 一个 panel2 
                                        panel2里有个button 
我想点击这个button的时候让textArea里的内容清空 
请问怎么做?谢谢! 呵呵 

解决方案 »

  1.   

    为button添加个监听
    点击button 
    对textArea.setText(""); 
      

  2.   

    将panel1对象传给panel2
    给textArea添加get方法
    在panel2中getTextArea().setText("");
      

  3.   

    button.addActionListener(
       new ActionListener(){
          public void actionPerformed(ActionEvent event){
             textArea.setText();
          }
       }
    );
      

  4.   

    谢谢楼上 关键是textArea到了actionPerformed里面就没作用了 编译器说找不到这个textArea啊
      

  5.   

    //panel1中加了个textArea
    JTextArea tArea=new JTextArea(15,20);
    tArea.setBorder(new LineBorder(Color.blue));
    pn1.add(tArea);//panel1的下部加了个panel2
    JPanel pn2=new JPanel(true);
    pn1.add("South",pn2);//panel2中添加个button 点这个button清空上面的textArea
    JButton btn2=new JButton("Cancel");
    btn2.addActionListener(
                               new ActionListener(){
                                                    public void actionPerformed(ActionEvent event){
                                                        tArea.setText("");
                                                      }
                                                   }
                                );
    pn2.add(btn2);编译器说找不到tArea
      

  6.   

    一個辦法  是你把JTextArea tArea  定義到外面
    另一個辦法就是樓上說的 用個get()方法
      

  7.   


    Why didn't u define textArea and button in the same panel?
      

  8.   

    把tArea定义成全局的,你的应该不是全局的
    public JTextArea tArea
    然后在实现的代码用:tArea=new new JTextArea(15,20); 
      

  9.   

    btn2.addActionListener(
    new ActionListener(){
          private JTextArea tArea = null;
          public void actionPerformed(ActionEvent event){
              tArea.setText("");
          }
          public ActionListener setTarget(JTextArea tArea) {
              this.tArea = tArea;
              return this;
          }
    }.setTarget(外面那个JTextArea引用)
    ); 潇洒的写法
      

  10.   

    jt sf 
    wo zai jf...
      

  11.   

    ....Please look at this....http://topic.csdn.net/u/20080829/12/c0f22640-1063-4569-a98c-ee228ae60266.html