在尝试添加一个写有“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);
关于java - JFrame Eclipse 'add' 命令错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52058058/