我目前在使用 Swing 时遇到问题,我试图从选项卡 1 中获取一个按钮,单击后会转到选项卡 2。据我所知,这里工作的两个代码块知道的,如下:
// Variables declaration - do not modify
private javax.swing.JTabbedPane jTabbedPane;
// End of variables declaration
private void addTabs() {
this.jTabbedPane.add("Home page", new Home());
this.jTabbedPane.add("Nieuwe Gebruiker", new UserNew());
this.jTabbedPane.add("Terugkerende gebruiker", new UserReturning());
this.jTabbedPane.add("Ingelogde gebruiker", new UserReturning());
this.jTabbedPane.add("Administrator", new Admin());
}
public void setTab(String tabName) {
this.jTabbedPane.setSelectedIndex(jTabbedPane.indexOfTab(tabName));
}
和:
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO Go to user logged in page
FullHouse.setTab("UserLoggedIn");
}
但是,后一段代码在调用 setTab 方法时出错:
non-static method setTab(String) cannot be referenced from a static context
我曾尝试将 setTab 方法设为静态,但随后 Netbeans 提示 jTabbedPane 不是静态的。我无法将 jTabbedPane 设为静态,因为 Netbeans 不允许我以这种方式编辑代码。
我该如何解决这个问题?
最佳答案
您需要有一个可以调用 setTab 的 FullHouse 实例。 换句话说,编译器问,你想在哪个 FullHouse 上调用 setTab?您希望在某个地方实例化它。
如果没有第一个代码块的更多细节,就无法提供更多细节。
关于java - 无法更改 Swing 中的 Activity 选项卡, "Non static method cannot be referenced",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27554478/