Topic: 如何修改另一个类中变量的值

  Print this page

1.如何修改另一个类中变量的值 Copy to clipboard
Posted by: yikaikai
Posted on: 2007-04-28 14:17

class MyGUI extends JPanel
{
public JTextArea outTextArea;
...

public void init()
{
outTextArea.append("ok");
........
}
}

class Tcpdump implements PacketReceiver
{

public void receivePacket(Packet packet)
{
MyGUI.outTextArea.append(packet); //这里有问题,不知道怎么实现这个功能?
}
}

2.Re:如何修改另一个类中变量的值 [Re: yikaikai] Copy to clipboard
Posted by: hulizhong
Posted on: 2007-04-28 20:22

Do you mean to change the variable outTextArea of class MyGUI in class Tcpdump ?
eg:
class MyGUI extends JPanel
{
public JTextArea outTextArea;
//新增方法
public JTextArea getOutTextArea(){
return this.outTextArea;
}
...

public void init()
{
outTextArea.append("ok");
........
}
}

class Tcpdump implements PacketReceiver
{

public void receivePacket(Packet packet)
{

MyGUI.getOutTextArea().append(packet); //试试这样写行不?
//MyGUI.outTextArea.append(packet); //这里有问题,不知道怎么实现这个功能?
}
}

3.Re:如何修改另一个类中变量的值 [Re: yikaikai] Copy to clipboard
Posted by: yikaikai
Posted on: 2007-04-29 10:07

谢谢,就是想要这个效果, 不过报错:

D:\myids\Tcpdump.java:106: 无法从静态上下文中引用非静态 变量 this
return this.outTextArea;
^

4.Re:如何修改另一个类中变量的值 [Re: yikaikai] Copy to clipboard
Posted by: csLittleye
Posted on: 2007-04-30 09:59

inner class or static method

5.Re:如何修改另一个类中变量的值 [Re: yikaikai] Copy to clipboard
Posted by: yikaikai
Posted on: 2007-04-30 12:18

好了,谢谢


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923