当前位置: 技术问答>java相关
在静态上下文中不能引用非静态变量 this WindowAdapter e=new MyAdapter();why?
来源: 互联网 发布时间:2015-03-09
本文导语: MyExit.java:13: 在静态上下文中不能引用非静态变量 this WindowAdapter e=new MyAdapter(); 为什么? import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MyExit extends JFrame { public static ...
MyExit.java:13: 在静态上下文中不能引用非静态变量 this
WindowAdapter e=new MyAdapter();
为什么?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyExit extends JFrame
{
public static void main(String[] args)
{
JFrame my=new MyExit();
my.show();
WindowAdapter e=new MyAdapter();
my.addWindowListener(e);
}
public MyExit()
{
setSize(200,200);
}
class MyAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
}
下面这个程序为什么是对的
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyExit extends JFrame
{
public static void main(String[] args)
{
JFrame my=new MyExit();
my.show();
WindowAdapter e=new MyAdapter();
my.addWindowListener(e);
}
public MyExit()
{
setSize(200,200);
}
}
class MyAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
WindowAdapter e=new MyAdapter();
为什么?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyExit extends JFrame
{
public static void main(String[] args)
{
JFrame my=new MyExit();
my.show();
WindowAdapter e=new MyAdapter();
my.addWindowListener(e);
}
public MyExit()
{
setSize(200,200);
}
class MyAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
}
下面这个程序为什么是对的
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyExit extends JFrame
{
public static void main(String[] args)
{
JFrame my=new MyExit();
my.show();
WindowAdapter e=new MyAdapter();
my.addWindowListener(e);
}
public MyExit()
{
setSize(200,200);
}
}
class MyAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
|
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyExit extends JFrame
{
public static void main(String[] args)
{
JFrame my=new MyExit();
my.show();
WindowAdapter e=((MyExit)my).new MyAdapter();//inner class 只能由parent class建立实例
my.addWindowListener(e);
}
public MyExit()
{
setSize(200,200);
}
class MyAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
}
import java.awt.*;
import java.awt.event.*;
public class MyExit extends JFrame
{
public static void main(String[] args)
{
JFrame my=new MyExit();
my.show();
WindowAdapter e=((MyExit)my).new MyAdapter();//inner class 只能由parent class建立实例
my.addWindowListener(e);
}
public MyExit()
{
setSize(200,200);
}
class MyAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
}