Luk*_*ton 0 java replace this frame
我希望得到showGUI()方法,编译器说"this"不是静态变量,不能从静态上下文引用,我会用什么来代替"this"?我已经尝试过test.main(测试是它所在的包).我使用静态方法showGUI()的原因是因为我需要从另一个静态方法以及startup()方法调用该方法.以下是我的两个主要课程.
public class Main extends SingleFrameApplication {
@Override protected void startup() {
showGUI();
}
@Override protected void configureWindow(java.awt.Window root) {
}
public static Main getApplication() {
return Application.getInstance(Main.class);
}
public static void main(String[] args) {
launch(Main.class, args);
}
public static void showGUI() {
show(new GUI(this));
}
}
public class GUI extends FrameView {
public GUI(SingleFrameApplication app) {
super(app);
initComponents();
}
private void initComponents() {
//all the GUI stuff is somehow defined here
}
}
Run Code Online (Sandbox Code Playgroud)
好吧,this在静态方法中使用没有意义.this指的是类的特定实例,但static意味着这是一个不需要实例的方法,因此无法访问任何成员变量或方法.
只做showGUI非静态.
public void showGUI() {
show(new GUI(this));
}
Run Code Online (Sandbox Code Playgroud)