以下代码总是在注释处提示:
"non-static variabel this cannot be referenced from a static context
                  button(new button1(),200,100)"
为什么呀?错误是什么意思呀?import javax.swing.*;
import java.awt.*;public class Button
{
//Creat a title string from the class name
public static String title(Object o)
{
String t = o.getClass().toString();
//Remove the word "class"
if(t.indexOf("class") != -1)
t = t.substring(6);
return t;
}
public static void run(JFrame frame,int width,int height)
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width,height);
frame.setVisible(true);
}
public static void run(JApplet applet,int width,int height)
{
JFrame frame = new JFrame(title(applet));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(applet);
frame.setSize(width,height);
applet.init();
applet.start();
frame.setVisible(true);
}
public static void run(JPanel panel,int width,int height)
{
JFrame frame = new JFrame(title(panel));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.setSize(width,height);
frame.setVisible(true);
}
public class Button1 extends JApplet
{
private JButton
b1 = new JButton("button1"),
b2 = new JButton("button2");
public void init()
{
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(b1);
cp.add(b2);
}
}
public static void main(String [] args)
{
Button.run(new Button1(),200,100);//此处总是出现错误
}
}

解决方案 »

  1.   

    public static class Button1 extends JApplet
    内部类加一个static修饰
      

  2.   

    PS:你的类的名字取的好怪哦,Button跟java.awt.Button重名了,,,使用的时候会给你带来不方便
      

  3.   

    如果不加static也行,如下:
    public static void main(String [] args)
    {
    Button btn=new Button();
    Button.Button1 btn1=btn.new Button1();
    Button.run(btn1,200,100);
    }
      

  4.   

    xsjohn() 说的不错,最主要的是搂住要理解static的用法
      

  5.   

    定义的run方法是不是也不行啊。
      

  6.   

    这run方法在thinking in javaIII里面见过,是用来显示JApplet的框架