CODE:ublic class textadd 
{
private Frame winframe; 
private Button scanhost;
private Button website;
private TextField ipadd;
private TextField weburl;
private TextArea result;

public static void main(String args[])
{
                  String rs="asdfasdf";
scanner window = new scanner();
window.init();
result.setText(rs+"\n");
} public void init()
{
winframe = new Frame("sdfsd");
winframe.addWindowListener(new thiswindowclosing());
winframe.setSize(600,350);
winframe.setBackground(Color.gray);
winframe.setLayout(new FlowLayout(FlowLayout.CENTER,20,30));
scanhost = new Button("fsdfds");
website = new Button("werwer");
ipadd = new TextField(20);
weburl = new TextField(20);
result = new TextArea(13,70);
result.setEditable(false);
winframe.add(ipadd);
winframe.add(scanhost);
winframe.add(weburl);
winframe.add(website);
winframe.add(result);
winframe.setVisible(true);
}

class thiswindowclosing extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(1);
}

}结果编译了出现下面的错误 无法从静态上下文中引用非静态 变量 result
                result.setText(rs+"\n");
                ^哪错了?谢谢!

解决方案 »

  1.   

    我怎么看不懂你的东西?你的main是不是应该这样:
    public static void main(String args[])
    {
                      String rs="asdfasdf";
    //scanner window = new scanner();
            textadd window = new textadd();
    window.init();
    window.result.setText(rs+"\n");
    }
      

  2.   

    你的main函数是static的函数,不能使用任何非static的变量和函数。
    你的result.setText要么放到init中;
    要么像楼上一样构造一个对象来调用;
    或者把其他部分全部写到构造函数中调用。