以下代码总是在注释处提示:
"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);//此处总是出现错误
}
}
"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);//此处总是出现错误
}
}
内部类加一个static修饰
public static void main(String [] args)
{
Button btn=new Button();
Button.Button1 btn1=btn.new Button1();
Button.run(btn1,200,100);
}