不行啊!!看下面的程序。frame1实现了一个窗口,按钮点击事件中创建了communication对象。该对象试图完成更新窗口中文本框内容的任务。请问communication类中怎样才能调用文本框的方法?package test_tcp;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.*;public class Frame1 extends JFrame {
  private JPanel contentPane;
  private JToggleButton cmdSend = new JToggleButton();
  private ScrollPane scrollPane1 = new ScrollPane();
  public JTextArea jTextArea1 = new JTextArea();
  private GridBagLayout gridBagLayout1 = new GridBagLayout();
  private JTextField txtMessage = new JTextField();
  private JButton cmdClose = new JButton();
  private JLabel jLabel1 = new JLabel();
  private JLabel jLabel2 = new JLabel();
  private JTextField inPort = new JTextField();
  private JTextField outPort = new JTextField();
  private communication mysend;
  private communication myserver;  //Construct the frame
  public Frame1() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    bInit();
  }  //Component initialization
  private void jbInit() throws Exception  {
    //setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(gridBagLayout1);
    this.setSize(new Dimension(416, 400));
    this.setTitle("测试tcp通讯");
    cmdSend.setText("发送");
    cmdSend.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        cmdSend_actionPerformed(e);
      }
    });
    txtMessage.setToolTipText("");
    cmdClose.setText("关闭");
    cmdClose.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        cmdClose_actionPerformed(e);
      }
    });
    jLabel1.setToolTipText("");
    jLabel1.setText("监听端口");
    jLabel2.setText("目标端口");
    jLabel2.setToolTipText("");
    inPort.setText("4321");
    outPort.setToolTipText("");
    outPort.setText("4322");
    jTextArea1.setText("1111");
    jTextArea1.setTabSize(80);
    contentPane.add(scrollPane1,   new GridBagConstraints(0, 0, 6, 1, 1.0, 1.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 12, 0, 18), 369, 210));
    contentPane.add(jLabel1,                        new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.SOUTHWEST, GridBagConstraints.NONE, new Insets(0, 14, 0, 0), 6, 0));
    contentPane.add(inPort,new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 73, 0));
    contentPane.add(outPort,  new GridBagConstraints(4, 1, 2, 1, 0.0, 0.0
            ,GridBagConstraints.SOUTHWEST, GridBagConstraints.NONE, new Insets(0, -2, 0, 2), 67, 0));
    contentPane.add(jLabel2,    new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 5, 3, 10), 0, 0));
    contentPane.add(cmdClose,            new GridBagConstraints(5, 2, 1, 1, 0.0, 0.0
            ,GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(8, 5, 5, 34), 0, -3));
    contentPane.add(cmdSend,   new GridBagConstraints(3, 2, 2, 1, 1.0, 1.0
            ,GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(9, 2, 9, 0), 0, 0));
    contentPane.add(txtMessage,   new GridBagConstraints(0, 2, 3, 1, 0.0, 0.0
            ,GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(10, 15, 7, 3), 226, 0));
    scrollPane1.add(jTextArea1, null);
  }
  //Overridden so we can exit when window is closed
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }  void cmdClose_actionPerformed(ActionEvent e) {
    //if (mysend.isAlive() ==true){
      //mysend.sto
    //}
    //if (JOptionPane.showInternalOptionDialog()==1){
     System.exit(0);
    //} //.showMessageDialog(this,"hello xg.");  }  void cmdSend_actionPerformed(ActionEvent e) {
    //打开新线程发送文本信息
    //mysend = new communication("send");
    this.jTextArea1.insert("aaabbbccc",0)  ;
  }  //接口
  public interface apply1{    void refreshshow(String strMessage);
  }
}/////////////////////////////////////////////////////
package test_tcp;
import java.net.*;
import java.io.*;public class communication extends Thread implements Frame1.apply1 {//继承Thread类
  //希望通过接口引用文本框的方法
  public void refreshshow(String strMessage) {
    /**@todo Implement this test_tcp.Frame1$apply method*/
    throw new java.lang.UnsupportedOperationException("Method refreshshow() not yet implemented.");    Frame1.jTextArea1.insert("aaabbbccc",0);//编译出错
  }
}

解决方案 »

  1.   

    看你的代码没必要搞成两个类吗?
    如果是要在别的类里得到Frame1中的东西,你在Frame1里写个方法,比如:
    public JTextArea getTextArea(){
     return jTextArea1;
    }
    然后在你的这个类里调用这个方法
    或者你直接定义你所要用的东西为静态的:static,就能用你的那种方式直接访问。
      

  2.   

    因为需要做成多线程,所以要用communication类扩展Thread类。另外你说的重载getTextArea()怎样实现?communication类已经扩展Thread类了,只能引用接口(java不支持多重继承)。