JFrame Eclipse 'add' 命令错误
JFrame Eclipse 'add' command error
在尝试添加上面写有 "click me" 的按钮时,我收到以下错误消息:
Exception in thread "main" java.lang.Error: Unresolved compilation
problem: Cannot make a static reference to the non-static method
add(Component) from the type Container
import javax.swing.JButton;
import javax.swing.JFrame;
public class FirstFrame extends JFrame {
public static void main(String[] args) {
JFrame frame = new JFrame("My little frame");
JButton button = new JButton("Click Me");
add(button);
frame.setSize(300,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
如能解决此错误,我们将不胜感激!
add() 方法是一个实例方法,只能在(在本例中)框架的实例上调用。
尝试
frame.add(button);
在尝试添加上面写有 "click me" 的按钮时,我收到以下错误消息:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: Cannot make a static reference to the non-static method add(Component) from the type Container
import javax.swing.JButton;
import javax.swing.JFrame;
public class FirstFrame extends JFrame {
public static void main(String[] args) {
JFrame frame = new JFrame("My little frame");
JButton button = new JButton("Click Me");
add(button);
frame.setSize(300,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
如能解决此错误,我们将不胜感激!
add() 方法是一个实例方法,只能在(在本例中)框架的实例上调用。
尝试
frame.add(button);